简体   繁体   中英

Wrapper/adverb to suppress messages in R

A lot of tidyverse functions are very verbose with their messages, leading to logs that are full of stuff like

New names:
* `` -> ...1
* `` -> ...2
* `` -> ...3
* `` -> ...4
* `` -> ...5

repeated a zillion times. This makes reviewing actual warning messages very difficult.

Is there a drop-in (a) wrapper function that will suppress messages generated from code run inside of it or (b) an adverb that turns a function into one that doesn't generate messages? I'm hoping for a simple modification I can make for my code once it's stable and I don't need to look at all the messages from a particularly verbose line. purrr::quietly is almost what I want, but because it returns the original output as part of a list it's not truly a drop-in replacement or wrapper for an unmodified line of code.

suppressMessages() does what you are looking for.

a <- data.frame(a=1,a=2, check.names = FALSE)

tibble::as_tibble(a, .name_repair = "universal")
#> New names:
#> * a -> a...1
#> * a -> a...2
#> # A tibble: 1 x 2
#>   a...1 a...2
#>   <dbl> <dbl>
#> 1     1     2

suppressMessages({
  tibble::as_tibble(a, .name_repair = "universal")
})
#> # A tibble: 1 x 2
#>   a...1 a...2
#>   <dbl> <dbl>
#> 1     1     2

It also works with the pipe %>% .

library(dplyr, warn.conflicts = FALSE) 
tibble::as_tibble(a, .name_repair = "universal") %>% 
  suppressMessages()
#> # A tibble: 1 x 2
#>   a...1 a...2
#>   <dbl> <dbl>
#> 1     1     2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM