繁体   English   中英

Is it possible to knit an R Markdown file only to a preview window/pane in RStudio without saving an html file?

[英]Is it possible to knit an R Markdown file only to a preview window/pane in RStudio without saving an html file?

I would like to keep the base folder of my R Markdown project tree as uncluttered as possible and still use the Knit button in RStudio to see some results as an HTML output in the RStudio's preview window. 但是,每次编织时,都会在 my.Rmd 文件的基本文件夹中创建一个 HTML 文件。

我的文件夹结构可能如下所示:

在此处输入图像描述

If I've understood correctly - and do please correct me if I'm wrong - it is not very straightforward to output the HTML files to an upstream folder (here:"results" folder) from an R Markdown file by pressing the Knit button ,所以我愿意:

  1. 完全阻止创建 HTML 文件,
  2. 在 RStudio 的预览版 window 中仍然可以看到 HTML 格式的结果,以及
  3. 仅当我真的愿意保存 HTML 文件时,保存(导出)这样的文件并(手动)将其传输到结果文件夹。

因此,如果没有简单的方法将 HTML 文件定向到“结果”文件夹,我可以完全阻止 HTML 文件的创建,并且只在预览 window 中预览结果吗?

我不知道将其附加到knit按钮的方法,但您可以编写一个 function 来执行此操作:

  • 在编辑窗格中打开当前文件。 这个答案提供了详细信息。
  • 运行rmarkdown::render()并按照您想要的方式output_dir参数。

您可以使用这些说明将此 function 附加到 RStudio 中的键盘快捷键。

这是 function 的简单版本:

  knit2 <- function() {
    filename <- rstudioapi::getSourceEditorContext()$path
    output_dir <- file.path(dirname(filename), "results")
    result <- rmarkdown::render(filename, output_dir = output_dir)
    getOption("viewer")(result)
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM