I'm trying to generate a summary table which is quite long in a pdf format.
But the table is breaking down at the end of the page (See the image below). I guess it is something to do with page margin.
I have checked the online documentation. It has argument page.margin.bottom in tab_options() to work with a RTF format. Is there any workaround for pdf format.
The following example closely mimics my problem. But table is not breaking at the end. However, it is very close to the page margin.
library(gt)
library(tidyverse)
d = iris %>% gt()
d
gtsave(d, filename = "long_table.pdf", zoom = 0.7)
Thanks.
I have a solution using RMarkdown that converts it into a PDF. Knit (Cntrl+Shift+K) the Rmd file in RStudio to create the pdf file in your working directory.
Normally, RMarkdown uses 3 backticks (e.g. ```{r codechunk}) to create and end a code chunk. Since stackoverflow also uses 3 backticks to create a codeblock, I will swap out the Rmd codechunk 3 backticks for *** instead.
Output screenshot: https://prnt.sc/vhO5Px59mWYQ
---
title: "test_pdf"
output: pdf_document
---
***{r setup, include=FALSE}
library(tidyverse)
library(gt)
tinytex::install_tinytex() #install this if you have not already
knitr::opts_chunk$set(echo = TRUE)
***
***{r iris2, echo=F}
dfA <- iris
dfB <- iris
df <- bind_rows(dfA, dfB)
d <- df %>% gt()
d
***
Related
I need to generate an HTML document which displays a gt table. At the same time, it should export the table as a rtf file. I have the following process, but when I knit the document I get an unexpected output in the rtf file which is different from when I run the code interactively.
---
title: "rtf test"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r results='asis'}
library(tidyverse)
library(gt)
out <- mtcars %>%
head() %>%
gt()
print(out)
gtsave(out, filename = "rtf_test.rtf")
```
The rtf output look like the following:
!!!!!RAW-KNITR-CONTENT{\rtf\ansi\ansicpg1252{\fonttbl{\f0\froman\fcharset0\fprq0 Calibri;}{\f1\froman\fcharset0\fprq0 Courier;}}{\colortbl;\red211\green211\blue211;}
\trowd\trrh0\trhdr
\pard\plain\uc0\qr\clvertalc\clpadb50\clpadfb3\clpadr50\clpadfr3\clpadl50\clpadfl3\clpadt50\clpadft3\clbrdrt\brdrs\brdrw40\brdrcf1\clbrdrb\brdrs\brdrw40\brdrcf1\clbrdrl\brdrs\brdrw20\brdrcf1\clbrdrr\brdrs\brdrw20\brdrcf1\cellx861
\intbl {\f0 {\f0\fs20 mpg}}\cell
\pard\plain
...
Instead of the desired table in the rtf file which I get when I run the code interactively:
I have tried using writeLines instead of gtsave and removing results = 'asis' from the code chunk but nothing I do seems to export the table as I want it. Any help is appreciated.
This works in a usual code chunk in R markdown:
m1_aov <- anova(m1)
m1_aov$`Sum Sq`[2] %>% round(3)
Unfortunately, using the latter in inline code breaks the knitr parser down
`r m1_aov$`Sum Sq`[2] %>% round(3)`
Indeed, it also breaks Stackoverflow.
I looked at this related question but could not infer a working solution to my problem. Any hint?
Expanding the comment with a working example:
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r}
a <- tibble::tibble(`a column` = 1:10) # using tibble to get a column name with a white space
m <- mean(a$`a column`)
```
Mean is `r m`
To me this looks like a neat trick because it avoids to include unecessary long code inside the text, and do not create the problem you are facing at the (small) cost of creating new objects.
The output:
I have a table in a Rmd file to print to pdf in which I need to add a dagger symbol into a column header. The basic test code is:
---
title: "Untitled"
author: "L. G. Hunsicker"
date: "4/29/2022"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)
library(magrittr)
library(knitr)
library(kableExtra)
```
```{r test}
df <- data.frame(x = 1:10)
names(df)[1] <- 'maxCpep (ng/mL) †'
df %>% kbl()
```
I have tried several ways to get the dagger symbol to print, but with each effort, the outcome is that the code for the dagger symbol is simply printed as text or raises an error (with the backslash). I have tried using bquote, \dagger, \dagger, etc. I also tried to use a superscript "a" as an alternative. Again, kbl just prints the literal code text without substituting the required symbol. There must be a straight-forward way to insert special characters or math strings into a kable header, but I have been unable to find it.
Thanks in advance for any help with this issue.
Larry Hunsicker
You can directly add a Unicode escape:
df <- data.frame(x = 1:10)
names(df)[1] <- 'maxCpep (ng/mL) \u2020'
df %>% kbl()
Use intToUtf8 to get the special character.
names(df)[1] <- paste('maxCpep (ng/mL)', intToUtf8(8224))
The table prints nicely in markdown but is not present in the knitted html file. I noticed that it is classified as a list but don't know how to change it to an acceptable file type. The knitted output is not formatted as a table. I appreciate the help.
library("crosstable") #important package crosstable() function
library('dplyr')
library("flextable")
tbl1 = crosstable(mtcars2, c(1), by = 2) %>%
as_flextable(keep_id=FALSE)
print(tbl1)
According to ?print.flextable
Note also that a print method is used when flextable are used within R markdown documents. See knit_print.flextable().
Therefore, if we want to print in Rmarkdown, either use knitr::knit_print or remove the print as the ?knit_print.flextable documentation shows
You should not call this method directly. This function is used by the knitr package to automatically display a flextable in an "R Markdown" document from a chunk.
---
title: "Testing"
author: "akrun"
date: "09/12/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, echo = FALSE}
suppressPackageStartupMessages(library("crosstable")) #important package crosstable() function
suppressPackageStartupMessages(library('dplyr'))
suppressPackageStartupMessages(library("flextable"))
tbl1 = crosstable(mtcars2, c(1), by = 2) %>%
as_flextable(keep_id=FALSE)
# either use knit_print or remove the print wrapper
#knitr::knit_print(tbl1)
tbl1
```
-output
I am working on an R Markdown document that can be downloaded from a Shiny App as a pdf. I have structured my 1-page document with a layout that presents two columns at the beginning and then again one column until the end.
The main issue is that in one of the two columns I can't insert a table generated, for example, through the kable() function of the knitr package, because I get the following error:
Package longtable Error: longtable not in 1-column mode
Below you can find some reproducible code:
---
output: pdf_document
header-includes:
- \usepackage{multicol}
- \newcommand{\btwocol}{\begin{multicols}{2}}
- \newcommand{\etwocol}{\end{multicols}}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = T)
library(ggplot2)
library(knitr)
```
\btwocol
#### Column one
```{r}
ggplot(mtcars,
aes(x = mpg,
y = cyl)) +
geom_point()
```
\columnbreak
#### Column two
```{r}
kable(mtcars[1:10, 1:2], format = "markdown")
```
\etwocol
Note that if you remove the kable() function the script can be compiled but the table is in the standard "R" format.
Thanks!
Very simple solution: use from the kableExtra library kbl(df, longtable=F) to produce a table that is not of the longtable format.
I found a solution by using the functionalities of the grid, gridExtra and gtable package. I created and customised the table and its caption directly inside a chunk of the R Markdown file, and with the grid.draw() function I created a graphical object of that table that can now be inserted inside a two-columns layout.