【发布时间】:2017-06-23 17:37:18
【问题描述】:
我想知道是否有一种自动化的方法可以使 cmets 不会在输出中打印。现在,我使用以下代码来省略 cmets:
<<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)
@
我想知道是否有一个现有的选项可以取出所有 cmets 或将 grep 传递给 echo 命令的能力,例如 echo=-function(x)grep(“^##”, x)。我知道一个特定的解决方案不起作用,但我想知道类似的事情是否可能?
此外,目前看来,如果我定义一个函数并注释该函数的各个方面,则整个函数都算作一个表达式,并且可以打印它(及其 cmets)或者它(及其所有 cmets)可以抑制,但似乎无法抑制函数内的 cmets。例如:
<<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 <-
...
}
这是唯一的行为还是可以抑制功能中的 cmets?
【问题讨论】: