I want to use bold and italics in a flextable caption. I know that it is possible to apply font syles to text within the header or body of a flextable using the ftExtra package and one can choose different output styles for a caption in word output by officeR. But either these do not work with the caption (ftExtra) or do not provide a style that allows selective application of bold and italics.
Here is some code with the intended markdown in the caption:
library(flextable)
library(dplyr)
iris[1:10,] %>%
flextable() %>%
set_caption(caption = "**Tab. 1:** First ten (*n* = 10) samples of the Iris species dataset")
The caption should read as "Tab. 1: First ten (n = 10) samples of the Iris species dataset"
UPDATE
based on the comment by dario a possible solution would be:
library(flextable)
library(dplyr)
iris[1:10,] %>%
flextable() %>%
add_header_lines("") %>%
compose(
i = 1, part = "header",
value = as_paragraph(
as_chunk("Table 1: ", props = fp_text(bold = TRUE)),
as_chunk("First ten ("),
as_chunk("n", props = fp_text(italic = TRUE)),
as_chunk(" = 10) samples of the Iris species dataset")
)
)
Thank you very much. In the end I need to produce about 20 tables in different chapters and so I will try the suggestion by David to build the captions during knitting a markdown. But it is good to know that there is way, although clunky and not flexible using compose.
I prepared a markdown template for you:
---
title: "Hello World"
header-includes:
- \usepackage{caption}
output:
pdf_document:
latex_engine: xelatex
---
```{r, include = FALSE}
library(flextable)
library(dplyr)
table2 <- flextable(mtcars[1:5, 1:6])
```
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
\begin{table}[hbtp]
\captionof{table}{Text of the caption.}
`r table2`
\end{table}
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
An output
Related
Using quarto's HMTL-output functionalities, I am trying to produce a kable from a data.frame that contains some Markdown-style formatting that should show up in the final document. In the actual use case, I have a number of documents already formatted this way and I would like re-use these commands for correctly rendering the output.
Here's my example.qmd:
---
title: "example"
format:
html
---
```{r setup}
library(kableExtra)
```
```{r}
#| echo: false
data.frame(Function = "`read_delim()`",
Formula = "$\\leftarrow$",
Break = "this continues on a<br>new line",
Link = "[Google](www.google.com)") |>
kbl(format = "html")
```
After running the chunk, the preview in RStudio does display the arrow and line break correctly, but ` ` and the link fail to have an effect:
When rendering the qmd to HTML, the result looks like this, i.e. ignores the formatting:
What am I missing? Is there a way to include such formatting commands into a kable when rendering a quarto document to HTML?
When creating a table in Quarto, you can't mix Markdown with HTML - the Markdown syntax won't be processed within the HTML table.
This R code would work
data.frame(Function = "`read_delim()`",
Formula = "$\\leftarrow$",
Break = "this continues on a<br>new line",
Link = "[Google](www.google.com)") |>
kbl(format = "markdown")
So if you can, output only Markdown table which knitr::kable() should do by default.
If you need to output a HTML table (e.g for specific HTML features), you need to use a framework that will render the markdown for you while creating the HTML table.
gt with fmt_markdown() and md()
flextable with ftextra and colformat_md() or as_paragraph_md
This is possible that this limitation of note being able to include raw Markdown inside HTML table will be improve in the future (https://github.com/quarto-dev/quarto-cli/discussions/957#discussioncomment-2807907)
I'm attempting to add a table into an RMarkdown Powerpoint Presentation using the kableExtra package. Initially, I tried to run the code without always_use_html in my YAML and the following error appeared.
Error: Functions that produce HTML output found in document targeting pptx 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.
Execution halted
After adding always_allow_html to my YAML, my table is still not appearing as desired in my slide .
My table should look something like this
Does anyone perhaps know how to embed a kableExtra table in Rmarkdown slides?
Here is my full code
---
title: "Untitled"
output: powerpoint_presentation
always_use_html: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Table
```{r table}
library(kableExtra)
data <- data.frame(a = c(1,2,3), b = c(4,5,6))
data %>%
kable() %>%
kable_styling()
```
Try kbl() instead of kable():
library(kableExtra)
data <- data.frame(a = c(1,2,3), b = c(4,5,6))
data %>%
kbl() %>%
kable_styling()
kable() renders to html in the viewer pane when run live, and can knit to html.
Office docs don't handle the html.
Options
include, pass a table to the default powerpoint style for your document with .rmd.
See:https://support.rstudio.com/hc/en-us/articles/360004672913-Rendering-PowerPoint-Presentations-with-the-RStudio-IDE
use officer with officemarkdown
Officer bookdown summary
-advanced styling to build a custom template can be done for reproducible
-you can save to powerpoint, then use table style editing to tweak, vs. setting the styling with style_type, style_id,style_name parameters in officedown
I am using huxtable to display tables in RMarkdown for output: pdf_document.
When I put a label in the table
huxtable(mytable) %>%
set_label("tab:mylabel")
and then make a reference in the body text like
See Table \#ref(tab:mylabel) for more details
It displays "See Table #ref(tab:mylabel) for more details" rather than giving the table number.
This was covered for Bookdown in
Huxtable package for R: How to correctly reference huxtables in bookdown?
Do I need some special option or other setting to make it work for a regular PDF document?
Seems so. The rmarkdown guide says you will need "a bookdown format" to use cross-referencing in that way. However, there's an alternative: use embedded TeX, rather than this rmarkdown format. A MWE:
```{r}
hux("A huxtable") %>%
set_label("tab:foo") %>%
set_caption("You'll need a caption too")
```
Here's a reference to \ref{tab:foo}.
I am writing a Rmarkdown document using bookdown::pdf_document2 for which I have a table that needs to do several very specific things, but I simply cannot find the solution to get all of them to work at the same time:
The table needs to float to the end of the document as it would with the latex endfloat package.
The table elements have Markdown bold and italics formatting that should appear correctly in the final PDF document
One of the columns of the table has a lot of text that I would like to wrap within the table cell.
I have tried to get these three options to work with all sorts of combinations of knitr::kable, kableExtra::column_spec and pander, but I cannot find a way to get them all going at once. Below I am pasting an example Rmarkdown document with various tests, none of which fully works.
Is there a simple way to get the table to do what I want? Even a good workaround would be acceptable...
Thanks,
David
---
title: "Test table formatting"
author: "David M. Kaplan"
date: "5/24/2020"
output: bookdown::pdf_document2
header-includes:
- \usepackage[tablesfirst]{endfloat}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
# Data
```{r}
df.long = data.frame(
char = c('*this is some very long text with lots of words that will cause problems with word wrap if it is not properly handled with something like kableExtra*','**b**','~~c~~'),
num = c(1,2,3))
```
# Kable with format=pandoc
```{r}
knitr::kable(df.long,caption="test1",format="pandoc")
```
**Result:** Handles formatting, but table does not float and no wordwrap.
# Kable with booktab
```{r}
knitr::kable(df.long,caption="test2",booktab=TRUE)
```
**Result:** Floats, but does not handle formatting or do wordwrap.
# kableExtra for wordwrap
```{r}
knitr::kable(df.long,caption="test3",booktab=TRUE) %>%
kableExtra::column_spec(1,width="30em")
```
**Result:** Table floats and has wordwrap, but does not handle formatting.
# Pander
```{r}
pander::panderOptions("table.alignment.default","left")
pander::pander(df.long,caption="test4")
```
**Result:** Wordwrap and formatting, but does not float.
I found a solution to this problem. It basically consists of:
In the YAML header, add a header-includes entry declaring longtable to be a float flavor using some LaTeX magic I found here.
Use pander to format the table to get wordwrap
Specifically, I have the following in the YAML header:
header-includes:
- \usepackage[tablesfirst]{endfloat}
- \DeclareDelayedFloatFlavour*{longtable}{table}
and then the table can be generated with pander:
pander::panderOptions("table.alignment.default","left")
pander::pander(df.long,caption="test pander",split.cell=50)
I am making some slides inside Rstudio following instructions here:
http://rmarkdown.rstudio.com/beamer_presentation_format.html
How do I define text size, colors, and "flow" following numbers into two columns?
```{r,results='asis', echo=FALSE}
rd <- sample(x=1e6:1e7, size = 10, replace = FALSE)
cat(rd, sep = "\n")
```
Output is either HTML (ioslides) or PDF (Beamer)
Update:
Currently the code above will only give something like the following
6683209
1268680
8412827
9688104
6958695
9655315
3255629
8754025
3775265
2810182
I can't do anything to change text size, color or put them into a table. The output of R codechunk is just plain text. Maybe it is possible to put them in a table indeed, as mentioned at this post:
http://tex.aspcode.net/view/635399273629833626273734/dynamically-format-labelscolumns-of-a-latex-table-generated-in-rknitrxtable
But I don't know about text size and color.
Update 2:
The idea weaving native HTML code to R output is very useful. I haven't thought of that. This however only works if I want to output HTML. For PDF output, I have to weave the native Latex code with R output. For example, the code following works using "knitr PDF" output:
```{r,results='asis', echo=FALSE}
cat("\\textcolor{blue}{")
rd <- sample(x=1e6:1e7, size = 10, replace = FALSE)
for (n in rd) {
cat(paste0(n, '\\newline \n')) }
cat("}")
```
You are using results='asis', hence, you can simply use print() and formatting markup. If you want your text to be red, simply do:
```{r,results='asis', echo=FALSE}
print("<div class='red2'>")
rd <- sample(x=1e6:1e7, size = 10, replace = FALSE)
cat(rd, sep = "\n")
print("</div>")
```
Hope it helps.
It sounds as if you want the output to be either PDF or HTML.
One possibility is the xtable package. It produces tables either in PDF or HTML format. There's no (output-independent) way to specify colour, however. Here's an example.
xt <- xtable(data.frame(a=1:10))
print(xt, type="html")
print(xt) # Latex default
Another option is the pandoc.table function from the pander package. You need the pandoc binary installed. If you have RStudio, you have this already. The function spits out some markdown which then can be converted to HTML or PDF by pandoc.
Here's how you could use this from RStudio. Create an RMarkdown document like this:
---
title: "Untitled"
author: "You"
date: "20 November 2014"
output: html_document
---
```{r, results='asis'}
library(pander)
tmp <- data.frame(a=1:10,b=1:10)
pandoc.table(tmp)
```
When you click "knit HTML", it will spit out a nice HTML document. If you change output to pdf_document, it will spit out a nice PDF. You can edit the options to change output - e.g.
pandoc.table(tmp, emphasize.strong.rows=c(2,4,6,8,10))
and this will work both in PDF or HTML. (Still no options to change colour though. Homework task: fix pandoc.table to allow arbitrary colours.)
Under the hood, knitr is writing markdown, and pandoc is converting the markdown to whatever you like.