Something for the more advanced R user! We’ll be back to our more exciting programming shortly (I hope!).
rlang? I already speak R
Quite right. rlang is part of the tidyverse side of things, so is probably more useful if you’re an advanced R user. It’s certainly not for the faint-hearted and needs a comprehensive understanding of how R ‘sees’ the code you write.
rlang is a low-level programming API for R which the tidyverse uses (meaning it speaks to R in as R like way as possible, rather than a ‘high-level’ – high level is more user orientated and interpretable). It enables you to extend what the tidyverse can do and adapt it for your own uses. It’s particularly good to use if you’re doing lots of more ‘programming’ type R work, for example, building a package, making a complex shiny app or writing functions. It might also be handy if you’re doing lots of big data manipulation and want to manipulate different datasets in the same way, for example.
Here’s an example of dynamically naming variables
In this example, say we have a tibble of variables, but we want to apply dynamic changes to it (so we feed R a variable, that can change, either using another function like purr::map
or in a ShinyApp). In this instance, specifying each variable and each different possible consequence using different logical functions would take forever and be very clunky. So we can use rlang to simply put a dynamic variable/object through the same function.
We make use of the curly curlys too, which allow us to avoid using bulky enquo() – !! syntax
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
library(tidyverse) #Make data - here nonsense numbers on deaths per 100k from green spacemen say example_data = tibble(countries = c('UK', 'USA', 'Pakistan', 'Mexico', 'Ireland', 'Estonia'), region = c('Europe', 'Americas', 'Asia', 'Americas', 'Europe', 'Europe'), Death_from_green_spacemen = c(1, 200, 150, 450, 3, 3.5), Death_from_mermaids = c(100, 300, 140, 150, 120, 300)) #example function for summarising using dynamic variables and bang bangs #note metric must be numeric summarise_feature = function(df, col_var, ...){ require(tidyverse) df %>% group_by(...) %>% summarise({{col_var}} := sum({{col_var}})) } #output new variable example_data %>% summarise_feature(Death_from_green_spacemen, region) #Metric can be provided by a shiny input too i.e. UQ(rlang::sym(input$metric)) #How to dynamically change names using mutate and rlang #Bang bang variables into mutate/tidyverse(so they can be dynamically changed) #Select metric metric = 'metric_Death_from_green_spacemen' #example but can be any named column metric_mutate = paste0('prefix_', metric) #some reason doesn't take these within the mutate function so has to be outside example_data %>% summarise_feature(Death_from_green_spacemen, region) %>% mutate(!!metric_mutate := !!rlang::sym(metric) * 2)#apply arbitary multiplication by two |
This is great Tom.
I wonder whether there is any plan for a “name” solution to curly-curly? Come across anything?