在 RMarkdown 中使用knitr按钮时如何调整输出文件夹?

Tom*_*Tom 6 r knitr r-markdown

如果我有以下文件夹结构...

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md \n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 project.RProj\n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 data\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 some_data.csv\n\xe2\x94\x82\n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 notebooks\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 analysis.Rmd\n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 output\n
Run Code Online (Sandbox Code Playgroud)\n

使用 knit 按钮时,如何更改 RMarkdown 文件中的 yaml 以将 HTML 文件输出到输出文件夹而不是笔记本文件夹中?

\n

我希望能够在 RMarkdown 中单击 knit 并最终得到......

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md \n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 project.RProj\n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 data\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 some_data.csv\n\xe2\x94\x82\n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 notebooks\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 analysis.Rmd\n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 output\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 analysis.html\n
Run Code Online (Sandbox Code Playgroud)\n

我已经看到 Yihui 有一个很好的示例,说明如何编辑输出以在 R Markdown 食谱中添加日期(https://bookdown.org/yihui/rmarkdown-cookbook/custom-knit.html)。贴在下面。但我想为这个特定用例提供一个示例。

\n
knit: (function(input, ...) {\n    rmarkdown::render(\n      input,\n      output_file = paste0(\n        xfun::sans_ext(input), \'-\', Sys.Date(), \'.html\'\n      ),\n      envir = globalenv()\n    )\n  })\n
Run Code Online (Sandbox Code Playgroud)\n

sta*_*rja 12

您可以指定output_dir参数,在您的情况下,基本 YAML 标头如下所示:

---
title: "Test"
output: html_document
knit: (function(input, ...) {
    rmarkdown::render(
      input,
      output_dir = "../output"
    )
  })
---
Run Code Online (Sandbox Code Playgroud)