I want to extract from an RNW all the chunks with R programming and put them in a separate file
I have a long rnw file and would find it tedious to do that manually.
Is there a function or a script to do that ?
I want to extract from an RNW all the chunks with R programming and put them in a separate file
I have a long rnw file and would find it tedious to do that manually.
Is there a function or a script to do that ?
You can perform some regex matching to retrieve all the code chunks as follows:
#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")