在r中抑制读者解析问题

Han*_*aal 10 r readr

我目前正在阅读使用该软件包的文件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) 
Run Code Online (Sandbox Code Playgroud)

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

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

Meh*_*ian 22

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

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

所以在你的情况下,imho也让包开发者知道,以便他/她可以verbose在函数中添加一个参数.


Amo*_*mod 7

如果你在RStudio中使用rmd'R Markdown',你可以传入以下参数来抑制警告消息以及列名.

RMD警告抑制

```{r warning = FALSE, message=FALSE}
Run Code Online (Sandbox Code Playgroud)

HTH
AA