Search code examples
rshinyreactable

How to give title to a table created using renderReactable


When I execute following code in R, it works well.

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)

However, when I execute the same code with shiny using

reactableOutput("table_1")  

and

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)

})

It gives error:

Warning in renderWidget(instance) : Ignoring prepended content; prependContent can't be used in a Shiny render call

Kindly guide me to give title to the above table and other parameters like background color, font color etc. for the title.


Solution

  • Thanks YBS. I got the idea from your answer. I used div() text in ui before reactableOutput() and could get the heading with choice color and background. The code used is given below:

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

    Thanks once again.