dplyr - R Conditional evaluation when using the pipe operator %>% -
when using pipe operator %>% packages such dplyr, ggvis, dycharts, etc, how do step conditionally? example;
step_1 %>% step_2 %>% if(condition) step_3 these approaches don't seem work:
step_1 %>% step_2 if(condition) %>% step_3 step_1 %>% step_2 %>% if(condition) step_3 there long way:
if(condition) { step_1 %>% step_2 }else{ step_1 %>% step_2 %>% step_3 } is there better way without redundancy? working dygraphs if matters.
here quick example take advantage of . , ifelse;
x<-1 y<-t x %>% add(1) %>% { ifelse(y ,add(1), . ) } in ifelse if y true if add 1, otherwise return last value of x. . stand-in tells function output previous step of chain goes, can use on both branches.
edit @benbolker pointed out, might not want ifelse, here if version.
x %>% add(1) %>% {if(y) add(1) else .} thanks @frank pointing out should use { braces around if , ifelse statements continue chain.
Comments
Post a Comment