gpt4 book ai didi

r - 将列名作为点-点-点传递以进行 qplot() 的非标准评估

转载 作者:行者123 更新时间:2023-12-04 07:15:08 25 4
gpt4 key购买 nike

我为 map 想到了一个可爱的用例+ ...对于 purrr 研讨会:将 data.frame 拆分为 data.frames 列表,然后通过 qplot(...) 单独绘制它们称呼。

groups_of_plots <- function(data, group, ...){
table_list <- split(data, data[[group]])

plots <- purrr::map(.x = table_list,
.f = ~ggplot2::qplot(data = .x, ...))
return(plots)
}
但是当我像往常一样直接调用函数时,我得到这个错误,这似乎是对 x 的非标准评估的问题。作为 qplot 的美学参数(使其成为函数定义中的固定参数使其工作,但当然会使函数基本无用)
groups_of_plots(data = iris, 
group = 'Species',
x = Sepal.Length)

error/rlang_error>
Unknown input: data.frame
Backtrace:
1. (function (x, ...) ...
2. ggplot2:::print.ggplot(x)
4. ggplot2:::ggplot_build.ggplot(x)
5. ggplot2:::by_layer(function(l, d) l$compute_aesthetics(d, plot))
6. ggplot2:::f(l = layers[[i]], d = data[[i]])
7. l$compute_aesthetics(d, plot)
8. ggplot2:::f(..., self = self)
9. ggplot2:::is_calculated_aes(aesthetics)
10. base::vapply(aesthetics, is_calculated, logical(1), USE.NAMES = FALSE)
11. ggplot2:::FUN(X[[i]], ...)
好想坚持 qplot ,不仅是为了简单,而且因为在我看来这是一个很好的例子,你不想也不能在你的函数定义中明确指定所有可能的参数,但据我所知它只支持传递审美作为非标准评估,这使事情变得相当复杂。

最佳答案

问题在于~带有 map 的函数的快捷方式重新利用 ...干扰参数放置的符号。一个可能的解决方法是

groups_of_plots <- function(data, group, ...){
table_list <- split(data, data[[group]])

plots <- purrr::map(.x = table_list,
.f = function(.x, ...) ggplot2::qplot(data = .x, ...), ...)
return(plots)
}
这里我们创建了具有 ... 的函数值,我们将它们传递给内部函数,我们还必须将它们传递给 map()函数,以便它们可以传递到内部函数。

关于r - 将列名作为点-点-点传递以进行 qplot() 的非标准评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68810393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com