I would like to use the < symbol in a table caption of a Rmarkdown that converts to a docx document. I am using the flextable package as this gives a lot of (needed) flexibility to tables in the docx format.
But I am really confused by the multiple conversion steps through pandoc. It does not seem so easy to get to the < as it is a special coding HTML character. I have read that in HTML you would escape it via <. This gives me the problem that the & has to be escaped, too. The conversion then turns < into < (as it converts the & into &) and \\< would yield me &lt; (as it converts the & of the & again). Latex does not seem to work either, I've tried <, $<$ and $\\textless$ but to no avail.
All combinations basically follow the same logic, i.e. that < are correctly transformed to < but then the HTML is not converted again.
Any idea how to solve this? What do I miss?
Example RMD file:
---
title: "Untitled"
author: "Unkown"
date: "1/25/2021"
output: bookdown::word_document2
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(flextable)
library(tidyverse)
```
## R Markdown
This is an R Markdown document, see Table \#ref(tab:test).
```{r test, echo = F}
flextable(head(cars, n = 10)) %>%
bold(part = "header") %>%
autofit() %>%
set_caption("Table: (\\#tab:test) Example caption with less-than symbol: \\< or < or < or $<$ or $\\textless$")
```
This should answer the question about < and > in the captions
---
title: "Untitled"
output: bookdown::word_document2
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(flextable)
library(tidyverse)
```
## R Markdown
This is an R Markdown document, see Table \#ref(tab:test).
```{r test, echo = F, tab.id="test"}
flextable(head(cars, n = 10)) %>%
bold(part = "header") %>%
autofit() %>%
set_caption("Example caption with less-than symbol: > and <")
```
You could use the package officedown. It will make the references as real Word references, it also offers few features to customize your captions:
---
output:
bookdown::markdown_document2:
base_format: officedown::rdocx_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, tab.cap.style="Table Caption")
library(flextable)
library(tidyverse)
```
```{r test1}
flextable(head(cars, n = 10)) %>%
bold(part = "header") %>%
autofit() %>%
set_caption("Example caption with less-than symbol: > and <")
```
```{r "test2", tab.cap="Example caption with less-than symbol: > & <"}
flextable(head(cars, n = 10)) %>%
bold(part = "header") %>%
autofit()
```
\newpage
See \#ref(tab:test1).
See \#ref(tab:test2).
Related
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.
I am having a problem with my ztable,
I want to knit my Rmarkdown file to HTML but I cannot find a way to display the table I created with ztable:
z=ztable(loucaste) %>% makeHeatmap(palette = "Blues") %>% print(caption="Table 2.)
I tried to set
options(ztable.table="html")
and put this at the beginning as I read somewhere else
output: html_document
header-includes: \usepackage{colortbl}
but it doesn't work when I knit to HTML. My intention was to create a sort of formatting table similar to the ones made on excel and ztable looks like the only way.
Try this minimal Rmd. The key seems to be options(ztable.type = "html") and in the chunk that generates the table, r results='asis'
If that works for you, substitute your code in the appropriate place i.e. ztable(loucaste) %>% makeHeatmap(palette = "Blues") %>% print(caption="Table 2.).
---
title: "ztable"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ztable)
library(magrittr)
options(ztable.type = "html")
```
## R Markdown
```{r results='asis'}
matrix(1:100, nrow = 10) %>%
as.data.frame() %>%
ztable() %>%
makeHeatmap() %>%
print(caption = "table 2")
```
I am trying to set up a knitr::knit_hooks() to automatically format data frame output of an R-markdown chunk with kableExtra in my HTML report.
I would like to not repeatedly add the following lines (or any lines) to the end of each chunk of tabulated data:
head(iris) %>%
kable("html") %>%
kable_styling("hover", full_width = FALSE)
I came up with one solution based on this answer that works by evaluating the chunk source (see my answer below that includes some issues that I have with this approach); I'm hoping there might be a better solution using the chunk output.
Here is an example .Rmd with an outline of what I would like to achieve.
---
title: "Untitled"
author: "Paul"
date: "25 September 2018"
output: html_document
---
```{r setup, include = F}
library(dplyr)
library(kableExtra)
library(knitr)
data(iris)
default_source_hook <- knit_hooks$get('source')
knit_hooks$set(
output = function(x, options) {
x %>%
kable("html") %>%
kable_styling("hover", full_width = FALSE)
},
source = function(x, options) {
if(is.null(options$table))
default_source_hook(x, options)
else {
eval(parse(text = x)) %>%
kable("html") %>%
kable_styling("hover", full_width = F)
}}
)
```
Desired chunk input:
```{r test, echo = F}
head(iris)
```
Desired output will look like:
```{r output, echo = F}
head(iris) %>%
kable("html") %>%
kable_styling("hover", full_width = FALSE)
```
Solution using the source chunk output:
```{r table_format, results = "hide", table = T, eval = F}
head(iris)
```
Thank you.
If using knit hooks is not necessary, the following might help. The idea is to just define a function that prints whatever it gets exactly the way you want. This does not eliminate all typing, but reduces it substantially.
---
title: "Untitled"
author: "Paul"
date: "25 September 2018"
output: html_document
---
```{r setup, include = F}
library(dplyr)
library(kableExtra)
library(knitr)
tbl_out <- function(data) {
data %>% kable("html") %>% kable_styling("hover", full_width = FALSE)
}
```
Prints as desired:
```{r test, echo = F}
head(iris) %>% tbl_out()
```
Output:
I found a work-around by using the knit_hooks() source as the formatted chunk output.
The difficulty in formatting the chunk output is that character data is passed to the hook. Drawing inspiration from the linked answer in the OP, I found a solution by evaluating the chunk source in knit_hooks().
This solution re-evaluates the chunk and is piped into the desired KableExtra formatting.
I needed to add a new chunk option table = T so that the formatting was only applied to source code that produces a data table; results = "hide" was also needed so the default chunk output was not included in the report.
---
title: "Untitled"
author: "Paul"
date: "27 September 2018"
output: html_document
---
```{r setup, include = F}
library(dplyr)
library(ggplot2)
library(kableExtra)
library(knitr)
default_source_hook <- knit_hooks$get('source')
knit_hooks$set(
source = function(x, options) {
if(is.null(options$table))
default_source_hook(x, options)
else {
eval(parse(text = x)) %>%
kable("html") %>%
kable_styling("hover", full_width = F)
}
}
)
data(iris)
```
## Normal
With no chunk options:
```{r normal}
head(iris)
```
## The desired ouptut
With chunk options `results = "hide"` and `table = T`:
```{r table_format, results = "hide", table = T, eval = F}
head(iris)
```
## It still work as normal for other other output types
With no chunk options:
```{r image}
iris %>%
ggplot(aes(x = Sepal.Length, y = Sepal.Width, group = Species)) +
geom_point()
```
There are a few problems with this solution
the code used to produce the table is now unavailable
eval = F should be included - especially if the code will take a while to run (as it is again evaluated in the hook)
I'm replacing 2 lines of code with 3 chunk options - although not a deal killer it's not quite as automagical as I would have liked.
Consider this simple example:
---
title: "Untitled"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Slide with R Output
```{r t, warning=FALSE, message=FALSE}
library(knitr)
library(kableExtra)
library(dplyr)
for(threshold in c(20, 25)) {
cars %>%
filter(dist < threshold) %>%
kable('html') %>%
kable_styling(bootstrap_options = "striped")
}
```
Here I simply want to print each output of the for loop into a different slide. In this example, there are two calls to kablethat should go on two different slides.
The code above does not work. Am I even using the right packages for that? Any ideas?
Thanks!
You can use the asis option:
---
title: "Untitled"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)
library(dplyr)
# needed so r will include javascript/css dependencies needed for striped tables:
kable(cars, "html") %>% kable_styling(bootstrap_options = "striped")
```
```{r, results = "asis"}
for (threshold in c(20, 25)) {
cat("\n\n##\n\n")
x <- cars %>%
filter(dist < threshold) %>%
kable('html') %>%
kable_styling(bootstrap_options = "striped")
cat(x)
}
```
To get rid of that bogus table, you can try to put options(kableExtra.html.bsTable = T) in your setup section.
Here's the start of a solution. You can print strings with markdown, either by making the strings yourself or using pander's pandoc.* functions. If you set results="asis" for that chunk, it will get compiled the same as any other markdown. I used cat to make the ## headings, but commented out two pander functions that you could try also to make headers or horizontal rules to split slides.
There's more detail on the pander functions here, plus other SO questions such as this one.
---
title: "Untitled"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)
library(dplyr)
```
```{r, results='asis'}
for(threshold in c(20, 25)) {
# pander::pandoc.header(sprintf("Threshold = %s", threshold))
# pander::pandoc.horizontal.rule()
cat(paste("\n##", "Threshold =", threshold), "\n")
tbl <- cars %>%
filter(dist < threshold) %>%
kable(format = "html") %>%
kable_styling(bootstrap_options = "striped")
print(tbl)
}
```
One issue is that when I knit this, I'm not getting the striped table that you'd expect. If I add a slide before this chunk and put a table in it with these kableExtra settings, I do get stripes, but the first table is also pretty ugly...I'm not sure if that's a bug or conflicting CSS somewhere or what.
I am attempting to output a latex table using r markdown, kable and kableExtra. When i use the option row.names=FALSE instead of row.names=TRUE the latex code generates \vphantom code which produce an error to create the pdf .
It seems the problem is linked to the row_spec option.
Here is Rmarkdown code (.Rmd File):
---
title: "Test"
output:
pdf_document:
fig_caption: true
keep_tex: true
---
{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
{r}
library(knitr)
library(kableExtra)
temp <- mtcars[1:5,1:5]
kable(temp, format = "latex", booktabs = F,row.names=F) %>%
kable_styling(position = "center") %>%
row_spec(1, bold = T, background = "red")
The error is :
! Forbidden control sequence found while scanning use of
\check#nocorr#.
\par l.105 ...color{red} \textbf{21.0 &\vphantom{1} 6}
& \textbf{160} & \textbf{...
Do you have any issue of what is happening ?
This is caused by the duplicated rows in the dataframe, as both rows 1 and 2 are the same.
Reviewing the code for row_spec_latex, when kableExtra is used against a kable table, it checks for duplicated rows. If it finds one, it inserts the vphantom argument within the fix_duplicated_rows_latex internal function. This vphantom insertion is then messing up the formatting of the textbf function.
This seems like a slight bug, so it may be worth reporting it as an issue in kableExtra: https://github.com/haozhu233/kableExtra . I am sure that the vphantom is added for a good reason though, but doubt this was an intended consequence.
Supporting code:
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)
temp <- mtcars[1:5,1:5]
```
```{r}
# Keeping the row names (means all rows are unique)
kable(temp, format = "latex", booktabs = F) %>%
kable_styling(position = "center") %>%
row_spec(1, bold = T, color = "red")
```
```{r}
# Highlighting second row (which doesn't have the vphantom statement)
kable(temp, format = "latex", booktabs = F, row.names=F) %>%
kable_styling(position = "center") %>%
row_spec(2, bold = T, color = "red")
```