Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knit one markdown file to two output files

Is there a way to knit a single .md file to a .html or .docx in the working directory in R and simultaneously post a copy of the .html to another folder, possibly on another drive?

Alternatively, can the 'publish' button in RStudio be used to send a .md file to a location other than RPubs?

like image 784
steinbock Avatar asked Sep 23 '16 13:09

steinbock


People also ask

How do you knit a rmarkdown file?

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.

What is knitr in rmarkdown?

RMarkdown is an extension to markdown which includes the ability to embed code chunks and several other extensions useful for writing technical reports. The rmarkdown package extends the knitr package to, in one step, allow conversion between an RMarkdown file (.Rmd) into PDF, HTML, word document, amongst others.


2 Answers

Actually, the knit button can indeed -

  • Render multiple outputs.
  • Use a custom output directory.

Example with output directory called output:

title: "multiple outputs"
output:
  word_document: default
  html_document: default
knit: (function(inputFile, encoding) {
  rmarkdown::render(inputFile, encoding = encoding,
  output_dir = "output", output_format = "all") })
like image 84
Peter Harrison Avatar answered Sep 26 '22 01:09

Peter Harrison


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.

like image 37
J_F Avatar answered Sep 25 '22 01:09

J_F