从Knitr输出中的打印中删除注释

时间:2017-06-23 17:37:18

标签: r knitr

我想知道是否有一种自动化的方法来使它不会在输出中打印注释。现在,我正在使用以下内容来省略注释:

<<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)
@

我想知道是否存在可以取出所有注释的现有选项或将grep传递给echo命令的能力,例如echo = -function(x)grep(“^ ##”,x)。我知道特定的解决方案不起作用的事实,但我想知道是否可能有类似的东西?

此外,似乎目前如果我定义函数的一个函数和注释方面,整个函数计为一个单独的表达式,它可以打印它(及其注释)或它(及其所有注释)可以被抑制,但似乎无法抑制函数内的注释。例如:

med.fun <- 
... 
}

echo语句中的6不包括

中的所有内容
<script>
    uniqueString = "";

    alert("Displays the number of a specific character in user entered string and then finds the number of unique characters:");

    function countChar(testString, lookFor) {
        var charCounter = 0;
        document.write("Looking at this string:<br>");

        for (pos = 0; pos < testString.length; pos++) {
            if (testString.charAt(pos) == lookFor) {
                charCounter += 1;
                document.write("<B>" + lookFor + "</B>");
            } else
                document.write(testString.charAt(pos));
        }
        document.write("<br><br>");
        return charCounter;
    }

    function findNumberOfUniqueChar(testString) {
        var numChar = 0,
            uniqueChar = 0;
        for (pos = 0; pos < testString.length; pos++) {
            var newLookFor = "";
            for (pos2 = 0; pos2 <= pos; pos2++) {
                if (testString.charAt(pos) == testString.charAt(pos2)) {
                    numChar += 1;
                }
            }
            if (numChar == 1) {
                uniqueChar += 1;
                uniqueString = uniqueString + " " + testString.charAt(pos)
            }
            numChar = 0;
        }
        return uniqueChar;
    }

    var testString = prompt("Give me a string of characters to check", "");
    var lookFor = "startvalue";
    while (lookFor.length > 1) {
        if (lookFor != "startvalue")
            alert("Please select only one character");
        lookFor = prompt(testString + "\n\nWhat should character should I look for?", "");
    }
    document.write("I found " + countChar(testString, lookFor) + " of the<b> " + lookFor + "</B> character");
    document.write("<br><br>I counted the following " + findNumberOfUniqueChar(testString) + " unique character(s):");
    document.write("<br>" + uniqueString)
</script>

这是唯一的行为,还是可以抑制功能内注释?

1 个答案:

答案 0 :(得分:0)

最接近的解决方案(可能不是您想要的)是设置块选项tidy = TRUE, tidy.opts = list(comment = FALSE),以便formatR包用于格式化代码并删除所有注释。如果要全局启用此功能,可以在文档的第一个代码块中设置knitr::opts_chunk$set(tidy = TRUE, tidy.opts = list(comment = FALSE))

您可能不喜欢tidy = TRUE,但它比您提出的可能解决方案更可靠(如果您想处理R源代码,正则表达式不健壮。)