NSE Arguments in functions calling plotly raise an error
还没有人认领这个 Issue。
- 主要语言
- R
- 星标
- 2.7k
- 派生
- 641
- PR 合并指标
- 30 天内没有已合并 PR
描述
While toying with different NSE approaches I stumbled upon the following behaviour which seems to me like a bug. Any NSE argument (even if not used in plotly) results in an error:
library(plotly)
nse_arg_raises_error <- function(xx) {
mf <- rlang::new_formula(NULL, rlang::ensym(xx), globalenv())
plot_ly(mtcars, x = ~ cyl, y = ~ mpg, type = "scatter",
mode = "markers")
}
nse_arg_raises_error() ## works
nse_arg_raises_error(cyl) ## does not work
#> Error in as.list.environment(x, all.names = TRUE): object 'cyl' not found
This behaviour is unique to plotly, b/c if I leave out the plot_ly call, the error does not happen.
it_is_a_plotly_problem <- function(xx) {
mf <- rlang::new_formula(NULL, rlang::ensym(xx), globalenv())
1 + 1
}
it_is_a_plotly_problem() ## works
#> [1] 2
it_is_a_plotly_problem(cyl) ## also works
#> [1] 2
However, some preliminary debugging shows that the culprit sits (deep) in base:::as.list.environment (which is called from lazyeval:::clone_env, which in turn together with its brethren from lazyeval is responsible for the NSE part in plot_ly)
it_is_a_as_list_environment_issue <- function(xx) {
mf <- rlang::new_formula(NULL, rlang::ensym(xx), globalenv())
cp <- as.list.environment(environment())
1 + 1
}
it_is_a_as_list_environment_issue(cyl)
#> Error in as.list.environment(x, all.names = TRUE): object 'cyl' not found
So basically, when lazyeval:::clone_env copies the function environment which contains an NSE argument this error from within the C code implementation of base::as.list.environment bubbles up.
I am inclined to say that this a feature and not a bug of as.list.environment and the user of it should make sure that environments using it are in fact able to be safely transformed into a list. Thus, I am posting the issue here as it touches the whole NSE strategy of plotly - coming from plotly:::eval_attr (especially since lazyeval is retired in favor of rlang).
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
使用提供的 nse_arg_raises_error 示例复现该问题,然后跟踪 plotly:::eval_attr,直到 lazyeval:::clone_env 以及对 base:::as.list.environment 的调用。检查 plotly 的 NSE 路径如何处理包含未使用 NSE 参数的函数环境。完成标准是:示例中的 plot_ly 调用不再引发 as.list.environment 错误;payload 未指定测试文件。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- r
- 领域
- data-visualization
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100