R Shiny使用reactable设置sticky列报colDef未使用参数错误
问题描述
在R Shiny应用中配置reactable的sticky固定列时,运行代码抛出报错:
Error in colDef: unused argument (sticky = "left")
暂未定位报错原因,不确定是自身配置错误,还是数据聚合功能与sticky列存在兼容性冲突。
运行环境
- reactable 版本:0.3
- 系统平台:x86_64-apple-darwin17.0
- R版本:R version 4.2.0 (2022-04-22)
可复现问题的完整代码
library(shiny) library(reactable) library(tidyverse) ui <- fluidPage( titlePanel("Test sticky column"), mainPanel( reactableOutput("reactTop10") ) ) server <- function(input, output) { data<-reactive({ data.frame(Business.Name = c("Shop1","Shop2","Shop1","Shop2"), Method.Name = c("m1","m1","m2","m2"), Margin = c(25,32,43,12)) }) output$reactTop10 <- renderReactable({ sticky_style <- list(backgroundColor = "#f7f7f7") expr = reactable( data(), defaultColDef = colDef( footer = function(values, name) { htmltools::div(name, style = list(fontWeight = 600)) } ), rowStyle = JS("function(rowInfo) { if (rowInfo.aggregated == true) { return { fontWeight: 'bold' } } }"), groupBy = c("Business.Name", "Method.Name"), columns = list( Business.Name = colDef(style = list(fontWeight = 600), sticky = "left", headerStyle = list(borderRight = "1px solid #eee"), header = with_tooltip("Business.Name", "Names of our top 10 merchants")), Method.Name = colDef(aggregate = "unique", header = with_tooltip("Method.Name", "Name of the method bla bla")), Margin = colDef(aggregate = "mean", header = with_tooltip("Margin", "Name of the premium services")) )) }) } # Run the application shinyApp(ui = ui, server = server)
报错原因
该报错和数据聚合功能没有任何兼容性冲突,核心原因是当前使用的reactable 0.3版本过低,sticky固定列参数是在reactable 0.4.0版本才正式上线的功能,旧版本的colDef()函数本身不包含这个参数,所以会抛出「未使用参数」的错误。
解决步骤
- 在R控制台运行命令升级reactable到最新正式版本:
install.packages("reactable") - 升级完成后重启R/RStudio会话,重新运行原有代码即可正常使用sticky固定列功能,原有分组聚合配置不需要做任何修改。
- 代码中调用的
with_tooltip()是reactable内置的辅助函数,包版本升级完成后可直接正常调用,不需要额外引入其他依赖。
内容的提问来源于stack exchange,提问作者HK6
相关产品推荐
相关产品推荐

