string_eval: Evaluate an interpolated string as symbol

Description Usage Arguments Value Examples

Description

This function takes a glue specifcation as input, and evaluates the final argument string as name in the caller environment.

Usage

1
.(x)

Arguments

x

A glue specification, that is, a string which contains an R expression wrapped in curly braces, e.g. ."{.x}_some_string".

Value

The values of the variable with the name of the final argument string, given that it exists in the caller environment.

Examples

library(dplyr)

# For better printing
iris <- as_tibble(iris)

Below is a simple example from over(). In over's function argument .x is first evaluated as 'Sepal' and then as 'Petal' which results in the final argument strings 'Sepal.Width' and 'Sepal.Length' as well as 'Petal.Width' and 'Petal.Length'.

iris %>%
  mutate(over(c("Sepal", "Petal"),
              ~ .("{.x}.Width") + .("{.x}.Length")
              ))
#> # A tibble: 150 x 7
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal Petal
#>          <dbl>       <dbl>        <dbl>       <dbl> <fct>   <dbl> <dbl>
#> 1          5.1         3.5          1.4         0.2 setosa    8.6   1.6
#> 2          4.9         3            1.4         0.2 setosa    7.9   1.6
#> 3          4.7         3.2          1.3         0.2 setosa    7.9   1.5
#> 4          4.6         3.1          1.5         0.2 setosa    7.7   1.7
#> # ... with 146 more rows

The above syntax is equal to the more verbose:

iris %>%
  mutate(over(c("Sepal", "Petal"),
              ~ eval(sym(paste0(.x, ".Width"))) +
                eval(sym(paste0(.x, ".Length")))
              ))
#> # A tibble: 150 x 7
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal Petal
#>          <dbl>       <dbl>        <dbl>       <dbl> <fct>   <dbl> <dbl>
#> 1          5.1         3.5          1.4         0.2 setosa    8.6   1.6
#> 2          4.9         3            1.4         0.2 setosa    7.9   1.6
#> 3          4.7         3.2          1.3         0.2 setosa    7.9   1.5
#> 4          4.6         3.1          1.5         0.2 setosa    7.7   1.7
#> # ... with 146 more rows

Although .() was created with the use of over() in mind, it can also be used within dplyr::across() in combination with dplyr::cur_column(). First let's rename 'Sepal.Length' and 'Petal.Length' to 'Sepal' and 'Petal' to have a stem to which we can attach the string '.Width' to access the two 'Width' variables. Now we can call .(cur_colunm()) to access the variable across() has been called on (Note: we could have used .x instead). We can further access the values of the 'Width' variables by wrapping cur_column() in curly braces {}, adding .Width and wrapping everything with quotation marks .("{cur_column()}.Width").

iris %>%
  rename("Sepal" = "Sepal.Length",
         "Petal" = "Petal.Length") %>%
   mutate(across(c(Sepal, Petal),
                 ~ .(cur_column()) + .("{cur_column()}.Width"),
                 .names = "{col}_sum"))
#> # A tibble: 150 x 7
#>   Sepal Sepal.Width Petal Petal.Width Species Sepal_sum Petal_sum
#>   <dbl>       <dbl> <dbl>       <dbl> <fct>       <dbl>     <dbl>
#> 1   5.1         3.5   1.4         0.2 setosa        8.6       1.6
#> 2   4.9         3     1.4         0.2 setosa        7.9       1.6
#> 3   4.7         3.2   1.3         0.2 setosa        7.9       1.5
#> 4   4.6         3.1   1.5         0.2 setosa        7.7       1.7
#> # ... with 146 more rows

A similar approach can be achieved using purrr::map in combination with .():

iris %>%
  rename("Sepal" = "Sepal.Length",
         "Petal" = "Petal.Length") %>%
  mutate(purrr::map_dfc(c("Sepal_sum" = "Sepal", "Petal_sum" = "Petal"),
                        ~ .(.x) + .("{.x}.Width")))
#> # A tibble: 150 x 7
#>   Sepal Sepal.Width Petal Petal.Width Species Sepal_sum Petal_sum
#>   <dbl>       <dbl> <dbl>       <dbl> <fct>       <dbl>     <dbl>
#> 1   5.1         3.5   1.4         0.2 setosa        8.6       1.6
#> 2   4.9         3     1.4         0.2 setosa        7.9       1.6
#> 3   4.7         3.2   1.3         0.2 setosa        7.9       1.5
#> 4   4.6         3.1   1.5         0.2 setosa        7.7       1.7
#> # ... with 146 more rows

TimTeaFan/dplyover documentation built on Sept. 27, 2021, 3:14 p.m.