Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture 'output_format' from rmarkdown::render as variable

I am using RStudio with knitr, etc, to make reproducible reports, and want to have the best versions I can for both Word documents and PDF - I prefer working with LaTeX, but the end-users tend to prefer the flexibility of editable Word documents.

I have written a ifelse statement that essentially says 'if this is rendered as a word document, create a kable table in markdown, else create the kable table in LaTeX and then manipulate to make the table look better (shaded rows, etc)'.

I don't understand how the rmarkdown::rendering process works to capture the output_format, but is there a way to store this as a variable and use in the ifelse statement?

A minimal example would be to save this code as test.Rmd:

format <- output_format #(somehow captured as a variable)

printTable <- function(data = df, format = format){

if (format %in% 'pdf_document') {

    # create nice latex table

} else {

    # create markdown table

}
}

Then, when running this code:

rmarkdown::render(input = "test.Rmd", output_format = c("word_document", "pdf_document"))

the different versions of the report would have the correct tables included.

like image 613
Jonny Avatar asked Apr 26 '15 18:04

Jonny


People also ask

How do I show output in Rmarkdown?

If you prefer to use the console by default for all your R Markdown documents (restoring the behavior in previous versions of RStudio), you can make Chunk Output in Console the default: Tools -> Options -> R Markdown -> Show output inline for all R Markdown documents .

How do I export HTML from Rmarkdown?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

How do I save a plot from Rmarkdown?

You want to make sure that you save images you create in your rmarkdown document. To do this automatically, and avoid writing things like ggsave(plot) or dev. off() , just create the plots in rmarkdown, and tell it you want to save the output. In the funky looking code at the top, the YAML, specify keep_md: yes .

How do I use parameters in Rmarkdown?

Parameters in an R Markdown document are very simple to use. In the yaml header (the section at the top of the markdown document), you just have to add a couple new lines for your parameters to hardcode them in. Once they're coded in, they will be available in the params object for use in the rest of the analysis.


1 Answers

You can access the output format via knitr::opts_knit$get("rmarkdown.pandoc.to"). This will return a string with the target output format. Here's an example:

---
title: "Untitled"
output: html_document
---

```{r}
library(knitr)
opts_knit$get("rmarkdown.pandoc.to")
```

This returns "html" for html_document, "docx" for word_document, and "latex" for pdf_document.

like image 151
tmpname12345 Avatar answered Nov 14 '22 22:11

tmpname12345