在R中打印消息并询问用户输入

时间:2017-09-13 16:45:06

标签: r r-markdown

我正在使用rmarkdown创建一个html报告,需要检查某些条件下的输入数据集。如果满足条件,我需要向控制台打印一条消息,并询问用户是否要继续。以下是我的rmarkdown文件中的代码块作为可重现的示例:

var threshold = 1;

function isAdjacent (id1, id2) {
    var s = Snap("#mysvg");
    var first = s.select('#' + id1);
    var second = s.select('#' + id2);

    var len = first.getTotalLength(first);

    var p1, p2;
    for (var at = 0; at <= len; at += threshold) {
        p1 = first.getPointAtLength(at);
        if ( Snap.closestPoint(second, p1.x, p1.y).distance <= threshold) {
            return true;
        }
    }
    return false;
}

我希望将以下内容打印到控制台:

```{r, echo = FALSE}
data <- as.data.frame(matrix(c("A", "B", "C", "no", "no", "no"), nrow = 3, ncol = 2))
names(data) <- c("Name", "Include")

if(nrow(data) > 0 ){
    message("Following are names to not include:\n",
       paste0(capture.output(data[c("Name", "Include")]), collapse = "\n"))

   continue <- readline(prompt = "Do you wish to continue? Type y or n and press enter:")

}
```

当我在rmarkdown文件之外执行此操作时,所需的消息和问题将打印到控制台。但是,当我在rmarkdown文件中执行此操作时,只有

Following are names to not include:   
  Name   Include   
1    A        no   
2    B        no
3    C        no
Do you wish to continue? Type y or n and press enter: 

打印到控制台。

0 个答案:

没有答案