Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
dplyr的功能与非标准评价_R_Function_Dplyr - Fatal编程技术网

dplyr的功能与非标准评价

dplyr的功能与非标准评价,r,function,dplyr,R,Function,Dplyr,我刚读完,开始掌握函数的非标准评估。这篇文章的具体问题是,如何使用tidyverse(例如quo(),!!,等等)直接编写下面的代码“而不是base-R方法eval(),替换,等等 library(tidyverse) xy <- data.frame(xvar = 1:10, yvar = 11:20) plotfunc <- function(data, x, y){ y.sqr <- (eval(substitute(y), envir = data))^2 p

我刚读完,开始掌握函数的非标准评估。这篇文章的具体问题是,如何使用tidyverse(例如
quo()
!!
,等等)直接编写下面的代码“
而不是base-R方法
eval()
替换
等等

library(tidyverse)
xy <- data.frame(xvar = 1:10, yvar = 11:20)

plotfunc <- function(data, x, y){
  y.sqr <- (eval(substitute(y), envir = data))^2
  print(
    ggplot(data, aes_q(x = substitute(x), y = substitute(y.sqr))) + 
      geom_line()
  )
}

plotfunc(xy, xvar, yvar)
库(tidyverse)

xy您可以执行以下操作:

library(tidyverse)
xy <- data.frame(xvar = 1:10, yvar = 11:20)

plotfunc <- function(data, x, y){
  x <- enquo(x)
  y <- enquo(y)
  print(
    ggplot(data, aes(x = !!x, y = (!!y)^2)) + 
      geom_line()
  )
}
plotfunc(xy, xvar, yvar)
库(tidyverse)

xy使用
rlang_0.4.0
,我们可以使用整洁的求值运算符(
{…}
)或
卷曲
,它将引号和unquote抽象为一个插值步骤。这使得创建函数更容易

library(rlang)
library(ggplot2)
plotfunc <- function(data, x, y){

  print(
    ggplot(data, aes(x = {{x}}, y = {{y}}^2)) + 
      geom_line()
  )
}
plotfunc(xy, xvar, yvar)
库(rlang)
图书馆(GG2)
plotfunc
library(rlang)
library(ggplot2)
plotfunc <- function(data, x, y){

  print(
    ggplot(data, aes(x = {{x}}, y = {{y}}^2)) + 
      geom_line()
  )
}
plotfunc(xy, xvar, yvar)