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

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

问题描述

我正在构建一个闪亮的应用程序,该应用程序需要允许用户定义新的绘图变量.具体来说,我想允许用户定义要在变异动词中使用的表达式.服务器接收该表达式作为文本,我想知道如何在dplyr 0.7中使mutate执行它.我可以使用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

以下是可重现的示例:

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版本实现它的正确方法是什么(正在阅读

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

!!:取消对等价单的引用,以便可以使用tidyeval动词

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

请注意,parse_quosure()已被软弃用,并且根据其文档在rlang 0.2.0中重命名为parse_quo().如果使用parse_quo(),则需要为具体环境指定环境,例如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& RHS

to separate 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

reprex软件包(v0.2.0)创建于2018-03-24.

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

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

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