0

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 ?

zx8754
  • 52,746
  • 12
  • 114
  • 209

1 Answers1

-1

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")
chinsoon12
  • 25,005
  • 4
  • 25
  • 35