knitr中如何设置中间目录的位置?

Posted

技术标签:

【中文标题】knitr中如何设置中间目录的位置?【英文标题】:How to set the location of the intermediate directory in knitr? 【发布时间】:2022-01-19 10:52:56 【问题描述】:

我通过output_dirRmd 渲染的输出重定向到另一个文件夹。这会在 输出目录 中创建中间文件夹 <input name>_files,但我不想让 _files 文件夹在那里。理想情况下,我不想一开始就拥有它,但阅读Chapter 16.5 in the RMarkdown Cookbok 这是一个功能,而不是一个错误,即一旦我们有一个带有cache = TRUE 的块,中间文件夹_files 就不会因为自包含文件而被删除.

我尝试通过intermediates_dir 设置中间文件夹,但无济于事,_files 文件夹将始终在输出文件夹中。

我能做什么

    没有这个文件夹(没有设置cache = FALSE)? 如果这不可行,至少将_files 文件夹移动到另一个地方?

代表

    创建一个RStudio项目,并在项目根目录下创建文件夹tmpdocs

    knit 按钮或按Ctrl+Shift+K 获取以下名为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)
    ```
    

    您现在将看到文件夹test_files 出现在docs 下,而不是tmp

【问题讨论】:

我们可以另辟蹊径。 1. 输出 .pdf 文件 + 2. 包含图像等的文件夹 + 3. 包含图像和 md 文件的 inter 文件夹。如果您对此答案感兴趣,我可以添加。 @manro 绝对是。我正在输出到 html,但他不应该是一个破坏者,对吧? 我想,不。稍后我会尝试。并添加答案。 【参考方案1】:

这是你的例子:

---
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)
```

我通过控制台渲染,更容易实验:)

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
)

我们将在工作目录中实现什么:

    两个输出文件; 带有 .md 文件的 tmp 文件夹和两个带有内容的文件夹(html/pdf); 具有缓存和内容的文件夹。

补充:

我想,我们可以删除文件夹文件:

unlink("folder_files", recursive = TRUE)

...我们可以。

但是这个包不是每次都创建(可能只在第一个过程中???),我们的编织过程就会失败。

我试图找出,哪里出了问题。

如果你打开.md文件,你会看到:

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

...是的,它从该文件夹或 output_dir 中获取内容...

如何重定向到tmp或其他文件夹,我不知道。

【讨论】:

是的,但我想要的是没有创建内容文件夹。或者至少不在输出目录中创建。 @thothal 查看补充

以上是关于knitr中如何设置中间目录的位置?的主要内容,如果未能解决你的问题,请参考以下文章

如何更改 knitr 选项中间块

xtable + knitr 设置列宽html

如何在`fieldset legend`的中间位置设置文本

如何将 ActionBar 标题设置在中间而不是默认的左对齐位置?

matlab中如何设置y轴位置,使y轴在x轴的中间,我做的图y轴在X轴的一边了

如何在带有`knitr::kable`的表格*上方添加标题?