16

I am currently reading in a file using the package readr. The idea is to use read_delim to read in row for row to find the maximum columns in my unstructured data file. The code outputs that there are parsing problems. I know of these and will deal with column type after import. Is there a way to turn off the problems() as the usual options(warn) is not working

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) 

The output to console that I am trying to suppress is the following:

.See problems(...) for more details.
Warning: 11 parsing failures.
row      col   expected  actual
  1 1####4 valid date 1###8
Hanjo Odendaal
  • 1,395
  • 2
  • 13
  • 32
  • I've the feeling you should fix the file out of R before importing it. Maybe awk is better suited for the task ? – Tensibai May 26 '16 at 13:52
  • `suppressWarnings(x <- readr::parse_integer(c("1X", "blah", "3")))` seems to work... – cory May 26 '16 at 13:53
  • 2
    Probably better to open a `file` connection, read the file a line at a time and count the separators? `max(sapply(readLines("file.txt"),function(x){length(strsplit(x,"\t")[[1]])}))` – Spacedman May 26 '16 at 13:55

2 Answers2

30

In R you can suppress three main annoying things while using packages:

  1. messages suppressMessages(YOUR_FUNCTION)
  2. warnings suppressWarnings(YOUR_FUNCTION)
  3. package startup messages suppressPackageStartupMessages(YOUR_FUNCTION)

So in your case imho also let the package developer knows so that he/she can for example add a verbose argument in the function.

Mehrad Mahmoudian
  • 3,466
  • 32
  • 36
8

if you are using rmd 'R Markdown' with RStudio, you can pass in the following arguments which will suppress the warning messages as well as the columns names.

RMD warning suppress

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

HTH
AA

Amod
  • 636
  • 1
  • 8
  • 9
  • or in your knitr options with knitr::opts_chunk$set( message = FALSE, warning = FALSE ) – Mike Nov 12 '19 at 20:37