I am trying to create a table to hold some basic information using Kable in RMarkdown that will be generated in HTML, PDF, and Word. Here is the code I have
---
title: "test"
author: ''
date: "2015/03/24"
output:
pdf_document:
keep_tex: yes
---
```{r kable1, echo=FALSE}
Variable <- c("VAR1", "VAR2", "VAR3", "VAR4")
Label <- c("LABEL", "A very loooooooooooooooooooooooooooooooooooooooooooo ooooooooooooooooooooooooooooooooooooooo nnnnnnngggggggg label yall", "LAB3", "LAB4")
Classification <- c("Type1", "Type2", "Type1", "Type1")
data <- data.frame(Variable, Label, Classification)
library(knitr)
kable(data)
```
```{r kable2, echo=FALSE}
Variable <- c("VAR1", "VAR2", "VAR3", "VAR4")
Label <- c("LABEL", "LabLE", "LAB3", "LAB4")
Classification <- c("Type1", "Type2", "Type1", "Type1")
data <- data.frame(Variable, Label, Classification)
library(knitr)
kable(data)
```
The HTML output is as follows.
This is what I want. I like how the table fills out the html. However, when I produce PDF I get the following.
As we can see there are issues with the PDF, the first table runs off the page and the second does not fill up the entire width. I am, unfortunately, a n00b when it comes to R, Kable, and RMarkdown. Is there a way to set options on the kable table so that the PDF looks as nice as the HTML in terms of page placement and width? Thanks!
LaTeX will not break the long lines inside of a table cell for you -- or you should use a custom environment for that, which can be sometimes really frustrating when writing markdown to be processed by Pandoc. That's why I came up with the idea to break long lines inside of the cells before transforming to markdown via the pander package. Quick example:
> pander(data, split.cells = 30, split.table = Inf)
-------------------------------------------------------------------------
Variable Label Classification
---------- --------------------------------------------- ----------------
VAR1 LABEL Type1
VAR2 A very Type2
loooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooo
nnnnnnngggggggg label yall
VAR3 LAB3 Type1
VAR4 LAB4 Type1
-------------------------------------------------------------------------
Or if you will have several tables with the same problem, specify the split value once:
> panderOptions('table.split.table', Inf)
> pander(data)
-------------------------------------------------------------------------
Variable Label Classification
---------- --------------------------------------------- ----------------
VAR1 LABEL Type1
VAR2 A very Type2
loooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooo
nnnnnnngggggggg label yall
VAR3 LAB3 Type1
VAR4 LAB4 Type1
-------------------------------------------------------------------------
In short, look for the table.split.table and table.split.cells global options, although there are a bunch of other useful tweaks as well.
Related
I am writing my thesis using a Quarto book in HTML, which has some dynamic content (leaflet maps, plotly dynamic graphs). However, eventually, I will need to export the book in PDF/LaTeX, or at least Word (and then I can copy and paste into LaTeX).
When I try to export to PDF I of course run into this error:
Functions that produce HTML output found in document targeting pdf
output. Please change the output type of this document to HTML.
Alternatively, you can allow HTML output in non-HTML formats by adding
this option to the YAML front-matter of your rmarkdown file:
always_allow_html: true
Note however that the HTML output will not be visible in non-HTML
formats.
I did try to add the always_allow_html: true in my YAML file, but I get the same exact error. I also tried the conditional rendering with {.content-hidden unless-format="pdf"}, but I can't seem to get it working.
Has anyone experienced the same issue?
Using .content-visible when-format="html" and .content-visible when-format="pdf" works very smoothly.
---
title: "Conditional Rendering"
format:
html: default
pdf: default
---
## Conditional Content in Quarto
::: {.content-visible when-format="html"}
```{r}
#| message: false
library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))
p <- p + geom_point(aes(colour = factor(cyl)))
ggplotly(p)
```
```{r}
#| message: false
#| fig-pos: "H"
#| fig-width: 4
#| fig-height: 3
library(leaflet)
# took this example from leaflet docs
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m # Print the map
```
:::
::: {.content-visible when-format="pdf"}
```{r}
library(plotly)
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))
p <- p + geom_point(aes(colour = factor(cyl)))
p
```
:::
I use constructs like below
p <- ggplot()
if (interactive() || opts_knit$get("rmarkdown.pandoc.to") == "html") {
ggplotly(p)
} else {
p
}
Stumbled across this one too. I'm currently checking the output format of pandoc globally
```{r, echo = F}
output <- knitr::opts_knit$get("rmarkdown.pandoc.to")
```
and then evaluate chunks conditionally:
(leaflet example from here.)
```{r, echo = F, eval = output != "latex"}
library(leaflet)
leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
```
This is optional if you want a note on a missing component in the PDF version:
```{r, echo = F, eval = output == "latex", results = "asis"}
cat("\\textit{Please see the HTML version for interactive content.}")
```
Edit
I just checked, this also works with Quarto documents for me using the below YAML header.
---
title: "Untitled"
format:
html:
theme: cosmo
pdf:
documentclass: scrreprt
---
I am trying to nut out how to use a company word document as a rdocx_doxument template for R markdown using the officedown package.
---
title: Company Report Title
author: Ross
date: 3/3/2022
output:
officedown::rdocx_document: default
---
# Introduction
Identifying default table and figure caption styles
```{r plot, fig.cap="My plot caption"}
plot(cars)
```
# Table
```{r, message = FALSE, warning=FALSE}
library(flextable)
cars |> head(6) |> flextable() |>
set_caption("Caption for table")
```
The style from this example gives an 'Image Caption' style for the plot and a 'Table Caption' style for the table.
Now try using the company reference template
---
title: Company Report Title
subtitle: Report subtitle
author: Ross
date: 3/3/2022
output:
officedown::rdocx_document:
reference_docx: company_report_template.docx
mapstyles:
Image Caption: ['Caption']
Table Caption: ['Caption']
Title: ['CoverTitle']
---
# Introduction
How to use mapstyles to avoid redefining company word template styles.
```{r plot, fig.cap="My plot caption"}
plot(cars)
```
# Table
```{r, message = FALSE, warning=FALSE}
library(flextable)
cars |> head(6) |> flextable() |>
set_caption("Caption for table")
```
In the word template file there is a style defined for both figure and table captions called 'Caption' and a style called 'CoverTitle' for the Title.
The rendering error message is
Error: could not find style "Caption". In addition: Warning message:
paragraph style for plots "Figure" has not been found in the
reference_docx document. Style 'Normal' will be used instead.
Execution halted
The template file does have a 'Caption' style defined.
Wondered if someone can help me fathom this out?
I am very reluctant to change the styles in the company word template file.
Thanks
Screenshot of rmarkdown header, render error and word template file showing style
I'm using RMarkdown to create Supplementary documents for a paper. These supplements contain many tables. Let's call these documents Supplement A and Supplement B. I want the table numbering to reflect the supplement letter, that is, Table A1 or Table 1A for the first table in Supplement A and so on for all tables.
How can I modify the table numbering to add a letter into the table numbering schema?
Here's an example that will produce a table with normal numbering:
---
title: "Supplement A"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)
```
```{r cars}
kable(mtcars[1:5, ], booktabs=TRUE, caption="MTCars") %>%
kable_styling(latex_options="hold_position", position="left")
```
An inelegant solution using the captioner package (details provided here: https://datascienceplus.com/r-markdown-how-to-number-and-reference-tables/) can create captions and insert them manually just before the code chunk. Combining this with the Latex package caption makes it possible to remove the automatic table naming and numbering if the caption is generated within the code chunk (How to suppress automatic table name and number in an .Rmd file using xtable or knitr::kable?). I've done this in the YAML with \captionsetup[table]{labelformat=empty}, but it can be done in the document body also. Either way, the caption can then be generated within the code chunk, which was important in my case.
This solution stops bookdown referencing from working (because labelformat cannot be empty), but a workaround for table referencing was provided in the link for using the captioner package (and is included below).
---
title: "Supplement A"
header-includes:
- \usepackage{caption}
\captionsetup[table]{labelformat=empty}
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)
library(captioner)
library(stringr)
```
```{r captions}
# Create the caption(s)
caption <- captioner("Table A", FALSE)
tab_1_cap <- caption("Tab_1", "MTCars")
# Create a function for referring to the tables in text
ref <- function(x) str_extract(x, "[^:]*")
```
The following table is `r ref(tab_1_cap)`.
```{r cars}
kable(mtcars[1:5, ], booktabs=TRUE, caption=tab_1_cap) %>%
kable_styling(latex_options="hold_position", position="left")
```
An alternative solution is avaiable via LaTeX. Add the following somewhere in the body of the document to change the default figure and table names to include the letter.
\def\figurename{Figure A}
\def\tablename{Table A}
To reference a table or figure in text use, e.g., Table A\#ref(tab:label), after defining the table label as normal.
This alone will leave a space in the table/figure caption (e.g., 'Table A 1' instead of 'Table A1'). This can be solved by adding the following to the YAML:
header-includes:
- \usepackage{caption}
- \DeclareCaptionLabelFormat{nospace}{#1#2}
- \captionsetup[figure]{labelformat=nospace}
- \captionsetup[table]{labelformat=nospace}
The \DeclareCaptionLabelFormat creates a function (here labeled nospace), which overrides the default caption label when called. #1 represents the label assigned to refer to tables or figures (e.g., 'Table A') and #2 represents the number of the table or figure. The captionsetup lines change the label format for all tables and figures to the format defined by 'nospace'.
Thus, the code produces, e.g., 'Table A1: Table name' as the caption. If instead you wanted, e.g., 'Table A-1: Table name', the code in \DeclareCaptionLabelFormat should be changed to {#1-#2}.
---
title: "Supplement A"
output: bookdown::pdf_document2
toc: false
header-includes:
- \usepackage{caption}
- \DeclareCaptionLabelFormat{nospace}{#1#2}
- \captionsetup[figure]{labelformat=nospace}
- \captionsetup[table]{labelformat=nospace}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)
library(bookdown)
```
\def\figurename{Figure A}
\def\tablename{Table A}
See Table A\#ref(tab:cars) for something about cars.
(ref:cars) Table caption
```{r cars}
kable(mtcars[1:5, ], booktabs=TRUE, caption="(ref:cars)") %>%
kable_styling(latex_options="hold_position", position="left")
```
Answer inspired by: Figure name in caption using RMarkdown
Caption latex package documentation: https://ctan.org/pkg/caption
Is there a way to automatically number the figures in captions when I knit R Markdown document into an HTML format?
What I would like is for my output to be the following:
Figure 1: Here is my caption for this amazing graph.
However, I am currently getting the following:
Here is my caption for this amazing graph.
Here is my MWE:
---
title: "My title"
author: "Me"
output:
html_document:
number_sections: TRUE
fig_caption: TRUE
---
```{r setup}
knitr::opts_chunk$set(echo=FALSE)
```
```{r plot1,fig.cap="Here is my caption for this amazing graph."}
x <- 1:10
y <- rnorm(10)
plot(x,y)
```
```{r table1, fig.cap="Here is my caption for an amazing table."}
head(mtcars, 2)
```
I have read that this issue is resolved with Bookdown but I've read the Definitive Guide to Bookdown, cover to cover, and can't find it.
If you wish to have numbered figures, you will need to use an output format provided by bookdown. These include html_document2, pdf_document2 etc. See here for a more comprehensive list of options.
Changing your document example html_document to bookdown::html_document2 will resolve your problem.
---
title: "My title"
author: "Me"
output:
bookdown::html_document2:
number_sections: TRUE
fig_caption: TRUE
---
```{r setup}
knitr::opts_chunk$set(echo=FALSE)
```
```{r plot1,fig.cap="Here is my caption for this amazing graph."}
x <- 1:10
y <- rnorm(10)
plot(x,y)
```
```{r plot2, fig.cap="Here is my caption for another amazing graph."}
plot(y,x)
```
If you want to label tables created by knitr::kable, you will need to specify the caption within the table call itself
```{r table1}
knitr::kable(mtcars[1:5, 1:5], caption = "Here is an amazing table")
```
I would like to show, in an rmarkdown document, the printed output of a tibble as shown in the console. i.e.
First line: dimension summary;
then the column headers;
then classes of variables;
then first 10 lines of the tibble;
then number of rows not shown.
However, by default, tibbles and other data.frame objects are automatically converted to a tabbed, paginated table showing all contents.
I note that the option to turn off this default behaviour can be set for a whole markdown document by changing the df_print option to tibble in the YAML.
However, how do I set the df_print option to tibble for just a single R code chunk, so that I can show in an Rmarkdown document what the user will see on the console?
Thanks,
Jon
There's no need to touch the global setting. You can explicitly call the print function tibble:::print.tbl_df(df)
Example:
title: "Untitled"
author: "TC"
date: "7/27/2018"
output:
html_document:
df_print: "kable"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## print tibble
```{r}
df <- tibble::as.tibble(mtcars)
tibble:::print.tbl_df(head(df))
```
## print kable (set in YAML header)
```{r}
head(df)
```
output html