In RStudio the shortcut cmd-shift-a exists to add spaces before and after operators (e.g. +, =, -, %>%,...).
I wonder whether it would be possible to reformat the tidyverse code working with the pipe (as well as the + for ggplot2), such that one not only adds spaces but also new lines where commonly added (e.g after %>%
)
Such that:
rec_prep <- rec %>% step_range(all_outcomes(), max = 10) %>% prep()
turns into
rec_prep <- rec %>%
step_range(all_outcomes(), max = 10) %>%
prep()
My thoughts on how it could work:
Remark: whenever I say should this is of course just how I do it. sometimes more widely shared sometimes not. the proper formatting guidelines i guess would need to be worked out (in case they haven't already...)
I guess one needs to distinguish between context of operators as a +
in ggplot2 (ggplot() + geom_point()
) is different to a +
in a simple addition of vectors (c(1,2, 3) + c(1,2,3)
).
Maybe classes of operators are a way to distinguish. Logical operations (a >= 3
or "a" %in% c("a", "b")
) should remain on the same line, whereas piping should be followed by a line break.
Lastly, commas as in mutate(a = fun(...), b = fun(...)
should be broken into
mutate(a = fun(...),
b = fun(...)) # %>%
I figure, such a formatting command would make a great addition to RStudio and all it's handy commands.