I am building a beamer presentation in rmarkdown and I have a long table that I create using kable and kableExtra, and splitting between two slides. I have been able to split the table fine, but I am having trouble getting the header to repeat. The solutions I have found on the Tex stackexchange pages suggest using different packages, or manually splitting the table in the Tex file. I don't think the additional package suggested, xtab, is compatible with kableExtra. Here is and example of what I am working on.
---
title: Title
author: James
classoption: table
output:
beamer_presentation:
keep_tex: true
includes:
in_header: ./R presentaion/header.tex
---
```{r setup, include=FALSE}
library(knitr)
library(kableExtra)
library(magrittr)
options(knitr.kable.NA = '')
```
```{r, echo=FALSE}
long_dt <- rbind(mtcars, mtcars)
```
#Slide {.allowframebreaks}
```{r, echo=FALSE, results='asis'}
kable(mtcars, format = "latex", longtable = TRUE, booktabs = TRUE) %>%
add_header_above(c(" ", "Group 1" = 5, "Group 2" = 6)) %>%
kable_styling(latex_options = c("striped", "repeat_header"), font_size = 6)
```
This is what the template file looks like.
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage[normalem]{ulem}
\usepackage{makecell}
\usepackage{xcolor}
\usepackage{xtab}
\def\begincols{\begin{columns}}
\def\begincol{\begin{column}}
\def\endcol{\end{column}}
\def\endcols{\end{columns}}
\def\begincols{\begin{columns}}
\def\begincol{\begin{column}}
\def\endcol{\end{column}}
\def\endcols{\end{columns}}
Related
In a LaTex beamer presentation generated with rmarkdown::beamer_presentation, is there a way to highlight the content of a specific cell of a kableExtra table upon clicking?
MWE
---
output:
bookdown::pdf_book:
base_format: rmarkdown::beamer_presentation
latex_engine: xelatex
slide_level: 2
---
```{r}
library(dplyr)
library(kableExtra)
```
## Table
```{r table, echo = FALSE}
my_df <- mtcars[1:4, 1:2]
my_table <- kable(my_df, booktabs = T, caption = "Table caption")
my_table
```
The proposed solution uses kableExtra's row_spec/column_spec/cell_spec functionality, for which several LaTex packages have to be loaded (e.g., via header-includes: ...).
The solution works with only one little thing remaining: the table numeration increases.
That is, the table on the first frame is numbered "1" and on the second frame with "2".
To make things really look like the added color is the only difference between both frames, the numeration would ideally be the same for both tables.
To color the rows/columns/cells as you need, see also the rmarkdown cookbook for more info on how to use row_spec/column_spec/cell_spec.
---
output:
bookdown::pdf_book:
base_format: rmarkdown::beamer_presentation
latex_engine: xelatex
slide_level: 2
header-includes:
- \usepackage{booktabs}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage{makecell}
---
```{r include=FALSE}
library(dplyr)
library(kableExtra)
```
## Table
```{r table, echo = FALSE}
my_df <- mtcars[1:4, 1:2]
my_table <- kable(my_df, booktabs = T, caption = "Table caption")
my_table
```
## Table {.noframenumbering}
<!-- Add "{.noframenumbering}" such that the frame number is the same on both frames -->
```{r table-with-colored-cell, echo = FALSE}
special_cell <- c(rep(F, 3), T)
colors_of_column_with_special_cell <- c(rep("black", 3), "red")
my_table %>%
column_spec(2, strikeout = special_cell, bold = special_cell, color = colors_of_column_with_special_cell)
```
I am trying to automatically compile multiple individual PDF reports from RMarkdown using a for loop in a separate R script. There appears to be an issue using the kable_styling() function from the kableExtra package when compiling multiple reports, but not for a single report.
This is the render loop in a separate script:
mtcars = data.frame(mtcars)
for (car in unique(rownames(mtcars))){
rmarkdown::render('mtcars_report.Rmd', # file 2
output_file = paste("report_", car, ".pdf"))
}
Here is the YAML header used for all reports:
title: "mtcars_report"
output:
pdf_document: default
html_document:
df_print: paged
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage[normalem]{ulem}
- \usepackage[utf8]{inputenc}
- \usepackage{makecell}
- \usepackage{xcolor}
geometry: margin=.8cm
This RMD code works with this loop and renders each PDF:
knitr::opts_chunk$set(echo = TRUE)
library(rmarkdown)
library(dplyr)
library(kableExtra)
mtcars = data.frame(mtcars)
cars = mtcars[rownames(mtcars)==car,]
{r, echo=FALSE, message=FALSE}
cars_table = cars %>%
select(mpg, cyl)%>%
t() %>%
kable("latex")
cars_table
This add a kable_styling command, the PDFs do not render:
cars_table = cars %>%
select(mpg, cyl)%>%
t() %>%
kable("latex") %>%
kable_styling(latex_options=c("striped", "float_right", "hold_position"),full_width = FALSE)
cars_table
I get this error: ! Undefined control sequence.
\rowcolor
If I knit the RMD as a single file without the loop (i.e. cars = mtcars[rownames(mtcars)=="Merc 280",] it DOES work with the kable_styling . It just appears to be an issue when I try to render it with the loop. I have updated all packages to my knowledge running:
update.packages(ask = FALSE, checkBuilt = TRUE) # update R packages
tinytex::tlmgr_update() # update LaTeX packages
This question already has answers here:
Using an R variable before the code chunk in which the variable was created
(4 answers)
Closed 3 years ago.
I'm producing a pdf report with rmd. Before some plots I write some explanations about them, but their code and data haven't been produced yet, they are in code chunks after these explanations. The problem is, I need to access a R variable that is evaluated in one of those chunks.
In other words, I want to access a R variable before it's evaluation.
There is an example below:
---
title: ''
geometry: left=18mm, right=19mm, top=20mm, bottom=25mm
output:
pdf_document:
latex_engine: xelatex
header-includes:
- \usepackage{multicol}
- \usepackage{caption}
- \usepackage{graphicx}
- \usepackage{grffile}
- \usepackage{float}
- \usepackage{units}
- \usepackage{environ}
- \usepackage{setspace}
- \usepackage{colortbl}
- \usepackage{xcolor}
- \usepackage{fancyhdr}
- \usepackage{subfig}
- \usepackage{longtable}
- \usepackage{pdflscape}
- \usepackage{indentfirst}
- \usepackage{setspace}
tables: true
keep_tex: true
indent: true
fontsize: 12pt
---
```{r global_options, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
opts_knit$set(eval.after = "fig.cap")
knitr::opts_chunk$set(fig.pos = '!h')
options(knitr.kable.NA = '')
```
I want to access a R variable here, for example, `r variable`, that is in a chunck after this text.
```{r}
variable <- 2+2
```
Thanks in advance.
If your variable are staying the same during the document, I suggest you insert them in the R options chunks if you're going to use them more than one time.
---
title: 'A nice title'
geometry: left=18mm, right=19mm, top=20mm, bottom=25mm
output:
pdf_document:
latex_engine: xelatex
header-includes:
- \usepackage{multicol}
- \usepackage{caption}
- \usepackage{graphicx}
- \usepackage{grffile}
- \usepackage{float}
- \usepackage{units}
- \usepackage{environ}
- \usepackage{setspace}
- \usepackage{colortbl}
- \usepackage{xcolor}
- \usepackage{fancyhdr}
- \usepackage{subfig}
- \usepackage{longtable}
- \usepackage{pdflscape}
- \usepackage{indentfirst}
- \usepackage{setspace}
tables: true
keep_tex: true
indent: true
fontsize: 12pt
---
```{r global_options, include=FALSE}
# packages
library(knitr)
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
opts_knit$set(eval.after = "fig.cap")
knitr::opts_chunk$set(fig.pos = '!h')
options(knitr.kable.NA = '')
## preload your R variables here ##
variable_1 <- 2+2
variable_2 <- c("a", "b", "c")
variable_3 <- head(mtcars[, 2:5])
variable_4 <- plot(variable_3)
## preload your R variables here ##
```
The variable already runned/loaded in the ``R options`` chunk will be available at all time in the R Markdown document.
```{r}
print("Here is the value of the variable_1 :")
print(variable_1)
```
I am using the following template
---
title: "Nice try buddy"
author: "SpaceMan"
date: "13 December 2057"
output:
bookdown::pdf_document2
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage[table]{xcolor}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage{makecell}
---
---
references:
- id: fenner2012a
title: One-click science marketing
container-title: Nature Materials
volume: 11
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Title
\begin{equation}
f\left(k\right)=\binom{n}{k}p^k\left(1-p\right)^{n-k} \label{eq:binom}
\end{equation}
You may refer to it using `\#ref(eq:binom)`, e.g., see Equation \#ref(eq:binom).
and not a nice citation! #fenner2012a
## Including Tables
You can also embed tables, for example: \#ref(tab:tw)
```{r tw, echo=FALSE}
mytable
```
## References
where mytable is stored in R session and is generated with
mytable <- head(cars) %>% kable(format = "latex",
booktabs = T,
caption = "Demo Table",
escape = F) %>%
kable_styling(latex_options = 'HOLD_position')
Now, this is supposed to work, but when I knit the document using
rmarkdown::render('C:\\Users\\john\\Documents\\bbv.Rmd')
the cross-reference for the table is not there! I only see ??
and the table has this weird #tab thing - how to get rid of it ?
the TOC is here even though I did not ask for it
Any ideas how to fix these issues?
Thanks!
EDIT: the weird #tab thing disappeared after a reboot.
The problem is that you are working against the intentions of kable by using it outside of an R chunk:
The kable() function will automatically generate a label for a table environment, which is the prefix tab: plus the chunk label.
https://bookdown.org/yihui/bookdown/tables.html
So the following workaround is definitely on the hacky side. Using a file foo.Rmd with
---
output:
bookdown::pdf_document2:
toc: no
header-includes:
- \usepackage{float}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Including Tables
You can also embed tables, for example: \#ref(tab:tw)
```{r tw, echo=FALSE}
mytable
```
You can also embed tables, for example: \#ref(tab:tw2)
```{r tw2, echo=FALSE}
mytable2
```
Referencing images is easier: \#ref(fig:plt)
```{r plt, echo=FALSE, fig.cap = 'hello', fig.height=3}
myplot
```
one can process this file with a second file foo.R:
library(knitr)
library(kableExtra)
# add the label to the options that would normally be populated from the chunk options
opts_current$append(list(label = "tw"))
mytable <- head(cars) %>% kable(format = "latex",
booktabs = T,
caption = "Demo Table",
escape = F) %>%
kable_styling(latex_options = 'HOLD_position')
opts_current$restore()
opts_current$append(list(label = "tw2"))
mytable2 <- tail(cars) %>% kable(format = "latex",
booktabs = T,
caption = "Demo Table",
escape = F) %>%
kable_styling(latex_options = 'HOLD_position')
opts_current$restore()
myplot <- ggplot(cars, aes(x = dist, y = speed)) + geom_point()
rmarkdown::render("foo.Rmd")
In principle, you can do these commands also just at the R prompt, but I try to not use the prompt directly. BTW, I do not get the (#tab) output with your code.
However, I think it makes more sense to not work against the workings of kable. I can understand that it can make sense to separate the data manipulation fro the presentation. However, creating the table is presentation from my point of view. So instead of creating the table externally I would just create the data externally. To make this concrete, let's use a file bar.Rmd:
---
output:
bookdown::pdf_document2:
toc: no
header-includes:
- \usepackage{float}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)
```
## Including Tables
You can also embed tables, for example: \#ref(tab:tw)
```{r tw, echo=FALSE}
mydata %>% kable(format = "latex",
booktabs = T,
caption = "Demo Table",
escape = F) %>%
kable_styling(latex_options = 'HOLD_position')
```
together with a file bar.R:
# insert data processing here
mydata <- head(cars)
rmarkdown::render("bar.Rmd")
This gives me the same output and the data processing is (initially!) separated from the presentation.
How can a landscape table be plotted in R Markdown (PDF output) without causing a page break to be inserted?
There is the function landscape from the kableExtra package, but this forces a page break to be inserted.
Example:
The normal behaviour for tables in R Markdown is that the will float to minimise the breaking up of text.
---
output: pdf_document
---
Some Text
```{r, echo=F, warning=F}
library(kableExtra)
knitr::kable(mtcars, format = "latex", caption = "A table")
```
More Text
Landscape:
---
output: pdf_document
---
Some Text
```{r, echo=F, warning=F}
library(kableExtra)
knitr::kable(mtcars, format = "latex", booktabs = T, caption = "A table") %>%
landscape()
```
More Text
You can use the LaTeX package realboxes to do what you want
---
title: "Mixing portrait and landscape"
output: pdf_document
header-includes:
- \usepackage[graphicx]{realboxes}
- \usepackage{booktabs}
---
Some text
\Rotatebox{90}{
```{r, echo=FALSE, warning=FALSE}
knitr::kable(mtcars, "latex", booktabs = TRUE)
```
}
More text
This produces a single page pdf with the table presented in landscape. The problem with this approach is that it does not seem to work with a caption.
Edit You can use the caption latex package to add the caption
---
title: "Mixing portrait and landscape"
output: pdf_document
header-includes:
- \usepackage[graphicx]{realboxes}
- \usepackage{booktabs}
- \usepackage{caption}
---
Some text
\begingroup
\captionsetup{type=table}
\caption{A table}
\Rotatebox{90}{
```{r, echo=FALSE, warning=FALSE}
knitr::kable(mtcars, "latex", booktabs = TRUE)
```
}
\endgroup
More text
This produces the table in landscape with caption