调用 tidyverse 函数时如何使用引号和不引号?
How to use quotation and unquotation when calling tidyverse functions?
我不明白如何在 R tidyverse 的上下文中使用表达式和 quotation/unquotation/quasiquotation。在下面的示例中,我认为使用反引号 (!!
) 运算符可以让我在计算表达式 ex
后为 add_row 生成所需的参数。但是,我得到了这个错误。我已经阅读了 Advanced R 中的 Metaprogramming Big Picture and Quasiquotation 章节,但我仍然无法理解如何正确使用这些功能。
library(tidyverse)
# create sample data
df <- data.frame(x = 1:10, y = 11:20, z = 21:30)
df
#> x y z
#> 1 1 11 21
#> 2 2 12 22
#> 3 3 13 23
#> 4 4 14 24
#> 5 5 15 25
#> 6 6 16 26
#> 7 7 17 27
#> 8 8 18 28
#> 9 9 19 29
#> 10 10 20 30
mini_df <- data.frame(x = 33:35, y = 43:45, z = 53:55)
mini_df
#> x y z
#> 1 33 43 53
#> 2 34 44 54
#> 3 35 45 55
# store the expression I want to call in add_row
ex <- expr(paste0(names(df),':=',paste0('mini_df$',names(mini_df)),collapse=','))
# attempt to call add_row using arguments unquoted after evaluating expression ex
add_row(df,(!! eval(ex)), .after = 3L)
#> New rows in `add_row()` must use columns that already exist:
#> * Can't find column `"x:=mini_df$x,y:=mini_df$y,z:=mini_df$z"` in `.data`.
由 reprex package (v0.3.0)
于 2019-05-17 创建
原来我应该通过调用 UQS()
或 !!!
来使用 unquote-splicing。我根本不需要使用 eval()
或 expr()
函数。相反,正确的用法如下:
library(tidyverse)
df <- data.frame(x = 1:10, y = 11:20, z = 21:30)
mini_df <- data.frame(x = 33:35, y = 43:45, z = 53:55)
add_row(df,!!!mini_df, .after = 3L)
#> x y z
#> 1 1 11 21
#> 2 2 12 22
#> 3 3 13 23
#> 4 33 43 53
#> 5 34 44 54
#> 6 35 45 55
#> 7 4 14 24
#> 8 5 15 25
#> 9 6 16 26
#> 10 7 17 27
#> 11 8 18 28
#> 12 9 19 29
#> 13 10 20 30
由 reprex package (v0.3.0)
于 2019-05-17 创建
有关取消引用和 unquote-splicing 的更多信息,请参阅:
Programming with dplyr - 特别是声明 "A very useful feature of unquote-splicing is that the vector names become argument names"
的行
我不明白如何在 R tidyverse 的上下文中使用表达式和 quotation/unquotation/quasiquotation。在下面的示例中,我认为使用反引号 (!!
) 运算符可以让我在计算表达式 ex
后为 add_row 生成所需的参数。但是,我得到了这个错误。我已经阅读了 Advanced R 中的 Metaprogramming Big Picture and Quasiquotation 章节,但我仍然无法理解如何正确使用这些功能。
library(tidyverse)
# create sample data
df <- data.frame(x = 1:10, y = 11:20, z = 21:30)
df
#> x y z
#> 1 1 11 21
#> 2 2 12 22
#> 3 3 13 23
#> 4 4 14 24
#> 5 5 15 25
#> 6 6 16 26
#> 7 7 17 27
#> 8 8 18 28
#> 9 9 19 29
#> 10 10 20 30
mini_df <- data.frame(x = 33:35, y = 43:45, z = 53:55)
mini_df
#> x y z
#> 1 33 43 53
#> 2 34 44 54
#> 3 35 45 55
# store the expression I want to call in add_row
ex <- expr(paste0(names(df),':=',paste0('mini_df$',names(mini_df)),collapse=','))
# attempt to call add_row using arguments unquoted after evaluating expression ex
add_row(df,(!! eval(ex)), .after = 3L)
#> New rows in `add_row()` must use columns that already exist:
#> * Can't find column `"x:=mini_df$x,y:=mini_df$y,z:=mini_df$z"` in `.data`.
由 reprex package (v0.3.0)
于 2019-05-17 创建原来我应该通过调用 UQS()
或 !!!
来使用 unquote-splicing。我根本不需要使用 eval()
或 expr()
函数。相反,正确的用法如下:
library(tidyverse)
df <- data.frame(x = 1:10, y = 11:20, z = 21:30)
mini_df <- data.frame(x = 33:35, y = 43:45, z = 53:55)
add_row(df,!!!mini_df, .after = 3L)
#> x y z
#> 1 1 11 21
#> 2 2 12 22
#> 3 3 13 23
#> 4 33 43 53
#> 5 34 44 54
#> 6 35 45 55
#> 7 4 14 24
#> 8 5 15 25
#> 9 6 16 26
#> 10 7 17 27
#> 11 8 18 28
#> 12 9 19 29
#> 13 10 20 30
由 reprex package (v0.3.0)
于 2019-05-17 创建有关取消引用和 unquote-splicing 的更多信息,请参阅:
Programming with dplyr - 特别是声明 "A very useful feature of unquote-splicing is that the vector names become argument names"
的行