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 ?
c("1", "2", "ABC") %>% {suppressWarnings(as.numeric(.))}
suppressWarnings
around the full chain of calls.try
(to capture errors)warning = FALSE
to the chunk options.