I am trying to suppress warnings by using the suppressWarnings()
function.
Surprisingly, it removes warnings when used normally, but it fails to do so when you use the pipe %>%
operator.
Here is some example code :
library(magrittr)
c("1", "2", "ABC") %>% as.numeric()
# [1] 1 2 NA
# Warning message:
# In function_list[[k]](value) : NAs introduced by coercion
c("1", "2", "ABC") %>% as.numeric() %>% suppressWarnings
# [1] 1 2 NA
# Warning message:
# In function_list[[i]](value) : NAs introduced by coercion
suppressWarnings(c("1", "2", "ABC") %>% as.numeric())
# [1] 1 2 NA
Why does it work with parenthesis but not with pipe operator ? Is there a specific syntax I should use to make it work ?
One solution would be to use the
%T>%
pipes to modify options (frommagrittr
, not included indplyr
!)You could also use
purrr::quietly
, not so pretty in this case...for the sake of completeness, here are also @docendo-discimus 's solution and OP's own workaround
And I'm stealing @Benjamin's comment as to why the original try doesn't work :
EDIT:
The linked solution will allow you to just write
c("1", "2", "ABC") %W>% as.numeric
Custom pipe to silence warnings