Search code examples
rdplyrpipeline

Assign intermediate output to temp variable as part of dplyr pipeline


Q: In an R dplyr pipeline, how can I assign some intermediate output to a temp variable for use further down the pipeline?

My approach below works. But it assigns into the global frame, which is undesirable. There has to be a better way, right? I figured my approach involving the commented line would get the desired results. No dice. Confused why that didn't work.

df <- data.frame(a = LETTERS[1:3], b=1:3)
df %>%
  filter(b < 3) %>%
  assign("tmp", ., envir = .GlobalEnv) %>% # works
  #assign("tmp", .) %>% # doesn't work
  mutate(b = b*2) %>%
  bind_rows(tmp)
  a b
1 A 2
2 B 4
3 A 1
4 B 2

Solution

  • This does not create an object in the global environment:

    df %>% 
       filter(b < 3) %>% 
       { 
         { . -> tmp } %>% 
         mutate(b = b*2) %>% 
         bind_rows(tmp) 
       }
    

    This can also be used for debugging if you use . ->> tmp instead of . -> tmp or insert this into the pipeline:

    { browser(); . } %>%