I have a table with squirrel sightings, where I have species, year, total abundance and mean_abundance, like below:
library(tibble)
set.seed(42)
abundance_sum <- tibble(
Species = sample(c("Sciurus vulgaris", "Tamias striatus", "Marmota monax", "Sciurus niger", "Glaucomys volans"), 100, replace = TRUE),
Year = sample(2015:2023, 100, replace = TRUE),
Total_abundance = round(runif(100, 1, 500), 1),
n_observations = sample(1:10, 100, replace = TRUE))
print(abundance_sum)
I love the latex aesthetic for table, and when I knit my markdown file it works, but it is too long, so I can't have my whole table on one page.
abundance_sum %>%
arrange(Species, Year) %>%
select(Year, Total_abundance, n_observations) %>%
kbl(format="latex", booktabs = T) %>%
kable_styling(latex_options = c("striped", "hold_position")) %>%
pack_rows(index = table(abundance_sum$Species), label_row = TRUE)
#%>%
#save_kable("kable.pdf")
# this is what I tried to save my table in latex and get it in pdf format
I tried to do a save_kable, but I doesn't work when I precise format = "latex". It gives me this message :
Error: LaTeX failed to compile /Users/klervilecorre/Desktop/Stage\ M2/Data/kable.tex. See for debugging tips.
So I went to the link, tried to debug tinytex, tried to uninstall Latex, then reinstall it, nothing worked... Maybe it is because I work on Mac ?
With other formats (html, simple...) it works, but I don't like the aesthetics of the table.
Is there another way to save it with an aesthetic like the table in this image ?
I have a table with squirrel sightings, where I have species, year, total abundance and mean_abundance, like below:
library(tibble)
set.seed(42)
abundance_sum <- tibble(
Species = sample(c("Sciurus vulgaris", "Tamias striatus", "Marmota monax", "Sciurus niger", "Glaucomys volans"), 100, replace = TRUE),
Year = sample(2015:2023, 100, replace = TRUE),
Total_abundance = round(runif(100, 1, 500), 1),
n_observations = sample(1:10, 100, replace = TRUE))
print(abundance_sum)
I love the latex aesthetic for table, and when I knit my markdown file it works, but it is too long, so I can't have my whole table on one page.
abundance_sum %>%
arrange(Species, Year) %>%
select(Year, Total_abundance, n_observations) %>%
kbl(format="latex", booktabs = T) %>%
kable_styling(latex_options = c("striped", "hold_position")) %>%
pack_rows(index = table(abundance_sum$Species), label_row = TRUE)
#%>%
#save_kable("kable.pdf")
# this is what I tried to save my table in latex and get it in pdf format
I tried to do a save_kable, but I doesn't work when I precise format = "latex". It gives me this message :
Error: LaTeX failed to compile /Users/klervilecorre/Desktop/Stage\ M2/Data/kable.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips.
So I went to the link, tried to debug tinytex, tried to uninstall Latex, then reinstall it, nothing worked... Maybe it is because I work on Mac ?
With other formats (html, simple...) it works, but I don't like the aesthetics of the table.
Is there another way to save it with an aesthetic like the table in this image ?
Share Improve this question asked Feb 6 at 9:03 KlerviKlervi 112 bronze badges 3 |2 Answers
Reset to default 0While your code is not reproducible, I did manage to get the LaTeX output using kableExtra
and running it in LaTeX. Please do the following to get (closest possible) what you want:
- Get the .tex file from the
kableExtra
in R. - Input the table TeX file in your main document.
- Use a package like
longtable
orxltabular
in LaTeX to get your eventual output.
library(tibble)
library(kableExtra)
set.seed(42)
abundance_sum <- tibble(
Species = sample(c("Sciurus vulgaris", "Tamias striatus", "Marmota monax", "Sciurus niger", "Glaucomys volans"), 100, replace = TRUE),
Year = sample(2015:2023, 100, replace = TRUE),
Total_abundance = round(runif(100, 1, 500), 1),
n_observations = sample(1:10, 100, replace = TRUE))
# use `kableExtra` to generate the tex code
tbl_df <-
abundance_sum %>%
arrange(Species, Year) %>%
select(Year, Total_abundance, n_observations) %>%
kbl(format="latex", booktabs = T) %>%
kable_styling(latex_options =
c("striped", "hold_position")) %>%
pack_rows(index = table(abundance_sum$Species),
label_row = TRUE)
# save the table to tex file stored to a folder called `tabs` within your project
writeLines(tbl_df, "tabs/table_ex.tex")
Normally, for long tables you would use longtable
in LaTeX, which devides them into subtables. This would be a fix for your problem, but I am not sure, if you want the table to be devided over many pages. If you do, you can create an R-Quarto Document of type pdf
and paste the following code:
---
title: "Untitled"
format: pdf
editor: visual
---
```{r}
#| echo: false
#| warning: false
library(tibble)
library(kableExtra)
library(tidyverse)
abundance_sum <- tibble(
Species = sample(c("Sciurus vulgaris", "Tamias striatus", "Marmota monax", "Sciurus niger", "Glaucomys volans"), 100, replace = TRUE),
Year = sample(2015:2023, 100, replace = TRUE),
Total_abundance = round(runif(100, 1, 500), 1),
n_observations = sample(1:10, 100, replace = TRUE))
kbl(abundance_sum %>%
arrange(Species, Year) %>%
select(Year, Total_abundance, n_observations),
format = "latex",
booktabs = TRUE,
longtable = T,
font_size = 8) %>% # Reduce font size
kable_styling(
latex_options = c("striped","repeat_header"),
full_width = FALSE) %>% # This helps with scaling
pack_rows(index = table(abundance_sum$Species))
```
If you just want the table output on one page
we could just make a really long page...
---
title: "Untitled"
format:
pdf:
geometry:
- top=15mm
- paperheight=40 in
editor: visual
---
```{r}
#| echo: false
#| warning: false
library(tibble)
library(kableExtra)
library(tidyverse)
abundance_sum <- tibble(
Species = sample(c("Sciurus vulgaris", "Tamias striatus", "Marmota monax", "Sciurus niger", "Glaucomys volans"), 100, replace = TRUE),
Year = sample(2015:2023, 100, replace = TRUE),
Total_abundance = round(runif(100, 1, 500), 1),
n_observations = sample(1:10, 100, replace = TRUE))
kbl(abundance_sum %>%
arrange(Species, Year) %>%
select(Year, Total_abundance, n_observations),
format = "latex",
booktabs = TRUE,
font_size = 8) %>% # Reduce font size
kable_styling(
latex_options = c("striped"),
full_width = FALSE) %>% # This helps with scaling
pack_rows(index = table(abundance_sum$Species))
```
longtable
in LaTeX in conjunction with a Quarto Document. I will post an answer. – dog Commented Feb 6 at 15:09