我想从rnw中提取所有用R编程的块,然后将它们放在单独的文件中
我有一个很长的rnw文件,会发现手动执行此操作很乏味。
是否有函数或脚本可以这样做?
您可以执行一些正则匹配以检索所有代码块,如下所示:
#read my Rnw file
l <- readLines("myRnw.Rnw")
#find starting and ending lines of my code chunks
startIdx <- which(grepl("^<<", l))
endIdx <- which(grepl("^@$", l))
#extract all code chunks and save to a file
writeLines(unlist(Map(function(st, ed) l[(st+1):(ed-1)], startIdx, endIdx)),
"myRcode.R")