Save knitr chunk to R file

半城伤御伤魂 提交于 2019-12-05 13:06:20

I found a solution using hooks. Add the following hook:

knit_hooks$set(write_chunk = function(before, options, envir) {
    if (before) {
      fileConn<-file(paste0("chunk_",options$label,".R") )
      writeLines(options$code, fileConn)
      close(fileConn)
    }
})

and use option <<chunk-name, write_chunk=TRUE>> in the header of a chunk.

You can use the following syntax

Stangle(file = "Your_code.Rnw",output="Code.R"):

But this I can prove the following error:

#Error: ‘Your_code.Rnw’ is not ASCII and does not declare an encoding

Adding the following parameter (encoding = "UTF-8"), the coding problem is solved

Stangle("Your_code.Rnw",output="Code.R",encoding="utf8")
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!