我想知道是否有一种自动化的方法可以使注释不会打印在输出中。现在,我正在使用以下内容来省略评论:
<<relimpsum, echo=-c(1,3,4,5)>>=
## load relimp package
library(relimp)
## calculate relative improtance of
## sector (coefs 3:11) and
## nation (coefs 12:14)
relimp(mod1, set1=3:11, set2=12:14)
@
我想知道是否有一个现有的选项可以删除所有评论或将 grep 传递给 echo 命令的能力,例如,echo=-function(x)grep(“^##”, x)。我知道特定解决方案不起作用的事实,但我想知道是否可能出现类似的情况?
此外,目前看来,如果我定义一个函数和函数的注释方面,整个函数算作一个单一的表达式,它(及其注释)可以被打印,或者它(及其所有注释)可以被抑制,但似乎无法抑制函数中的注释。例如:
<<withboot, echo=-c(1,3,5,6)>>=
## Bootstrapping the median with the boot package
library(boot)
## set random number generator
set.seed(123)
## define function that we will bootstrap
med.fun <- function(x, inds){
## assigns .inds to the global environment to
## ensure that the appropriate obs numbers get
## used in the resampling
assign(".inds", inds, envir=.GlobalEnv)
## calculate the median for the resampled x values
med <- median(x[.inds])
## remove the .inds from the global environment
remove(".inds", envir=.GlobalEnv)
## return the bootstrapped median
med
}
## use the boot function to botstrap the median
boot.med <- boot(x, med.fun, R=1000)
boot.ci(boot.med)
@
echo 语句中的 6 排除了所有内容
med.fun <-
...
}
这是唯一的行为还是函数内注释也可以被抑制?
最接近的解决方案(可能不是您想要的)是设置块选项,tidy = TRUE, tidy.opts = list(comment = FALSE)
以便使用formatR包来格式化您的代码并删除所有注释。如果要全局开启此功能,可以knitr::opts_chunk$set(tidy = TRUE, tidy.opts = list(comment = FALSE))
在文档的第一个代码块中进行设置。
您可能不喜欢tidy = TRUE
,但它比您提出的可能解决方案更可靠(如果您想处理 R 源代码,正则表达式并不健壮)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句