如何将.nb.html笔记本文件保存在一个带有.rmd文件的目录之外的另一个目录中?

发布于 2025-01-28 04:11:46 字数 1590 浏览 3 评论 0原文

上下文:我有一个R项目,其中包含用于脚本,输出和数据的Disctinct文件夹。要匹配此逻辑,我想在保存./ scripts/文件夹中.rmd文件,而保存.nb.html文件./ outputs/文件夹。

问题:如何使编织保存.nd.html在文件夹./ output/中单击rstudio中的“保存”按钮时IDE?

我试图使用 knitr :: opts_knit $ set()没有成功。我知道可以在单独的r脚本中使用rmarkDown :: render(input =“ ./scripts/test.rmd”,output_dir =“ ./outputs/”)来执行此操作我正在寻找不需要制作此类R脚本的解决方案。

SO POST 非常接近,并且适用于HTML输出。但是,使用笔记本似乎没有考虑到YAML的自定义编织功能。

这是当前的文件夹文件树:

C:.
|   testR.Rproj
|   
+---outputs
\---scripts
        test.Rmd

如果我呈现test.rmd,我得到以下内容:

C:.
|   testR.Rproj
|   
+---outputs
\---scripts
        test.nb.html
        test.Rmd

预期输出为:

C:.
|   testR.Rproj
|   
+---outputs
|       test.nb.html
|       
\---scripts
        test.Rmd

复制此结构的代码。.RMD文件为空,我不知道如何使输出与文件完全相同>新文件> r笔记本

library(usethis)
library(rmarkdown)
# set up the folder and files
usethis::create_project(path = paste0(getwd(),"/testR"), open = FALSE, rstudio = TRUE)
dir.create(paste0(getwd(),"/testR/outputs"))
dir.create(paste0(getwd(),"/testR/scripts"))
file.edit(paste0(getwd(),"/testR/scripts/test.Rmd"))

Context: I have a R project with disctinct folders for scripts, outputs and data. To match this logic, I would like to have .rmd files in the ./scripts/ folder while saving the .nb.html file in the ./outputs/ folder.

Question: how to make knit save the .nd.html file in the folder ./output/ when clicking the "Save" button in RStudio IDE?

I tried to change knitr options using knitr::opts_knit$set() without success. I know it is possible to use rmarkdown::render(input = "./scripts/test.Rmd", output_dir = "./outputs/") in a separate R script to do this but I am looking for a solution that does not require to make such separate R script.

This SO post is very close, and works for HTML outputs. However, with Notebooks it seems not to take into account the custom knit function into the YAML.

Here is the current folder-files tree:

C:.
|   testR.Rproj
|   
+---outputs
\---scripts
        test.Rmd

If I render the test.Rmd, I get the following:

C:.
|   testR.Rproj
|   
+---outputs
\---scripts
        test.nb.html
        test.Rmd

And the expected output is:

C:.
|   testR.Rproj
|   
+---outputs
|       test.nb.html
|       
\---scripts
        test.Rmd

Code to reproduce this structure the .Rmd file is empty, I do not know how to make the exact same output as File > New File > R Notebook:

library(usethis)
library(rmarkdown)
# set up the folder and files
usethis::create_project(path = paste0(getwd(),"/testR"), open = FALSE, rstudio = TRUE)
dir.create(paste0(getwd(),"/testR/outputs"))
dir.create(paste0(getwd(),"/testR/scripts"))
file.edit(paste0(getwd(),"/testR/scripts/test.Rmd"))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雾里花 2025-02-04 04:11:46

您可以尝试以下内容,在YAML标题中,可以将函数传递到knit参数:

---
title: "Some Report"
knit: (function(input, ...) {rmarkdown::render(input, output_file="path/to/output/location/filename.html")})
output: html_document
---

您可以阅读更多在这里。请注意,尽管在链接中提到您可以传递多行函数,但是我在传递多函数方面遇到了麻烦。将函数作为单行工作传递。由于您正在通过一个函数,因此您可以进一步参数化(做更多的事情),例如,请参见在这里

You can try the following, in the yaml header, you can pass a function to knit parameter:

---
title: "Some Report"
knit: (function(input, ...) {rmarkdown::render(input, output_file="path/to/output/location/filename.html")})
output: html_document
---

You can read more here. Note although, in the link it is mentioned you can pass a multiline function, but I have had trouble with passing multiline function. Passing the function as a single line works. Since you are passing a function, you can parametrize (do more things) it further, for example see here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文