You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RShiny中dplyr group_by列未找到错误排查求助

解决dplyr group_by()在函数调用中的报错问题

问题根源

你在paste_info中调用gather_info时,传递的grp.col没有使用tidy eval的{{}}语法进行引用。这导致gather_info里的group_by({{ grp.col }})试图寻找名为grp.col的列,而非你实际传入的列(比如category),从而触发"Column grp.col is not found"的错误。同时,pull(grp.col)也存在同样问题,无法正确提取目标列。

修复方案

修改paste_info函数,在传递grp.col给gather_info以及调用pull时,都用{{}}包裹变量名:

paste_info <- function(df, grp.col) {
    
    high_df <- gather_info(df, {{ grp.col }}, maxm=TRUE)
    low_df <- gather_info(df, {{ grp.col }}, maxm=FALSE)

    paste0("Given your data, your best performing group is ",
          high_df |> pull({{ grp.col }}), " scoring ", high_df$percentage, "%",
          " and your worst performing group is ",
          low_df |> pull({{ grp.col }}), " scoring ", low_df$percentage, "%.")
}

验证效果

运行修改后的代码:

paste_info(df, category)

将返回期望结果:

[1] "Given your data, your best performing group is Cat1 scoring 90% and your worst performing group is Cat2 scoring 20%."

补充说明

{{}}(称为"curly-curly")是tidyverse中用于裸变量传递的语法,它会捕获函数调用时传入的变量名,并在数据框上下文解析为对应的列。在嵌套函数调用中,必须逐层使用{{}}传递裸变量,才能保证tidy eval正确工作。

内容的提问来源于stack exchange,提问作者big_cactus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 13:20:35