Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rmarkdown directing output file into a directory

Tags:

I found a really nice trick (link) to a function of knitr, where you can save your output html into an output folder and under a different filename.

The only thing you have to head to the header is the following:

title: "analysis" author: "Me" date: "`r format(Sys.time(), '%d %B, %Y, %H:%M')`" knit: (function(inputFile, encoding) {        rmarkdown::render(inputFile,                         encoding=encoding,                          output_file=file.path(dirname(inputFile), out_dir, 'analysis.html')) }) output:   html_document:     number_sections: yes     toc: yes 

This works on my Mac 'sometimes' very well, but sometimes it has problems to find the out_dir variable...

I first thought about executing the chunks first, so the variable is set... But this didn't solved the problem...

I also restarted R session and this didn't helped.

The last step was closing R, saving the workspace and after reopening R and loading workspace it works like a charm again.

I could not find the original post, where somebody recommended this trick...

EXACT WORKFLOW TO REPRODUCE

open new project, name it test in a new folder
create a r markdown document
change the header to:

--- title: "Untitled" author: "Me" date: "`r format(Sys.time(), '%d %B, %Y, %H:%M')`" knit: (function(inputFile, encoding) {        rmarkdown::render(inputFile,                         encoding=encoding,                          output_file=file.path(dirname(inputFile), out_dir, 'analysis.html')) }) output:   html_document:     number_sections: yes     toc: yes ---  ```{r write quant output files} out_dir <- 'test' if(!file.exists(out_dir)) {   dir.create(out_dir) } ``` 

save the document as test.Rmd
click the knit button (html is now removed from the options of the button)
This will fail!

Close the project!
Click on save environment!

Open the Project and click knit!
Everything works.

execute rm(list=ls()) everything works afterwards again

like image 708
drmariod Avatar asked Mar 06 '15 08:03

drmariod


People also ask

How do I change the working directory in R Markdown?

The usual way to change the working directory is setwd() , but please note that setwd() is not persistent in R Markdown (or other types of knitr source documents), which means setwd() only works for the current code chunk, and the working directory will be restored after this code chunk has been evaluated.

Where does R Markdown save to?

rmarkdown will use the formatting instructions that you provided with markdown syntax. Once the file is rendered, RStudio will show you a preview of the new output and save the output file in your working directory.


1 Answers

You could try setting the out_dir variable in the function you are giving knit to render:

knit: (function(inputFile, encoding) {        out_dir <- 'test';       rmarkdown::render(inputFile,                         encoding=encoding,                          output_file=file.path(dirname(inputFile), out_dir, 'analysis.html')) }) 
like image 122
NicE Avatar answered Oct 08 '22 03:10

NicE