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
out_dir
is if you haven't defined it? When this works after reloading the workspace, it's becauseout_dir
is an object carried forward from your last R session, which allows the code to work even though it shouldn't. – Thomas