从knitr中的子文档中剥离YAML
我正在rmarkdown中编写一些相关文档,我将通过jekyll将这些文档编译成一个网站。在这样做的过程中,我遇到了一个问题: 我正在使用的一些Rmd文件将其他Rmd文件作为子文档调用。使用knitr渲染时,生成的文档包含来自父文档和子文档的yaml前端内容。下面给出了一个例子 到目前为止,当一个子文档是Rmd时,我还看不到任何方法可以只指定该文档的一部分。有人知道有什么方法可以在knit()期间将子文档读入父Rmd时从子文档中剥离yaml吗 我很乐意考虑R以外的答案,最好是我可以嵌入RaKFr档中的一些东西。不过,我不想永久地修改子文档。所以剥离yaml不会是永久性的。最后,yaml的长度因文件而异,所以我猜任何解决方案都需要能够通过regex/grep/sed/etc找到yaml的开头和结尾 例如: %%%%母公司文件rmd%%%从knitr中的子文档中剥离YAML,r,yaml,knitr,rstudio,r-markdown,R,Yaml,Knitr,Rstudio,R Markdown,我正在rmarkdown中编写一些相关文档,我将通过jekyll将这些文档编译成一个网站。在这样做的过程中,我遇到了一个问题: 我正在使用的一些Rmd文件将其他Rmd文件作为子文档调用。使用knitr渲染时,生成的文档包含来自父文档和子文档的yaml前端内容。下面给出了一个例子 到目前为止,当一个子文档是Rmd时,我还看不到任何方法可以只指定该文档的一部分。有人知道有什么方法可以在knit()期间将子文档读入父Rmd时从子文档中剥离yaml吗 我很乐意考虑R以外的答案,最好是我可以嵌入RaKFr
---
title: parent doc
layout: default
etc: etc
---
This is the parent...
```{r child import, child="./child_doc."}
```
---
title: child doc
layout: default
etc: etc
---
lorem ipsum etc
%%%%子文档rmd%%%
---
title: parent doc
layout: default
etc: etc
---
This is the parent...
```{r child import, child="./child_doc."}
```
---
title: child doc
layout: default
etc: etc
---
lorem ipsum etc
%%%%output.md%%%
---
title: parent doc
layout: default
etc: etc
---
This is the parent...
---
title: child doc
layout: default
etc: etc
---
lorem ipsum etc
---
title: parent doc
layout: default
etc: etc
---
This is the parent...
lorem ipsum etc
%%%%理想输出.md%%%
---
title: parent doc
layout: default
etc: etc
---
This is the parent...
---
title: child doc
layout: default
etc: etc
---
lorem ipsum etc
---
title: parent doc
layout: default
etc: etc
---
This is the parent...
lorem ipsum etc
同时,也许以下几点对你有用;这是一种丑陋而低效的工作方式(我是knitr的新手,不是一个真正的程序员),但它实现了我相信您想要做的事情 我曾为类似的个人用途写过一篇文章,包括以下内容:;原文是西班牙语,因此我将其翻译如下:
extraction <- function(matter, escape = FALSE, ruta = ".", patron) {
require(yaml)
# Gather together directory of documents to be processed
doc_list <- list.files(
path = ruta,
pattern = patron,
full.names = TRUE
)
# Extract desired contents
lapply(
X = doc_list,
FUN = function(i) {
raw_contents <- readLines(con = i, encoding = "UTF-8")
switch(
EXPR = matter,
# !YAML (e.g., HTML)
"no_yaml" = {
if (escape == FALSE) {
paste(raw_contents, sep = "", collapse = "\n")
} else if (escape == TRUE) {
require(XML)
to_be_escaped <- paste(raw_contents, sep = "", collapse = "\n")
xmlTextNode(value = to_be_escaped)
}
},
# YAML header and Rmd contents
"rmd" = {
yaml_pattern <- "[-]{3}|[.]{3}"
limits_yaml <- grep(pattern = yaml_pattern, x = raw_contents)[1:2]
indices_yaml <- seq(
from = limits_yaml[1] + 1,
to = limits_yaml[2] - 1
)
yaml <- mapply(
FUN = function(i) {yaml.load(string = i)},
raw_contents[indices_yaml],
USE.NAMES = FALSE
)
indices_rmd <- seq(
from = limits_yaml[2] + 1,
to = length(x = raw_contents)
)
rmd<- paste(raw_contents[indices_rmd], sep = "", collapse = "\n")
c(yaml, "contents" = rmd)
},
# Anything else (just in case)
{
stop("Matter not extractable")
}
)
}
)
}
在main.Rmd
中,我从/sections
导入子文档:
---
title: Main
author: me
date: Today
output:
html_document
---
```{r, 'setup', include = FALSE}
opts_chunk$set(autodep = TRUE)
dep_auto()
```
```{r, 'import_children', cache = TRUE, include = FALSE}
source('./assets/extraction.R')
rmd <- extraction(
matter = 'rmd',
ruta = './sections',
patron = "*.Rmd"
)
```
# Abstract
```{r, 'abstract', echo = FALSE, results = 'asis'}
cat(x = rmd[[1]][["contents"]], sep = "\n")
```
# Introduction
```{r, 'intro', echo = FALSE, results = 'asis'}
cat(x = rmd[[2]][["contents"]], sep = "\n")
```
# Methods
```{r, 'methods', echo = FALSE, results = 'asis'}
cat(x = rmd[[3]][["contents"]], sep = "\n")
```
# Results
```{r, 'results', echo = FALSE, results = 'asis'}
cat(x = rmd[[4]][["contents"]], sep = "\n")
```
# Discussion
```{r, 'discussion', echo = FALSE, results = 'asis'}
cat(x = rmd[[5]][["contents"]], sep = "\n")
```
# Conclusion
```{r, 'conclusion', echo = FALSE, results = 'asis'}
cat(x = rmd[[6]][["contents"]], sep = "\n")
```
# References
上述文档是main.Rmd
的编织版本,即main.md
。请注意,在我的子文档04 results.Rmd
中的##Result 1
下,我获取了一个外部R脚本/stats/analysis.R
,它现在作为新的knitr块合并到我的针织文档中;因此,我现在需要再次编写该文件
当子文档也包含块时,我会将主文档编织成另一个.Rmd
,而不是编织成.md
,次数与嵌套块的次数相同,例如,继续上面的示例:
knit(input='./main.Rmd',output='./main_2.Rmd')
,而不是将main.Rmd
编织到main.md
中,我将它编织到另一个.Rmd中,以便能够编织包含新导入块的结果文件,例如上面的R脚本analysis.R
main_2.Rmd
编织成main.md
或通过rmarkdown::render(输入='./main_2.Rmd',输出文件='./main.html')
将其呈现为main.html
main.md
示例中,我的R脚本的路径是。/stats/analysis.R
。这是相对于源文件的子文档的路径,/sections/04 results.Rmd
。一旦我将子文档导入位于my_目录
根目录下的主文档,即/main.md
或/main_2.Rmd
,路径就会出错;因此,我必须在下次编织之前手动将其更正为/stats/analysis.R
我在上面提到过,最好按照导入主文档的顺序保存子文档。这是因为我的简单函数extraction();考虑:
> str(rmd)
List of 6
$ :List of 4
..$ title : chr "child doc 1"
..$ layout : chr "default"
..$ etc : chr "etc"
..$ contents: chr "\nThis is **Child Doc 1**, my abstract."
$ :List of 4
..$ title : chr "child doc 2"
..$ layout : chr "default"
..$ etc : chr "etc"
..$ contents: chr "\nThis is **Child Doc 2**, my introduction.\n\n- Point 1\n- Point 2\n- Point *n*"
$ :List of 4
..$ title : chr "child doc 3"
..$ layout : chr "default"
..$ etc : chr "etc"
..$ contents: chr "\nThis is **Child Doc 3**, my \"Methods\" section.\n\n| method 1 | method 2 | method *n* |\n|--------------|--------------|----"| __truncated__
$ :List of 4
..$ title : chr "child doc 4"
..$ layout : chr "default"
..$ etc : chr "etc"
..$ contents: chr "\nThis is **Child Doc 4**, my \"Results\" section.\n\n## Result 1\n\n```{r}\nlibrary(knitr)\n```\n\n```{r, cache = FALSE}\nsour"| __truncated__
$ :List of 4
..$ title : chr "child doc 5"
..$ layout : chr "default"
..$ etc : chr "etc"
..$ contents: chr "\nThis is **Child Doc 5**, where the results are discussed."
$ :List of 4
..$ title : chr "child doc 6"
..$ layout : chr "default"
..$ etc : chr "etc"
..$ contents: chr "\nThis is **Child Doc 6**, where I state my conclusions."
< > >代码> ExcActuple()/代码>这里实际上存储了指定子文档的R标记内容,以及它们的YAML,以防您也有此用途(我自己做的)。如果将文件提交给@ iHuri:我将把它放在一个特征请求中,我可以把它看作是KNITR的下一个版本的特征请求,但这对你来说可能不值得。我的用例可能相当具体。谢谢你的回复,不太感谢。我不介意小的功能请求:)这已经在knitr的当前开发版本中实现,将来将出现在CRAN上的knitr v1.8中。