来自另一个Rmd中Rmd文件的源代码 [英] Source code from Rmd file within another Rmd

查看:158
本文介绍了来自另一个Rmd中Rmd文件的源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使代码更具模块化:在一个脚本中进行数据加载和清理,在另一个脚本中进行分析,等等.如果我使用的是R脚本,那么在data_setup.R上调用source就很简单了.在analysis.R中,但是我想在Rmarkdown文档中记录我为数据设置和分析所做的决策.因此,我正在尝试编写某种source_rmd函数,该函数将允许我将代码从data_setup.Rmd导出到analysis.Rmd.

到目前为止我已经尝试过:

如何像这样获取R Markdown文件的答案如果有重复的块名称,source('myfile.r')`?不起作用(由于名为setup的块在Rstudio的笔记本处理中具有特殊行为,因此会出现问题). 如何合并两个RMarkdown(.Rmd)文件想要合并整个文档,而不仅仅是合并一个文档中的代码,还需要唯一的块名称.我已经尝试按照生成动态R降价块中的建议使用knit_expand a>,但是我必须用双花括号命名带有变量的块,而且我真的很想让我的colaborator也容易使用这种方法.............................................................................并按照的建议使用knit_child巢式编织调用可以解决重复的标签错误?仍然会给我重复的标签错误.

解决方案

进一步搜索后,我找到了解决方案. knitr中有一个软件包选项,可以将其设置为更改处理重复块的行为,在它们的标签后附加一个数字,而不是失败而导致错误.参见 https://github.com/yihui/knitr/issues/957 .. >

要设置此选项,请使用options(knitr.duplicate.label = 'allow').

为了完整起见,我编写的函数的完整代码为

source_rmd <- function(file, local = FALSE, ...){
  options(knitr.duplicate.label = 'allow')

  tempR <- tempfile(tmpdir = ".", fileext = ".R")
  on.exit(unlink(tempR))
  knitr::purl(file, output=tempR, quiet = TRUE)

  envir <- globalenv()
  source(tempR, local = envir, ...)
}

I'm attempting to make my code more modular: data loading and cleaning in one script, analysis in another, etc. If I were using R scripts, this would be a simple matter of calling source on data_setup.R inside analysis.R, but I'd like to document the decisions I'm making in an Rmarkdown document for both data setup and analysis. So I'm trying to write some sort of source_rmd function that will allow me to source the code from data_setup.Rmd into analysis.Rmd.

What I've tried so far:

The answer to How to source R Markdown file like `source('myfile.r')`? doesn't work if there are any repeated chunk names (a problem since the chunk named setup has special behavior in Rstudio's notebook handling). How to combine two RMarkdown (.Rmd) files into a single output? wants to combine entire documents, not just the code from one, and also requires unique chunk names. I've tried using knit_expand as recommended in Generate Dynamic R Markdown Blocks, but I have to name chunks with variables in double curly-braces, and I'd really like a way to make this easy for my colaborators to use as well. And using knit_child as recommended in How to nest knit calls to fix duplicate chunk label errors? still gives me duplicate label errors.

解决方案

After some further searching, I've found a solution. There is a package option in knitr that can be set to change the behavior for handling duplicate chunks, appending a number after their label rather than failing with an error. See https://github.com/yihui/knitr/issues/957.

To set this option, use options(knitr.duplicate.label = 'allow').

For the sake of completeness, the full code for the function I've written is

source_rmd <- function(file, local = FALSE, ...){
  options(knitr.duplicate.label = 'allow')

  tempR <- tempfile(tmpdir = ".", fileext = ".R")
  on.exit(unlink(tempR))
  knitr::purl(file, output=tempR, quiet = TRUE)

  envir <- globalenv()
  source(tempR, local = envir, ...)
}

这篇关于来自另一个Rmd中Rmd文件的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆