Custom pipe to silence warnings

后端 未结 4 491
孤城傲影
孤城傲影 2020-12-30 04:28

Related to this question.

I\'d like to build a custom pipe %W>% that would silence warnings for one operation

library(magrittr)
data.         


        
4条回答
  •  甜味超标
    2020-12-30 05:03

    I'm not sure this solution works perfectly, but it's a start:

    `%W>%` <- function(lhs, rhs) {
      call <- substitute(`%>%`(lhs, rhs))
      eval(withr::with_options(c("warn" = -1), eval(call)), parent.frame())
    }
    

    This seems to work for the following 2 examples:

    > data.frame(a= c(1,-1)) %W>% mutate(a=sqrt(a)) %>% cos
              a
    1 0.5403023
    2       NaN
    > c(1,-1) %W>% sqrt()
    [1]   1 NaN
    

提交回复
热议问题