12

我目前正在使用 package 读取文件readr。这个想法是使用read_delim逐行读取以查找我的非结构化数据文件中的最大列。代码输出有parsing问题。我知道这些并将在导入后处理列类型。problems()有没有办法像往常一样关闭它options(warn)不起作用

i=1
max_col <- 0
options(warn = -1)
while(i != "stop")
{
  n_col<- ncol(read_delim("file.txt", n_max = 1, skip = i, delim="\t"))
  if(n_col > max_col) {
    max_col <- n_col
    print(max_col)
  }
  i <- i+1
  if(n_col==0) i<-"stop"
}
options(warn = 0) 

我试图抑制的控制台输出如下:

.See problems(...) for more details.
Warning: 11 parsing failures.
row      col   expected  actual
  1 1####4 valid date 1###8
4

2 回答 2

26

在 R 中,您可以在使用包时抑制三个主要的烦人事情:

  1. 消息suppressMessages(YOUR_FUNCTION)
  2. 警告suppressWarnings(YOUR_FUNCTION)
  3. 包启动消息suppressPackageStartupMessages(YOUR_FUNCTION)

因此,在您的情况下,恕我直言,也让包开发人员知道,以便他/她可以verbose在函数中添加一个参数。

于 2016-12-22T14:25:47.030 回答
8

如果您将 rmd 'R Markdown' 与 RStudio 一起使用,则可以传入以下参数,这些参数将抑制警告消息以及列名称。

RMD 警告抑制

```{r warning = FALSE, message=FALSE}

HTH
AA

于 2018-07-27T22:10:28.973 回答