如何将.rnw转换为R脚本

  • 本文关键字:脚本 转换 rnw r rnw
  • 更新时间 :
  • 英文 :


我想从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")

最新更新