I want to rotate table output by 90 degrees on pdf. I am using markdown to generate a report and kable to display the tables in a loop. If possible, I would like to continue using kable since there are lot of other things which are dependent on it that I haven't included in this MWE.
This is a simple example using iris dataset. I tried using landscape function from this post Rotate a table from R markdown in pdf
---
output: pdf_document
header-includes:
\usepackage{lscape}
\usepackage{pdfpages}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
Report page -
```{r results='asis'}
library(knitr)
library(kableExtra)
for (i in 1:3) {
print(landscape(kable_styling(
kable(iris[i:(i+5), ], format = "latex", align = "c", booktabs = TRUE,
longtable = TRUE, row.names = FALSE), latex_options = c("striped"), full_width = T)))
}
```
But this only rotates the page number keeping the table as it is.
I am actually looking for a solution which provides me the output in this way -
To clarify, all the pages with table data in it (3 for this example) should be rotated whereas rest of them should remain as it is. Also, I need longtable = TRUE in kable since in my actual example I am printing lot of rows.
Use package rotating
I added a simple example for you.
---
title: "test"
header-includes: \usepackage[figuresright]{rotating}
#or \usepackage[figuresleft]{rotating}
output:
pdf_document:
latex_engine: xelatex
---
```{r setup, include = FALSE}
library(flextable)
ft <- flextable(head(mtcars))
```
\begin{sidewaysfigure}
`r ft`
\end{sidewaysfigure}
```
Further you can modify it for your tasks ;)
I found another way using rotatebox.
---
output: pdf_document
header-includes:
\usepackage{lscape}
\usepackage{pdfpages}
\usepackage{graphicx}
\usepackage[figuresright]{rotating}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
Report page -
```{r results='asis', warning=FALSE, message=FALSE}
library(knitr)
library(kableExtra)
for (i in 1:3) {
cat('\\rotatebox{90}{')
print(kable(iris[i:(i+5), ], format = "latex", align = "c", booktabs = TRUE,
row.names = FALSE))
cat('}')
cat("\n\\newpage\n")
}
```
Related
library(summarytools)
library(stargazer)
view(dfSummary(DataV2,graph.col = TRUE), method = "render")
I don't think summarytools has a way to produce LaTeX directly, but it can produce Markdown output, and the rmarkdown package can convert that to LaTeX. For example:
---
title: "Untitled"
date: "17/02/2022"
output:
pdf_document:
keep_tex: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r results='asis'}
library(summarytools)
library(stargazer)
dfSummary(tobacco,
plain.ascii = FALSE,
style = 'grid',
graph.magnif = 0.85,
varnumbers = FALSE,
valid.col = FALSE,
tmp.img.dir = "/tmp")
```
Because I used the keep_tex: true option in the YAML, it outputs the .tex file as well as the PDF, and you could theoretically extract the LaTeX from there if you weren't using R Markdown for the rest of the document. It might not be easy, because of all the embedded figures.
Default R Markdown captions in Word appear as:
Figure 1: Figure Title
Table 1: Table Title
I would like to format my captions so that the Figure 1/Table 1 is bold and the title is italic on the next line. This is the APA style of captions:
Figure 1
Figure Title
I have been trying to figure out an easy way to do this, but can't seem to find a solution. (If figure captions can be above figure, rather than below, that would also be great!). Here is a minimal reprex:
---
title: "Untitled"
author: "Test"
date: "11/8/2020"
output:
bookdown::word_document2: default
---
{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(flextable)
{r table, tab.cap="Descriptives", echo=FALSE, message=FALSE, warning=FALSE}
psych::describe(cars) %>%
as.data.frame() %>%
flextable() %>%
set_table_properties(layout = "autofit", width = 1)
{r fig, fig.cap="Pressure Figure", echo=FALSE, message=FALSE, warning=FALSE}
plot(pressure)
For tables you can do it with officedown, officer and flextable packages, styling in reference_doxc, and by taking the advantage of "\n\n" for tables.
In yaml in the pre argument you set up a prefix for every table and a separator between the number and the title itself.
---
title: "Untitled"
author: "Test"
date: "11/8/2020"
output:
officedown::rdocx_document:
tables:
caption:
pre: 'Table '
sep: ''
---
With
tab.cap.fp_text = officer::fp_text_lite()
you can define the formatting of the caption prefix. In APA style it would be bold without italics, preferrably configured in the global knitr options, e.g.
knitr::opts_chunk$set(tab.cap.fp_text = officer::fp_text_lite(italic = FALSE, bold = TRUE))
And you add
"\n\n"
to the table caption to produce a new line
{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, tab.cap.fp_text = officer::fp_text_lite(italic = FALSE, bold = TRUE))
library(tidyverse)
library(flextable)
{r table, tab.cap="\n\nDescriptives", echo=FALSE, message=FALSE, warning=FALSE}
psych::describe(cars) %>%
as.data.frame() %>%
flextable() %>%
set_table_properties(layout = "autofit", width = 1)
The last thing is to create a reference_doxc in which you add italics to the table caption style. The caption prefix is not affected, as you control it with the knitr options.
When I want to print some tables in html report from knitr, sometime I use knitr::kable(), and other times I use htmltable::htmltable.
When I use bookdown::html_document2 in the YAML numbering of tables from kable is automatic. However, it is not for htmltable, and I need to use options(table_counter = TRUE), which generates another numbering mechanism. Is there a way to unify it?
Example:
---
title: "Untitled"
author: "Guilherme"
date: "10/26/2020"
output:
bookdown::html_document2
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(table_counter = TRUE)
```
```{r cars}
library(htmlTable)
htmlTable(mtcars[1:5,1:5],
caption = "XX")
```
```{r}
htmlTable(mtcars[1:5,1:5],
caption = "XX")
```
```{r}
library(knitr)
kable(mtcars[1:5,1:5],
caption = "XX")
```
Outputs:
Thanks!
When I embed a table generated by using knitr::kable,
I want to adjust the width of the table in beamer_presentation's slide.
Like this picture, some columns don't show in slide when executing the following codes.
Could you tell me how to solve this problem without changing column's names?
---
title: "test"
author: "test"
date: "`r Sys.time()`"
output:
beamer_presentation:
latex_engine: xelatex
fontsize: 6pt
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(include = FALSE)
library(tidyverse)
library(kableExtra)
library(knitr)
```
## Table
```{r test, results = "asis",include=TRUE}
test_tbl = tibble(
example_name1 = c("a", "b"),
example_name2 = c("aa", "bb"),
example_name3 = c("aaa", "bbb"),
example_name4 = c("aaaa", "bbbb"),
example_name5 = c("aaaaa", "bbbbb")
)
knitr::kable(test_tbl, format = "markdown")
```
The table is too large to fit on one slide. By adjusting the font size with kableExtra the size of the table can be changed. To do this, set the argument format = "latex" in kable() and then pipe kable_styling().
knitr::kable(test_tbl, format = "latex") %>%
kableExtra::kable_styling(font_size = 7)
It is useful to include LaTeX packages like booktabs to format tables nicely with kable or kableExtra. This is not as flexible as working with LaTeX directly but often leads to quite good results.
First of all, include booktabs in the YAML header.
---
title: "test"
author: "test"
date: "`r Sys.time()`"
header-includes:
- \usepackage{booktabs}
output: beamer_presentation
---
Then set booktabs = TRUE in kable().
knitr::kable(test_tbl, format = "latex", booktabs = T) %>%
kableExtra::kable_styling(font_size = 7)
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.