使用rlang在表达式的准引用中使用预先存在的字符向量 [英] Using pre-existing character vectors in quasiquotation of an expression with rlang

查看:88
本文介绍了使用rlang在表达式的准引用中使用预先存在的字符向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,当使用dplyr时,其中一个具有用于处理数据的列名的字符向量,例如:

Sometimes when working with dplyr one has a character vector of column names that's to be used to operate on data, for instance:

 cols_of_interest <- c("Petal.Width", "Petal.Length")

dplyr 0.5.0及更早版本中,建议的解决此问题的方法是使用verb_下划线构造,如下所示:

In dplyr 0.5.0 and earlier, the recommended approach to this problem was to use the verb_ underscore construct as follows:

library("tidyverse")
my_cols <- c("Petal.Width", "Petal.Length")
iris %>%
  select_(.dots = my_cols)

现在不推荐使用verb_函数,而推荐使用rlang库引入的新的整洁评估框架(dplyr.tidyverse.org/articles/programming.html).

The verb_ functions are now deprecated in favour of the new tidy evaluation framework (dplyr.tidyverse.org/articles/programming.html) introduced by the rlang library.

dplyr 0.7.0开始,以下工作无需任何特殊调整:

As of dplyr 0.7.0 the following works without any special accommodations:

library("tidyverse")
# library("rlang")
my_cols <- c("Petal.Width", "Petal.Length")
iris %>%
  select(my_cols)

请注意,在dplyr的开发版本中,情况并非如此

Note that in development builds of dplyr, this was not the case

在Shiny应用程序中选择列是一个很好的示例用例,这就是使用verb_表示法的方式

Selecting columns in Shiny apps is a good example use case, this is how one could do it using verb_ notation

library("shiny")
library("tidyverse")
library("DT")

shinyApp(
  ui = fluidPage(
    selectInput("cols_to_show",
                "Columns to show",
                choices = colnames(iris),
                multiple = TRUE),
    dataTableOutput("verb_table")
  ),
  server = function(input, output){
    output$verb_table <- renderDataTable({
      iris %>%
        select_(.dots = input$cols_to_show)

    })
  }
)

推荐答案

在0.5.0版以前的dplyr中,用于非标准评估的基础框架为lazyeval,并且需要对字符串进行特殊考虑. Hadley Wickham发布了dplyr的根本新版本,并带有一个名为rlang的新肋骨,它为非标准评估提供了更一致的框架.这是版本0.70-这是为什么跳过0.6.0的解释-

In pre 0.5.0 dplyr the underlying framework for non-standard evaluation was lazyeval and required special consideration for strings. Hadley Wickham released a fundamentally new version of dplyr with a new underbelly called rlang which provides a more consistent framework for non-standard evaluation. This was version 0.70 - here's an explanation of why 0.6.0 was skipped - https://blog.rstudio.org/2017/06/13/dplyr-0-7-0/

以下内容无需任何特殊考虑即可工作:

The following now works without any special considerations:

library("tidyverse")
my_cols <- c("Petal.Width", "Petal.Length")
iris %>%
  select(my_cols)

请注意,新的rlang框架增加了使用quores拥有裸符号向量的功能

Note that the new rlang framework adds the ability to have a vector of naked symbols using quosures

my_quos <- quos(Petal.Width, Petal.Length)
iris %>%
  select(!!!my_quos)

您可以在此处了解有关使用dplyr进行编程的更多信息- http://dplyr.tidyverse .org/articles/programming.html

You can read more about programming with dplyr here - http://dplyr.tidyverse.org/articles/programming.html

library("shiny")
library("tidyverse")
library("DT")
library("rlang")
shinyApp(
  ui = fluidPage(
    selectInput(
      "cols_to_show",
      "Columns to show",
      choices = colnames(iris),
      multiple = TRUE
    ),
    dataTableOutput("verb_table"),
    dataTableOutput("tidyeval_table")
  ),
  server = function(input, output) {
    output$verb_table <- renderDataTable({
      iris %>%
        select_(.dots = input$cols_to_show)

    })

    output$tidyeval_table <- renderDataTable({
      iris %>%
        select(!!!syms(input$cols_to_show))

    })
  }
)

这篇关于使用rlang在表达式的准引用中使用预先存在的字符向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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