vignettes/dataframe_to_latex.Rmd
dataframe_to_latex.Rmd
We often want to include long table with lots of information, to be displayed in a paper. And very often, we have the contents of such a table as a data frame, e.g., in R. How do we go from an R data frame to a Latex table which can then be placed into a greater Latex manuscript file? It can be done on the fly. Here is how, using the FluxDataKit site list as an example.
The FluxDataKit site list is part of the FluxDataKit R package. To avoid complicated dependencies, it is also added as a file in this repository for demo purposes.
Read the site meta info file, select and re-name columns to be displayed in table, and re-format them as desired.
df <- read_csv(here("data/fdk_site_info.csv")) |>
mutate(
lon = format(lon, digits = 2),
lat = format(lat, digits = 2),
elv = format(elv, digits = 2),
year_start = as.character(as.integer(year_start)),
year_end = as.character(as.integer(year_end))
) |>
select(
ID = sitename,
`Lon.` = lon,
`Lat.` = lat,
`Ele.` = elv,
`Start year` = year_start,
`End year` = year_end,
`Veg. type` = igbp_land_use
) |>
# for demo, include only top 30 rows
slice(1:30)
Write Latex-formatted source code for table into a file using {xtable}. First, generate text.
latextable <- xtable::xtable(
df,
caption = "FluxDataKit site list.",
align = rep("l", (ncol(df) + 1)) # make all columns left-aligned
)
This looks like that:
latextable
## % latex table generated in R 4.5.1 by xtable 1.8-4 package
## % Tue Aug 26 07:45:42 2025
## \begin{table}[ht]
## \centering
## \begin{tabular}{llllllll}
## \hline
## & ID & Lon. & Lat. & Ele. & Start year & End year & Veg. type \\
## \hline
## 1 & AR-SLu & -66.46 & -33.5 & 500.0 & 2010 & 2010 & MF \\
## 2 & AR-TF1 & -66.73 & -55.0 & 40.0 & 2016 & 2018 & WET \\
## 3 & AT-Neu & 11.32 & 47.1 & 970.0 & 2002 & 2012 & GRA \\
## 4 & AU-ASM & 133.25 & -22.3 & 606.0 & 2011 & 2017 & SAV \\
## 5 & AU-Cow & 145.43 & -16.2 & 86.0 & 2010 & 2015 & EBF \\
## 6 & AU-Cpr & 140.59 & -34.0 & 76.0 & 2011 & 2017 & SAV \\
## 7 & AU-Ctr & 145.45 & -16.1 & 66.0 & 2010 & 2017 & EBF \\
## 8 & AU-Cum & 150.72 & -33.6 & 200.0 & 2013 & 2018 & EBF \\
## 9 & AU-DaP & 131.32 & -14.1 & 116.0 & 2009 & 2012 & GRA \\
## 10 & AU-DaS & 131.39 & -14.2 & 108.0 & 2010 & 2017 & SAV \\
## 11 & AU-Dry & 132.37 & -15.3 & 191.0 & 2011 & 2015 & SAV \\
## 12 & AU-Emr & 148.47 & -23.9 & 177.0 & 2012 & 2013 & GRA \\
## 13 & AU-GWW & 120.65 & -30.2 & 504.0 & 2013 & 2017 & SAV \\
## 14 & AU-Gin & 115.65 & -31.4 & 51.0 & 2012 & 2017 & WSA \\
## 15 & AU-How & 131.15 & -12.5 & 41.0 & 2003 & 2017 & WSA \\
## 16 & AU-Lit & 130.79 & -13.2 & 200.0 & 2016 & 2017 & WSA \\
## 17 & AU-Otw & 142.82 & -38.5 & 54.0 & 2009 & 2010 & GRA \\
## 18 & AU-Rig & 145.58 & -36.6 & 133.0 & 2011 & 2016 & GRA \\
## 19 & AU-Rob & 145.63 & -17.1 & 710.0 & 2014 & 2017 & EBF \\
## 20 & AU-Sam & 152.88 & -27.4 & 170.0 & 2011 & 2017 & GRA \\
## 21 & AU-Stp & 133.35 & -17.2 & 252.0 & 2010 & 2017 & GRA \\
## 22 & AU-TTE & 133.64 & -22.3 & 553.0 & 2013 & 2017 & GRA \\
## 23 & AU-Tum & 148.15 & -35.7 & 1200.0 & 2002 & 2017 & EBF \\
## 24 & AU-Whr & 145.03 & -36.7 & 152.0 & 2015 & 2016 & EBF \\
## 25 & AU-Wrr & 146.65 & -43.1 & 100.0 & 2016 & 2017 & EBF \\
## 26 & AU-Ync & 146.29 & -35.0 & 125.0 & 2011 & 2017 & GRA \\
## 27 & BE-Bra & 4.52 & 51.3 & 16.0 & 1996 & 2020 & MF \\
## 28 & BE-Dor & 4.97 & 50.3 & 253.0 & 2011 & 2020 & GRA \\
## 29 & BE-Lcr & 3.85 & 51.1 & 5.0 & 2019 & 2020 & DBF \\
## 30 & BE-Lon & 4.75 & 50.6 & 170.0 & 2004 & 2020 & CRO \\
## \hline
## \end{tabular}
## \caption{FluxDataKit site list.}
## \end{table}
Then, write text to file.