将一个Markdown文档中的特定块包含在另一个文档中 [英] Include a specific chunk from one markdown document in another document

查看:104
本文介绍了将一个Markdown文档中的特定块包含在另一个文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个Markdown文档的特定块中的代码包含到第二个Markdown文档中.我想通过按名称引用该块来做到这一点(请对行号的hacky引用).我不想在子代中运行所有代码,因为其中一些相当耗时.

I want to include the code from a specific chunk of a one markdown document into a second markdown doc. I want to do it by referring to the chunk by name (no hacky references to line-numbers, please). I don't want to run all the code in the child because some of it is rather time-consuming.

这就是我尝试过的.我们有 read_chunk ,用于在markdown文档中包含普通的R脚本.有 run_chunk ,但不清楚是否可以可以与外部文档一起使用(到目前为止,我还没有运气).

Here's what I've tried. We have read_chunk for including plain R script in markdown docs. There is run_chunk but it's not clear if this can be used with external docs (I haven't had any luck so far).

我们可以以获取整个降价文档,使其在另一个文档中运行:

And we can do this to get an entire markdown doc to run inside another one:

```{r child='first.Rmd'}
```

但是,如何从子文档中仅获取一个特定的块到另一个文档中呢?这是一个小例子:

But how can I get just one specific chunk from a child document into another doc? Here's a small example:

这是test-main.Rmd

```{r pick-up-the-kid, child='test-child.Rmd'}
```

这是test-child.Rmd

Hi, there. I'm a child.

```{r test-child-1}
1+1
dnorm(0)
```

```{r test-child-2}
2+2
dnorm(0)
```

当我们运行test-main.Rmd时,我们得到了:

When we run test-main.Rmd we get this:

Hi, there. I’m a child.

1+1
## [1] 2
dnorm(0)
## [1] 0.3989
2+2
## [1] 4
dnorm(0)
## [1] 0.3989

几乎可以做到这一点的一种方法是 ref.label .如果我们这样编辑test-main.Rmd:

One method that almost does it is ref.label. If we edit test-main.Rmd like so:

```{r pick-up-the-kid, child='test-child.Rmd', ref.label='test-child-2'}
```

输出仅具有所需的块,但它是重复的,这是不好的:

The output has the only the desired chunk, but it's duplicated, which is no good:

Hi, there. I’m a child.

2+2
## [1] 4
dnorm(0)
## [1] 0.3989
2+2
## [1] 4
dnorm(0)
## [1] 0.3989

重复的一个解决方案是在其中使用eval = FALSE, echo = FALSE子文档中的块选项:

One solution to the duplication is to use eval = FALSE, echo = FALSE in the chunk options in the child document:

```{r test-child-2, eval = FALSE, echo = FALSE}
2+2
dnorm(0)
```

给出所需的结果:

2+2
## [1] 4
dnorm(0)

但是更改子文档中的块并不方便,因为子文档中的其他块都需要该块,并且我不想每次运行时都在子文档中更改一些块主要文档,它对可重复性不好.

But this is not convenient to alter the chunk in the child document, since that chunk is needed by other chunks in the child doc, and I don't want to change with a few chunks in the child doc each time I run the main doc, it's not good for reproducibility.

如何通过引用块名称(并且不能重复或不喜欢块选项)将中的仅 test-child-2test-child.Rmd放入test-main.Rmd? strong>

How can I get only chunk test-child-2 from test-child.Rmd into test-main.Rmd by reference to the chunk name (and without duplicates or fiddling with the chunk options)?

我正在寻找一个可以称为child_chunk的函数,在该函数中我可以给子文档名称和块名称,并在主文档中对其应用与子文档中的块选项无关的块选项.

I'm looking for a function that could be calledchild_chunk where I can give the child doc name and chunk name and apply chunk options to it in the main doc that are independent of the chunk options in the child doc.

还是将代码移入R脚本文件并在两个markdown文档之间共享的唯一解决方案?

Or is the only solution to move code into R script files and share them between the two markdown docs?

推荐答案

我会尝试purl-原始子文档,然后从代码缠结文件中读取大块,然后可以将其删除.

I would try purl-ing the original child document and then reading the chunk in from the code tangle file, which you can then subsequently delete.

这里是test-main.Rmd

```{r echo=FALSE}
invisible(purl("test-child.Rmd", output="temp", quiet=TRUE))
read_chunk("temp")
```

```{r ref.label='test_child_2'}
```

```{r echo=FALSE}
unlink("temp")
```

我将您的test-child.Rmd修改为使用不同的标签,因为您的标签不在我的计算机上工作

I modified your test-child.Rmd to use different labels because yours weren't working on my machine:

在那里.我是小孩.

```{r test_child_1}
1+1
dnorm(0)
```

```{r test_child_2}
2+2
dnorm(0)
```

knit('test-main.Rmd')的输出为:

```r
2+2
```


```
## [1] 4
```

```r
dnorm(0)
```

```
## [1] 0.3989
```

这篇关于将一个Markdown文档中的特定块包含在另一个文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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