how to feed the result of a pipe chain (magrittr) to an object

后端 未结 5 559
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-31 19:02

This is a fairly simply question. But I couldn\'t find the answer per google/stackexchange and looking at the documentation of magrittr. How do you feed the result of a chain of

5条回答
  •  面向向阳花
    2021-01-31 19:12

    You can also use the <<- operator:

    data.frame( x = c(1:3), y = (4:6)) %>%
      sum() %>%
      `<<-`(a,.)
    

    Edit: I think John Paul's is the safest suggestion, and you could keep going with the chain doing different assignments of partial results. For example:

    data.frame( x = c(1:3), y = (4:6)) %>%  
      sum %>%  
      assign(x="a",value=., pos=1)  %>% 
      exp %>%
      assign(x="b",value=., pos=1) %>% 
      sqrt %>%
      assign(x="c", value=., pos=1)
    

    This will correctly create a, b and c.

提交回复
热议问题