将Knitr块保存到R文件 [英] Save knitr chunk to R file
本文介绍了将Knitr块保存到R文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在knitr中,有一个read_chunk
函数将外部代码读入块中.是否可以逆转该过程.也就是说,创建一个函数write_chunk()
来将源代码块中的源代码保存到R文件中?文件名可以与块名相同.
In knitr there is a read_chunk
function which read external code into a chunk. Is it possible to reverse the process. That is, make a function write_chunk()
which save the source code in the chunk to an R file? The file name may the same as the chunk name.
推荐答案
我找到了使用钩子的解决方案.添加以下钩子:
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)
}
})
并在块标题中使用选项<<chunk-name, write_chunk=TRUE>>
.
and use option <<chunk-name, write_chunk=TRUE>>
in the header of a chunk.
这篇关于将Knitr块保存到R文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文