简体   繁体   English

在knitr缠结的R代码中缺少块名称

[英]missing chunk names in knitr tangled R code

When using Stangle on a Sweave document, the output includes a comment that gives the chunk name. Sweave文档上使用Stangle时,输出包含一个给出块名称的注释。 This is incredibly useful in a teaching setting to orient students in the code as they cut-and-paste. 这在教学环境中非常有用,可以让学生在剪切和粘贴时对代码进行定位。 However, when I use knitr to generate the .R code from a .Rnw , the chunk names are not included (at least that I can see): 但是,当我使用knitr.Rnw生成.R代码时,不包括块名称(至少我可以看到):

knit('example.Rnw',tangle=TRUE)

I could run Stangle on a document written for knitr except that there are incompatibilities in chunk arguments that preclude running Stangle without errors. 我可以在为knitr编写的文档上运行Stangle ,除了块参数中存在不兼容性以阻止运行Stangle而没有错误。

Is there a way to force knitr to produce comments in tangled R code that include chunk names (and/or numbers) like native Stangle ? 有没有办法强制knitr在纠结的R代码中产生注释,包括像本机Stangle一样的块名(和/或数字)?

Thanks to @kohske (I had just about figured this out for myself ...) 感谢@kohske(我刚刚为自己想出了这个......)

purl() has a documentation argument, which is set to 1 by default (1=add chunk headers to code; 0 does nothing, 2 adds chunk headers as roxygen comments). purl()有一个documentation参数,默认设置为1(1 =将chunk头添加到代码中; 0不执行任何操作,2添加块头作为roxygen注释)。 If you want to stick to knit(.,tangle=TRUE) you just need to opts_knit$set(documentation=1) before tangling. 如果你想坚持knit(.,tangle=TRUE) opts_knit$set(documentation=1) knit(.,tangle=TRUE)你只需要在纠缠之前选择opts_knit$set(documentation=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM