Search code examples
rtidyversemagrittr

Can the use of return() after a magrittr pipeline to write an object break something?


I often have to manually clean up entries in datasets. For flexibility and readability, I like to use pipes. Sometimes I later come across another thing I need to clean up, so I keep a copy-paste'able line in my magrittr pipeline.

In ggplot2, calling an empty theme() at the end helps me to keep coding flexible for later additions. I never encountered problems with that and thought I could do the same with return() in a pipeline.

I think return() is not be meant to use outside functions, so: is there a conceivable way this could break my code?

I also stumbled upon {.} ans an alternative, but I don't really know what it does and searching info (even using advanced search on SO) is not helping.

Example:

starwars %<>% 
  mutate(hair_color = ifelse(name == "Captain Phasma", "blond", hair_color)) %>%
  mutate(skin_color = ifelse(name == "Captain Phasma", "fair", hair_color)) %>%
  mutate(hair_color = ifelse(name == "Zam Wesell", "blond", hair_color)) %>%
  #mutate(var = ifelse(name == "cond", "replacement", var)) %>% ### for future c/p
  return() #  

NB: I realise this is borderline the tag "coding-style", so I like point out I'm not interested in an opinion-based discussion but advice if this could break my code in certain circumstances. Examples where it does break code are welcome, as are alternative suggestions.

I think these topics/threads are related:


Solution

  • As put forward by @Roland and @RolandASc: ?identity() is doing what I wanted. I am using it since, and haven't encountered surprises so far.

    Further related discussion found over here at the RStudio community.