I can't work out how to pass a string to a function and then have that function use glue (or paste) to create a variable name which is then passed to dplyr::arrange()
Here are 16 failed attempts:
library(dplyr, warn.conflicts = FALSE)
arr1 <- function(df, var) {
df %>%
dplyr::arrange("{var}_color")
}
arr2 <- function(df, var) {
df %>%
dplyr::arrange(!!"{var}_color")
}
arr3 <- function(df, var) {
df %>%
dplyr::arrange(paste0(var, "_color"))
}
arr4 <- function(df, var) {
df %>%
dplyr::arrange({{ "{var}_color" }})
}
arr5 <- function(df, var) {
df %>%
dplyr::arrange(!!paste0(var, "_color"))
}
arr6 <- function(df, var) {
var <- rlang::as_label(paste0(var, "_color"))
df %>%
dplyr::arrange(var)
}
arr7 <- function(df, var) {
var <- rlang::ensym(paste0(var, "_color"))
df %>%
dplyr::arrange(!!var)
}
arr8 <- function(df, var) {
var <- rlang::ensym(paste0(var, "_color"))
df %>%
dplyr::arrange({{ var }})
}
arr9 <- function(df, var) {
var <- rlang::ensym("{var}_color")
df %>%
dplyr::arrange(!!var)
}
arr10 <- function(df, var) {
df %>%
dplyr::arrange("{var}_color")
}
arr11 <- function(df, var) {
df %>%
dplyr::arrange("{{ {var}_color }}")
}
arr12 <- function(df, var) {
var <- rlang::enquo(paste0(var, "_color"))
df %>%
dplyr::arrange(!!var)
}
arr13 <- function(df, var) {
var <- rlang::enquo(paste0(var, "_color"))
df %>%
dplyr::arrange({{ var }})
}
arr14 <- function(df, var) {
var <- rlang::enquo("{var}_color")
df %>%
dplyr::arrange(!!var)
}
arr15 <- function(df, var) {
var <- as.name(paste0(var, "_color"))
df %>%
dplyr::arrange(!!var)
}
arr16 <- function(df, var) {
var <- as.name(paste0(var, "_color"))
df %>%
dplyr::arrange({{ var }})
}
sw_select <- starwars %>%
head(3) %>%
dplyr::select(name:eye_color)
sw_select %>%
arr1("eye")
#> # A tibble: 3 x 6
#> name height mass hair_color skin_color eye_color
#> <chr> <int> <dbl> <chr> <chr> <chr>
#> 1 Luke Skywalker 172 77 blond fair blue
#> 2 C-3PO 167 75 <NA> gold yellow
#> 3 R2-D2 96 32 <NA> white, blue red
sw_select %>%
arr2("eye")
#> # A tibble: 3 x 6
#> name height mass hair_color skin_color eye_color
#> <chr> <int> <dbl> <chr> <chr> <chr>
#> 1 Luke Skywalker 172 77 blond fair blue
#> 2 C-3PO 167 75 <NA> gold yellow
#> 3 R2-D2 96 32 <NA> white, blue red
sw_select %>%
arr3("eye")
#> # A tibble: 3 x 6
#> name height mass hair_color skin_color eye_color
#> <chr> <int> <dbl> <chr> <chr> <chr>
#> 1 Luke Skywalker 172 77 blond fair blue
#> 2 C-3PO 167 75 <NA> gold yellow
#> 3 R2-D2 96 32 <NA> white, blue red
sw_select %>%
arr4("eye")
#> Error: `arg` must be a symbol
sw_select %>%
arr5("eye")
#> # A tibble: 3 x 6
#> name height mass hair_color skin_color eye_color
#> <chr> <int> <dbl> <chr> <chr> <chr>
#> 1 Luke Skywalker 172 77 blond fair blue
#> 2 C-3PO 167 75 <NA> gold yellow
#> 3 R2-D2 96 32 <NA> white, blue red
sw_select %>%
arr6("eye")
#> # A tibble: 3 x 6
#> name height mass hair_color skin_color eye_color
#> <chr> <int> <dbl> <chr> <chr> <chr>
#> 1 Luke Skywalker 172 77 blond fair blue
#> 2 C-3PO 167 75 <NA> gold yellow
#> 3 R2-D2 96 32 <NA> white, blue red
sw_select %>%
arr7("eye")
#> Error: `arg` must be a symbol
sw_select %>%
arr8("eye")
#> Error: `arg` must be a symbol
sw_select %>%
arr9("eye")
#> Error: `arg` must be a symbol
sw_select %>%
arr10("eye")
#> # A tibble: 3 x 6
#> name height mass hair_color skin_color eye_color
#> <chr> <int> <dbl> <chr> <chr> <chr>
#> 1 Luke Skywalker 172 77 blond fair blue
#> 2 C-3PO 167 75 <NA> gold yellow
#> 3 R2-D2 96 32 <NA> white, blue red
sw_select %>%
arr11("eye")
#> # A tibble: 3 x 6
#> name height mass hair_color skin_color eye_color
#> <chr> <int> <dbl> <chr> <chr> <chr>
#> 1 Luke Skywalker 172 77 blond fair blue
#> 2 C-3PO 167 75 <NA> gold yellow
#> 3 R2-D2 96 32 <NA> white, blue red
sw_select %>%
arr12("eye")
#> Error: `arg` must be a symbol
sw_select %>%
arr13("eye")
#> Error: `arg` must be a symbol
sw_select %>%
arr14("eye")
#> Error: `arg` must be a symbol
sw_select %>%
arr15("eye")
#> # A tibble: 3 x 6
#> name height mass hair_color skin_color eye_color
#> <chr> <int> <dbl> <chr> <chr> <chr>
#> 1 Luke Skywalker 172 77 blond fair blue
#> 2 R2-D2 96 32 <NA> white, blue red
#> 3 C-3PO 167 75 <NA> gold yellow
sw_select %>%
arr16("eye")
#> # A tibble: 3 x 6
#> name height mass hair_color skin_color eye_color
#> <chr> <int> <dbl> <chr> <chr> <chr>
#> 1 Luke Skywalker 172 77 blond fair blue
#> 2 R2-D2 96 32 <NA> white, blue red
#> 3 C-3PO 167 75 <NA> gold yellow
Created on 2021-10-12 by the reprex package (v2.0.1)