R:使用字符串作为参数来改变 dplyr 中的动词 [英] R: Using a string as an argument to mutate verb in dplyr

查看:25
本文介绍了R:使用字符串作为参数来改变 dplyr 中的动词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个闪亮的应用程序,它需要允许用户定义新的绘图变量.具体来说,我希望允许用户定义要在 mutate 动词中使用的表达式.服务器以文本形式接收表达式,我想知道如何让 mutate 在 dplyr 0.7 中执行它.我可以使用 mutate_ 使其(部分)工作,但现在已弃用.它还将新列名定义为整个表达式而不是新变量

I am building a shiny app which needs to allow users to define new variables for plotting. Specifically I want to allow users to define an expression to be used in mutate verb. The server receives the expression as text and I am wondering how to make mutate execute it in dplyr 0.7. I can make it work (partially) using mutate_ but it is deprecated now. It also defines the new column name as the entire expression rather than the new variable

这是一个可重复的例子:

Here is a reproducible example:

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

这给出了以下

> 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,但无法弄清楚)

Technically, I can use regular expression to extract new variable name from the string and rename the new column accordingly, but I am wondering what is the correct way to implement it using latest dplyr version (was reading https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html, but could not figure it out)

推荐答案

我们可以使用 rlang::parse_quosure()!! (bang bang) 来产生相同的结果:

We can use rlang::parse_quosure() together with !! (bang bang) to produce the same result:

  • parse_quosure:解析提供的字符串并将其转换为quosure

  • parse_quosure: parses the supplied string and converts it into a quosure

!!:取消引用一个 quosure 以便它可以被 tidyeval 动词评估

!!: unquotes a quosure so it can be evaluated by tidyeval verbs

请注意,parse_quosure()rlang 0.2.0 中根据其文档被软弃用并重命名为 parse_quo().如果我们使用 parse_quo(),我们需要为 quosures 指定环境,例如parse_quo(input_from_shiny, env = caller_env())

Note that parse_quosure() was soft-deprecated and renamed to parse_quo() in rlang 0.2.0 per its documentation. If we use parse_quo(), we need to specify the environment for the quosures e.g. 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 和右图

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

reprex 包 (v0.2.0) 于 2018 年 3 月 24 日创建.

Created on 2018-03-24 by the reprex package (v0.2.0).

这篇关于R:使用字符串作为参数来改变 dplyr 中的动词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆