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
相关产品推荐
相关产品推荐

