I was wondering if you can map/chain tidyverse functions together without explicitly needing to write them, but just change their input. Take this as an example:
myData = data.frame(x = 1:10, y = runif(10), z = LETTERS[1:10])
myData %>% mutate(A = 1) %>% mutate(B = 1) %>% mutate(C = 1)
x y z A B C
1 1 0.2343784 A 1 1 1
2 2 0.9754685 B 1 1 1
3 3 0.5395749 C 1 1 1
4 4 0.9732300 D 1 1 1
5 5 0.3909175 E 1 1 1
6 6 0.9226956 F 1 1 1
7 7 0.1892913 G 1 1 1
8 8 0.2193157 H 1 1 1
9 9 0.7612691 I 1 1 1
10 10 0.2245095 J 1 1 1
I know you can do this much easier all within the same mutate() but this is a dummy example where the function can be much more complex taking different data / inputs like for example different layers in a leaflet or ggplot.
I can implement this with the eval() function in R like this:
However, I have a feeling that you've simplified your problem too much and this solution won't generalize to what you are actually doing. So maybe think of a more involved example that showcases your problem a bit more?
@mishabalyasin : Thanks already for the great reply! I haven't figured out the full syntax yet, but it seems very clean and interesting.
I agree I should have come up with a more real world problem, so here it is:
library(dplyr)
library(leaflet)
m <- leaflet() %>% setView(lng = -71.0589, lat = 42.3601, zoom = 12)
m %>% addTiles() %>%
addCircles(-71.0589, 42.3601, 1000, color = "green", group = "group1") %>%
addCircles(-71.0200, 42.3601, 800, color = "purple", group = "group2") %>%
addCircles(-71.0869, 42.3206, 650, color = "blue", group = "group3") %>%
addLayersControl(
overlayGroups = c("group1", "group2","group3"),
options = layersControlOptions(collapsed = FALSE)
)
You see how I need to repeat the addCircles several times (example only 3, but could be more, or based on dataset). I would love to be able to not have to write them out and just use a smart mapping function...
Again, I might be missing something, but I am confronted with things like this from time to time and just wonder how I could make it more efficient.
I think this looks very cool! It was more of a theoretical question anyway to see how to make code more flexible and avoid repetition and you did a great job!