How to set the location of the intermediate directory in knitr?

Viewed 154

I redirect the output of my Rmd rendering to another folder via output_dir. This creates the intermediate folder <input name>_files in the output directory but I do not want to have the _files folder to be there. Ideally I would not like to have it in the first place but reading Chapter 16.5 in the RMarkdown Cookbok it is a feature and not a bug that the intermediate folder _files is not deleted for self containing files as soon as we have a chunk with cache = TRUE.

I tried to set the intermediate folder via intermediates_dir but to no avail, the _files folder will always be in the output folder.

What can I do to

  1. Not have this folder (without setting cache = FALSE)?
  2. And if this is not possible, move the _files folder at least to another place?

Reprex

  1. Create an RStudio project and create folders tmp and docs under the project root.

  2. Press the knit button or Press Ctrl+Shift+K for the following file named test.Rmd:

    ---
    title: "Test"
    knit: (function(inputFile, encoding) {
       rmarkdown::render(inputFile, encoding = encoding, 
                         intermediates_dir = here::here("tmp"),
                         output_dir = here::here("docs"))
       })
    ---
    
    ```{r setup, include = FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ## R Markdown
    
    ```{r cars, cache = TRUE}
    summary(cars)
    ```
    
    ## Including Plots
    
    ```{r pressure, echo = FALSE}
    plot(pressure)
    ```
    
  3. You will see now that a folder test_files appear under docs and not under tmp

1 Answers

This is your example:

---
title: "Test"

---

```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

```{r cars, cache = TRUE}
summary(cars)
```

## Including Plots

```{r pressure, cache = TRUE}
plot(pressure)
```

I rendered via console, easier to experiment :)

library (rmarkdown)

render(input = "our_file.Rmd",
       output_file = "our_file.html", #we should add an extention of the first output_format or this file will be rewritten
       output_format = c("html_document", "pdf_document"),
       intermediates_dir = "tmp",
       clean = FALSE
)

What will we achieve in the working directory:

  1. two output files;
  2. tmp folder with .md file and two folders with content(html/pdf);
  3. folders with cache and content separately.

An addition:

I thought, that we can remove the folder_files:

unlink("folder_files", recursive = TRUE)

... and we can.

But this package don't create every time (maybe only in the first process???) and our knitting process falls.

I tried to find out, where is a problem.

If you open .md file, you will see:

![](C:/MyFolder/folder_files/figure-latex/pressure-1.pdf)<!-- --> 

... yes, it takes content from that folder or from output_dir...

How to redirect to tmp or another folder, I don't know.

Related