【问题标题】:How to give title to a table created using renderReactable如何为使用 renderReactable 创建的表赋予标题
【发布时间】:2021-02-09 09:39:54
【问题描述】:

当我在 R 中执行以下代码时,它运行良好。

example<- reactable(data.frame(country=c("argentina","brazil"),
                               value=c(1,2)
))
withtitle <- htmlwidgets::prependContent(example, 
                                         h2(class = "title", "Top CRAN Packages of 2019"))
print(withtitle)

但是,当我使用闪亮的方式执行相同的代码时

reactableOutput("table_1")  

output$table_1 <- renderReactable({

example<- reactable(data.frame(country=c("argentina","brazil"),
                               value=c(1,2)
))
withtitle <- htmlwidgets::prependContent(example, 
                                         h2(class = "title", "Top CRAN Packages of 2019"))

print(withtitle)

})

报错:

renderWidget(instance) 中的警告:忽略前置内容; prependContent 不能在 Shiny 渲染调用中使用

请指导我为上表指定标题以及标题的背景颜色、字体颜色等其他参数。

【问题讨论】:

    标签: r shiny reactable


    【解决方案1】:

    您可以在ui 部分中定义标题。也许你正在寻找这个

    library(reactable)
    
    ui <- fluidPage(
      h2("Top CRAN Packages of 2019"),
      reactableOutput("table_1")
    )
     
    server <- function(input, output) {
      output$table_1 <- renderReactable({
        
        example<- reactable(data.frame(country=c("argentina","brazil"),value=c(1,2)))
    
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    请注意,您的代码给了我同样的错误,但我确实在 RStudio 的查看器窗格中得到了正确的输出。

    【讨论】:

    • 非常感谢。是的,我在查看器窗格中也得到了正确的输出,但当我使用我的主代码运行时却没有。但是,在 ui 中定义标题很顺利。您能否建议在ui中定义后如何更改标题的颜色和背景颜色?
    【解决方案2】:

    感谢 YBS。我从你的回答中得到了这个想法。我在 reactableOutput() 之前在 ui 中使用了 div() 文本,并且可以获得带有选择颜色和背景的标题。使用的代码如下:

                div("Top CRAN Packages of 2019", style = "text-align: center; 
                      background-color: #3380FF; color:yellow; font-size:200%"),
                reactableOutput("table_1")
    

    再次感谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2011-01-06
      • 2021-07-11
      • 1970-01-01
      • 2019-10-17
      相关资源
      最近更新 更多