使用R formattable的style函数时遇未使用参数错误求助
解决formattable中style函数报错的问题
嘿,咱们来看看为啥会出现这个报错!你遇到的Error in style(color = ifelse(union_pre > goal, "red", "green")) : unused argument (color = ifelse(union_pre > goal, "red", "green"))问题,核心原因是函数名冲突——你的R环境里可能加载了其他包含有style()函数的包(比如ggplot2或其他可视化工具包),而这个被优先调用的style()并不接受color参数,导致formattable无法识别你的样式设置。
下面有两种简单的解决方法,任选其一都能解决问题:
方法1:明确指定formattable包的style函数
在调用style()时加上包名前缀,避免函数名混淆:
formattable(x_test, list( area(col = customers) ~ normalize_bar("pink", 0.5), goal = color_tile("white", "orange"), union_pre = formatter("span", style = ~ formattable::style(color = ifelse(union_pre > goal, "red", "green")), ~ icontext(ifelse(union_pre > goal,"arrow-up", "arrow-down"), union_pre) ) ))
方法2:直接返回CSS样式列表
formattable的formatter函数的style参数接受一个返回命名列表的公式,你可以跳过style()函数,直接返回样式列表:
formattable(x_test, list( area(col = customers) ~ normalize_bar("pink", 0.5), goal = color_tile("white", "orange"), union_pre = formatter("span", style = ~ list(color = ifelse(union_pre > goal, "red", "green")), ~ icontext(ifelse(union_pre > goal,"arrow-up", "arrow-down"), union_pre) ) ))
这两种方法都能让formattable正确识别你想要设置的文字颜色和箭头样式,应该就能正常生成你需要的表格了。
内容的提问来源于stack exchange,提问作者user3600910
相关产品推荐
相关产品推荐

