Export multiples RMarkdown Files - r

How can we export multiple markdown files (HTML, PDF, WORD) from R into zip file. Many thanks for your help!

Suppose, we have the following Rmarkdown file (the source file) which can be rendered as pdf, html and word documents.
multiple_output.Rmd
---
title: "multiple outputs"
output:
word_document: default
html_document: default
pdf_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>.
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Now to render this source file to pdf, html, and word document at once and then zip it into a folder, we run the following code from another Rscript file (rscript file needs to be in the same place where your Rmd source file is)
file.R
# create a folder where the multiple output files will be placed
dir.create("multiple_output")
# then render the source file to that created folder
rmarkdown::render(
"multiple_outupt.Rmd", output_dir = "multiple_output/",
output_format = "all"
)
# create file paths for zipping
files_for_zip <- dir('multiple_output', full.names = TRUE)
# zip the folder
zip(zipfile = "multiple_output", files = files_for_zip)
Then we will have the zipped folder multiple_output.zip.

Related

Run R markdown (.Rmd) from inside other R script to produce HTML

As the example, if you create a new R markdown file and save it as 'test'. Can one then run or deploy this test.Rmd file from within a normal R script. The purpose being to generate the output in HTML, without having to open the .Rmd file.
I'm hoping to create one master file to do this for many markdown files in one go; which would save considerable time as you then don't have to open many markdown files and wait for each one to complete.
You are looking for rmarkdown::render().
Contents of "test.Rmd"
---
title: "Untitled"
output: html_document
---
```{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>.
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:
```{r cars}
summary(cars)
```
Contents of script.R
# provided test.Rmd is in the working directory
rmarkdown::render("test.Rmd")
A Way to Render Multiple Rmd
cwd_rmd_files <- list.files(pattern = ".Rmd$")
lapply(cwd_rmd_files, rmarkdown::render)
Thanks the-mad-statter, your answer was very helpful. The issue I faced, required me to prepare markdown dynamically. By adapting your code, that's easily possible:
Contents of "test_dyn.rmd"
---
title: "Untitled"
output: html_document
---
The chunk below adds formatted text, based on your inputs.
```{r text, echo=FALSE, results="asis"}
cat(text)
```
The chunk below uses your input in as code.
```{r results}
y
```
Contents of "script_dyn.r"
in_text <- c("**Test 1**", "*Test 2*")
in_y <- 1:2
lapply(1:2, function(x) {
text <- in_text[[x]]
y <- in_y[[x]]
rmarkdown::render(input = "test_dyn.rmd", output_file = paste0("test", x))
})
Like this you can create files with different text and different variables values in your code.

Show PNG images consecutively with loop in R Markdown chunk (HTML)

Can one use an R Markdown chunk loop to show multiple graphics files (e.g., PNG) consecutively (one above the other) in HTML output? This loop would identify the files in a vector, such as via list.files().
I have experimented with no avail to uses of writeLines("\n"), cat('\r\n\r\n') per this SO.
This R Markdown code (formatted below but link is .Rmd) is a reproducible example with an attempt using writeLines("\n") and cat('\r\n\r\n'). Please note this copies 5 R logo PNG (only 12kb) file copies into your working directory.
---
title: "Stack Overflow Consecutive PNG"
author: "Rick Pack"
date: "11/20/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Copy a PNG file as multiple files
```{r png_copy, echo=FALSE}
for (q in 1:5) {
file.copy(list.files(system.file("img", package = "png"),
full.names = TRUE),
getwd())
file.rename("Rlogo.png", paste0("Rlogo_",q,".png"))
}
```
# Only one R logo shown instead of the five available
```{r png_show, echo=FALSE}
library(png)
# Providing the folder so you can delete the png files
# created above
print(getwd())
all_img <- list.files(full.names = TRUE)[grepl(
"Rlogo", list.files())]
for (j in 1:length(all_img)) {
grid::grid.raster(readPNG(all_img[j]))
writeLines("\n")
cat('\r\n\r\n')
cat("\n\n\\pagebreak\n")
}
```
You can use R Markdown syntax within the for loop instead of the png package. Given you have the images in the same directory as your Rmd, the following should work:
```{r, results = "asis"}
filelist <- c("Rlogo_1", "Rlogo_2", "Rlogo_3")
for(i in filelist) {
cat(paste0("![](", i, ".png)"), "\n")
cat("\n\n\\pagebreak\n")
}
```
Output:
See this related question for more info: Insert images using knitr::include_graphics in a for loop

Setting knitr to output separate images

I'm trying to get the PNG plots from knitr output to write to disk as separate files without doing it manually.
What I tried
The dev = 'png' setting from http://gforge.se/2014/01/fast-track-publishing-using-knitr-part-iii/ (also mentioned in this question)
self_contained: no from knitr: include figures in report *and* output figures to separate files.
Neither worked. The folder the knitting process ran in has no extra files, and the HTML document has base64 embedded images in its source.
Environment
RStudio 0.99.903
R 3.2.3
knitr 1.15.1
MWE: The RStudio RMarkdown file, with abovementioned options added:
---
title: "Untitled"
output: html_document
self_contained: no
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(dev="png",
dev.args=list(type="cairo"),
dpi=96)
```
## 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>.
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:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
self_contained is an option for html_document, not a top-level YAML setting. The document below works using just that. PNG is the default figure type, so you don't need to specify that.
---
title: "Untitled"
output:
html_document:
self_contained: no
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```

Remove Markdown Default Code

In RStudio, whenever I make a new Markdown it always has default code. How do I remove it?
Here's the code that shows up.
```{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>.
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:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Every time, when creating a Rmd file, Rstudio will open a Rmd template:
https://github.com/rstudio/rstudio/blob/822e558c7a274de464f992f69e3acee2fde5ab04/src/cpp/session/modules/SessionRMarkdown.R
# given a path to a folder on disk, return information about the R Markdown
# template in that folder.
.rs.addFunction("getTemplateDetails", function(path) {
# check for required files
templateYaml <- file.path(path, "template.yaml")
skeletonPath <- file.path(path, "skeleton")
if (!file.exists(templateYaml))
return(NULL)
if (!file.exists(file.path(skeletonPath, "skeleton.Rmd")))
return(NULL)
# load template details from YAML
templateDetails <- yaml::yaml.load_file(templateYaml)
# enforce create_dir if there are multiple files in /skeleton/
if (length(list.files(skeletonPath)) > 1)
templateDetails$create_dir <- TRUE
templateDetails
})
Hence the simplest solution would be:
go to xxx\RStudio\resources\templates folder, where your
Rstudio installed
open r_markdown_v2.Rmd file, and delete
everthing
save it
Now, everytime, when you open a rmarkdown, you can have just the yaml part.

reference knitr chunk in R markdown

I'm using knitr to embed R code within a markdown (.Rmd) file. I'm converting the file to a PDF file with Pandoc. The contents of my .Rmd file looks like this:
Report
========================================================
This is my report. Some analysis is included in LINK TO CHUNK NAMED MY.ANALYSIS HERE
```{r my.analysis, echo=FALSE}
summary(cars)
```
Where it says LINK TO CHUNK NAMED MY.ANALYSIS HERE, am I able provide a link to the code chunk named my.analysis in the outputted PDF?
I believe the OP asked a similar, but slightly different question here: figure captions, references using knitr and markdown to html
Do this:
Report
========================================================
This is my report. Some analysis is included in \href{http://stackoverflow.com/q/16445247/1000343}{LINK TO CHUNK NAMED MY.ANALYSIS HERE}
```{r my.analysis, echo=FALSE}
summary(cars)
```
And then covert the markdown file (not Rmd) to the PDF.
This also works using the devel version of reports:
```{r setup, include=FALSE}
# set global chunk options
opts_chunk$set(cache=TRUE)
library(reports); library(knitr);
```
Report
========================================================
This is my report. Some analysis is included in `r HR("http://stackoverflow.com/q/16445247/1000343", "LINK TO CHUNK NAMED MY.ANALYSIS HERE")`
```{r my.analysis, echo=FALSE}
summary(cars)
```
You then convert the html file to pdf.

Resources