24
votes

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

1
Imagine running that code in a clean R session. How does R know what out_dir is if you haven't defined it? When this works after reloading the workspace, it's because out_dir is an object carried forward from your last R session, which allows the code to work even though it shouldn't.Thomas
You are right, but the command get evaluated at the of the rmakrdown process when the html gets created. I also can set the variable out_dir by hand and there is still the same problem and Rstudio complains that out_dir does not exist... If the script runs once, I can also execute rm(list=ls()) and it works afterwards as well... So I think it isn't just an issue of setting the variable...drmariod
There's no way this code can work without you specifying that variable. Why it seems to work is hard to say without a complete description of the workflow you're following.Thomas
I added the correct workflow. It is really strange, since everything works, if I at least reopen the project... Not sure if anyone else can reproduce this... But it also makes no concrete sense to me. I just thought, that the command will be evaluated in the end of the whole markdown process...drmariod
Just found the link to the original trickdrmariod

1 Answers

22
votes

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')) })