Knit one markdown file to two output files
Yes it's possible to render multiple outputs, but not with the "knit" Button in RStudio. Write your desired output in the YAML header and then use output_format = "all"
as argument in
rmarkdown::render(<your-rmd-file.rmd>, output_format ="all")
So the YAML header looks like:
title: "multiple outputs"
output:
word_document: default
html_document: default
Or any option you want to set for the different output formats.
I don't know if it is possible to set different output directories, but I don't think so.
Specifying multiple simultaneous output formats in knitr (new)
I actually briefly mentioned in Render all vignette formats #1051 and you missed it:
rmarkdown::render('your.Rmd', output_format = 'all')
It is documented on the help page ?rmarkdown::render
.
Render multiple outputs from one Rmd
Option 1
you can use rmarkdown::render
in knit
yaml key and specify ouput_format = "all"
to render the documents to all the specified formats in the yaml section.
---
title: Render a table in a tiny environment
output:
pdf_document: default
html_document: default
knit: (function(inputFile, encoding) {
rmarkdown::render(inputFile, encoding = encoding,
output_dir = "output", output_format = "all") })
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
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>.
After clicking on Knit, you will have both pdf and html file in the output folder.
Option 2
Which is actually the same thing as option 1, but uses a bit different workflow.
In your Rmd file yaml section, you simply specify the output format like this,
---
title: Render a table in a tiny environment
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
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>.
Then in the console or another r-script, run this,
rmarkdown::render('multiple_output.Rmd',output_dir = "output", output_format = 'all')
Again, similarly you will have both pdf and html file in the output folder.
Is it possible to knit multiple .rmd files at once?
To render a list of documents, first you need to put the document names in a variable. One way to do that is
files <- list.files(pattern = "[.]rmd$")
This assumes your files are named *.rmd
. If they are *.Rmd
, modify accordingly.
Then to render them all, just use a for
loop:
for (f in files) rmarkdown::render(f)
This assumes you have the headers all set up to define the output you want. Set the output_format
argument to render()
if you want to override that.
How to combine two RMarkdown (.Rmd) files into a single output?
August, 2018 update: This answer was written before the advent of bookdown, which is a more powerful approach to writing Rmarkdown based books. Check out the minimal bookdown example in @Mikey-Harper's answer!
When I want to break a large report into separate Rmd, I usually create a parent Rmd and include the chapters as children. This approach is easy for new users to understand, and if you include a table of contents (toc), it is easy to navigate between chapters.
report.Rmd
---
title: My Report
output:
pdf_document:
toc: yes
---
```{r child = 'chapter1.Rmd'}
```
```{r child = 'chapter2.Rmd'}
```
chapter1.Rmd
# Chapter 1
This is chapter 1.
```{r}
1
```
chapter2.Rmd
# Chapter 2
This is chapter 2.
```{r}
2
```
Build
rmarkdown::render('report.Rmd')
Which produces:
And if you want a quick way to create the chunks for your child documents:
rmd <- list.files(pattern = '*.Rmd', recursive = T)
chunks <- paste0("```{r child = '", rmd, "'}\n```\n")
cat(chunks, sep = '\n')
# ```{r child = 'chapter1.Rmd'}
# ```
#
# ```{r child = 'chapter2.Rmd'}
# ```
Run R Markdown on many different datasets and save each knitted word document separately
You could call render
in a loop to process each file
passed as a parameter :
dir_in <- 'data'
dir_out <- 'result'
files <- file.path(getwd(),dir_in,list.files(dir_in))
for (file in files) {
print(file)
rmarkdown::render(
input = 'Hello_World.Rmd',
output_file = tools::file_path_sans_ext(basename(file)),
output_dir = dir_out,
params = list(file = file)
)
}
Rmarkdown :
---
title: Hello_World
author: "Somebody"
output:
bookdown::word_document2:
fig_caption: yes
number_sections: FALSE
params:
file: ""
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(openxlsx)
# Load data for one .xlsx file. The other datasets are all in "/data".
dataset <- openxlsx::read.xlsx(file)
```
# Test for Errors
```{r test, echo=FALSE, comment=NA}
# Are there any NA values in the column?
suppressWarnings(if (TRUE %in% is.na(dataset$name)) {
na.index <- which(is.na(dataset$name))
cat(
paste(
"– There are NAs/blanks in the name column. There should be no blanks in this column. The following row numbers in this column need to be corrected:",
paste(na.index, collapse = ', ')
),
".",
sep = "",
"\n",
"\n"
)
})
```
Related Topics
Sum of Two Columns of Data Frame with Na Values
Merge Dataframes on Matching A, B and *Closest* C
Finding Where Two Linear Fits Intersect in R
Add Axis Tick-Marks on Top and to the Right to a Ggplot
Deleting Specific Rows from a Data Frame
List Members Can Be Accessed with Partial Name? Is This a Feature
Regression (Logistic) in R: Finding X Value (Predictor) for a Particular Y Value (Outcome)
Adding an Repeated Index for Factors in Data Frame
Understanding Element Wise Clearing of R's Workspace
How to Calculate Number of Days Between Two Dates in R
Get the Size of the Window in Shiny
How to Handle Vectors Without Knowing the Type in Rcpp
Add an Image to a Table-Like Output in R
Create Url Hyperlink in R Shiny
How to Calculate Adjacency Matrices in R
Adding 15 Business Days in Lubridate
Keeping Only Certain Rows of a Data Frame Based on a Set of Values