R: Using a string as an argument to change a verb in dplyr

I am creating a brilliant application that should allow users to define new variables for plotting. In particular, I want to allow users to define an expression that will be used in the mutate verb. The server receives the expression as text, and I wonder how to make the mutation execute it in dplyr 0.7. I can get it to work (partially) with mutate_, but now it is deprecated. It also defines the new column name as an integer expression, not a new variable

Here is an example of reproducibility:

input_from_shiny <- "Petal.ratio = Petal.Length/Petal.Width"
iris_mutated <- iris %>% mutate_(input_from_shiny)

This gives the following

> head(iris_mutated)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species Petal.ratio = Petal.Length/Petal.Width
1          5.1         3.5          1.4         0.2  setosa                                   7.00
2          4.9         3.0          1.4         0.2  setosa                                   7.00
3          4.7         3.2          1.3         0.2  setosa                                   6.50
4          4.6         3.1          1.5         0.2  setosa                                   7.50
5          5.0         3.6          1.4         0.2  setosa                                   7.00
6          5.4         3.9          1.7         0.4  setosa                                   4.25

, , , dplyr ( https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html, )

+4
1

rlang::parse_quosure() !! (bang bang), :

  • parse_quosure: quosure

  • !!: quosure, tidyeval

, parse_quosure() parse_quo() rlang 0.2.0 . parse_quo(), quosures, . parse_quo(input_from_shiny, env = caller_env())

library(rlang)
library(tidyverse)

input_from_shiny <- "Petal.ratio = Petal.Length/Petal.Width"
iris_mutated <- iris %>% mutate_(input_from_shiny)

iris_mutated2 <- iris %>% 
  mutate(!!parse_quosure(input_from_shiny))
head(iris_mutated2)

#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa
#>   Petal.ratio = Petal.Length/Petal.Width
#> 1                                   7.00
#> 2                                   7.00
#> 3                                   6.50
#> 4                                   7.50
#> 5                                   7.00
#> 6                                   4.25


identical(iris_mutated, iris_mutated2)
#> [1] TRUE

: LHS RHS

lhs <- "Petal.ratio"
rhs <- "Petal.Length/Petal.Width"

iris_mutated3 <- iris %>% 
  mutate(!!lhs := !!parse_quosure(rhs))
head(iris_mutated3)

> head(iris_mutated3)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
  Petal.ratio
1        7.00
2        7.00
3        6.50
4        7.50
5        7.00
6        4.25

2018-03-24 reprex (v0.2.0).

+5

Source: https://habr.com/ru/post/1695287/


All Articles