0

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:

aae
  • 439
  • 7
  • 18
  • 1
    Well, it might break as soon as the first example in issue 32 returns a different result, i.e., the issue gets fixed. I believe `{.}` just calls the `{` function. You could probably also use `identity` instead. – Roland Mar 08 '19 at 09:44
  • 1
    for your purpose, `identity` indeed is the appropriate function rather than `return` – RolandASc Mar 08 '19 at 10:01

1 Answers1

0

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.

aae
  • 439
  • 7
  • 18