如何从rlang';我能处理的美国数据。?

如何从rlang';我能处理的美国数据。?,r,rlang,R,Rlang,我正在构建一个整洁的兼容函数,用于dplyr的mutate中,我希望传递一个变量和我正在处理的数据集,并使用两者的信息构建一个向量 作为一个基本示例,假设我想要返回一个字符串,其中包含变量的平均值和数据集中的行数(我知道我可以只取var的长度,忽略它,这是一个示例) 如何从.data获取完整信息?我知道我没有将其包括在示例中,但具体地说,我需要attr(dat)中的一些内容,以及dat中的变量中的一些内容,这些内容是为分组正确设置的子集,因此,无论是恢复到,还是仅仅从中提取变量和获取内容都不会起

我正在构建一个整洁的兼容函数,用于
dplyr
mutate
中,我希望传递一个变量和我正在处理的数据集,并使用两者的信息构建一个向量

作为一个基本示例,假设我想要返回一个字符串,其中包含变量的平均值和数据集中的行数(我知道我可以只取
var
的长度,忽略它,这是一个示例)

如何从
.data
获取完整信息?我知道我没有将其包括在示例中,但具体地说,我需要
attr(dat)
中的一些内容,以及
dat
中的变量中的一些内容,这些内容是为分组正确设置的子集,因此,无论是恢复到
,还是仅仅从中提取变量和获取内容都不会起作用。

如上述评论所述,这是不可能的,因为这不是
.data
的预期用途。然而,现在我已经放弃了直接做这件事,我已经用
.data
的组合做了一个乱七八糟的工作

info <- function(var,df = get(".",envir = parent.frame())) {
  #First, get any information you need from .
  fulldatasize <- nrow(df)

  #Then, check if you actually need .data,
  #i.e. the data is grouped and you need a subsample
  if (length(var) < nrow(df)) {
      #If you are, get the list of variables you want from .data, maybe all of them
      namesiwant <- names(df)

      #Get .data
      datapronoun <- get('.data',envir=parent.frame())

      #And remake df using just the subsample
      df <- data.frame(lapply(namesiwant, function(x) datapronoun[[x]]))
      names(df) <- namesiwant
  }

  #Now do whatever you want with the .data data
  groupsize <- nrow(df)

  paste(mean(var),groupsize,fulldatasize,sep=', ')
}

dat <- data.frame(a = 1:10, i = c(rep(1,5),rep(2,5)))

#types contains the within-group mean, then 5, then 10
dat %>% group_by(i) %>% mutate(types = info(a))
info为什么不在这里使用
length()
而不是
nrow()

dat%变异(类型=信息(a))
#>#tibble:10 x 3
#>#组:i[2]
#>a i类型
#>      
#>  1     1     1 3, 5 
#>  2     2     1 3, 5 
#>  3     3     1 3, 5 
#>  4     4     1 3, 5 
#>  5     5     1 3, 5 
#>  6     6     2 8, 5 
#>  7     7     2 8, 5 
#>  8     8     2 8, 5 
#>  9     9     2 8, 5 
#> 10    10     2 8, 5

我想你做不到。正如您所说,
.data
实际上并不是一个数据帧,它有自己的类(
rlang\u data\u代词
没有“父”类),并且
rlang
不希望您以任何其他方式使用它。该类没有为
dim
nrow
定义方法,如果使用
[
length
。嗯,太糟糕了。也许我可以让它同时通过
.data
。它很笨重,但符合我的目的。正如问题中提到的,我知道我可以这样做,但这只能解决具体的例子,而不是一般的问题。需要从数据集。你能给出一个不那么琐碎的例子,用一种不适合你的方法来解决吗?我怀疑你的真实案例可能不需要像你自己的答案那样复杂。具体来说,我需要传入三件事:(1)组子集var[由var完成],(2)组子集数据集中的几个其他变量[由.data完成],和(3)关于(2)中需要哪些其他变量的信息,这些变量存储在attr(,“varnames”)。(3)与.data一起丢失,因此我似乎需要同时获取(2)和(3)的.data。
info2 <- function(var,df = get(".data",envir = parent.frame())) {
  paste(mean(var),nrow(.data),sep=', ')
}

#Doesn't work. nrow(.data) gives blank strings
dat %>% group_by(i) %>% mutate(types = info2(a))
info <- function(var,df = get(".",envir = parent.frame())) {
  #First, get any information you need from .
  fulldatasize <- nrow(df)

  #Then, check if you actually need .data,
  #i.e. the data is grouped and you need a subsample
  if (length(var) < nrow(df)) {
      #If you are, get the list of variables you want from .data, maybe all of them
      namesiwant <- names(df)

      #Get .data
      datapronoun <- get('.data',envir=parent.frame())

      #And remake df using just the subsample
      df <- data.frame(lapply(namesiwant, function(x) datapronoun[[x]]))
      names(df) <- namesiwant
  }

  #Now do whatever you want with the .data data
  groupsize <- nrow(df)

  paste(mean(var),groupsize,fulldatasize,sep=', ')
}

dat <- data.frame(a = 1:10, i = c(rep(1,5),rep(2,5)))

#types contains the within-group mean, then 5, then 10
dat %>% group_by(i) %>% mutate(types = info(a))