Custom pipe to silence warnings

后端 未结 4 494
孤城傲影
孤城傲影 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 04:39

    Coming back a little more experienced, I just missed an eval.parent and substitute combo, no need for rlang :

    `%W>%` <- function(lhs,rhs){
      w <- options()$warn
      on.exit(options(warn=w))
      options(warn=-1)
      eval.parent(substitute(lhs %>% rhs))
    }
    
    data.frame(a= c(1,-1)) %W>% mutate(a=sqrt(a)) %>% cos
    #           a
    # 1 0.5403023
    # 2       NaN
    

提交回复
热议问题