希望@Yihui能够比我提出的解决方案更优雅地回答你的问题。 :)
我之前在这里进行了一次hack
如何在R Markdown文件中包含HTML文件?。这种方法也适用于您的要求。
bookstruct.Rmd文件:
---
title: "BookTitle"
output: html_document
---
My introduction goes here
<<insertHTML:[chapters/chapter2.Rmd]
some practice questions
<<insertHTML:[chapters/chapter3.Rmd]
chapters/chapter2.Rmd 文件:
This is my chapter 2.
chapters/chapter3.Rmd 文件:
This is my chapter 3.
在 R 控制台中运行以下命令:
library(stringi)
subRender <- function(mdfile, flist) {
rmdlines <- readLines(mdfile)
toSubcode <- paste0("<<insertHTML:[",flist,"]")
locations <- sapply(toSubcode, function(xs) which(stri_detect_fixed(rmdlines, xs)))
subfiles <- lapply(flist, function(f) stri_trim(readLines(f)))
strlens <- sapply(subfiles,length)
newRmdfile <- tempfile("temp", getwd(), ".Rmd")
alllines <- c(rmdlines[-locations], unlist(subfiles))
ind <- c( (1:length(rmdlines))[-locations],
unlist(lapply(1:length(locations), function(n) locations[n] + seq(0, 1, len=strlens[n])/1.1 )) )
sortedlines <- alllines[order(ind)]
write(sortedlines, newRmdfile)
rmarkdown::render(newRmdfile, "html_document")
shell(gsub(".Rmd",".html",basename(newRmdfile),fixed=T))
}
subRender(mdfile="bookstruct.Rmd",
flist=list("chapters/chapter2.Rmd", "chapters/chapter3.Rmd"))
[2] 如何向向量中插入元素?
rmd
中使用first.Rmd
和second.Rmd
完全正常。 - CL.