How to change type of shiny visualization with radio button?


#1

I'd like to have a radio button in my shiny app that changes the table type -- one using DT::renderDataTable and one using renderTable. That requires different input and output types. I tried this in my server

 output$contents <- renderUI({
    if(input$myfilter == "interactive"){
     DT::renderDataTable({
         datatable(mydata())
      })
   } else {
   renderTable({
     mydata()
   })
    } 
   
 })

and this in my ui

uiOutput("contents")

but get an error that object of type 'closure' is not subsettable. What am I missing? Thanks.


#2

Hi,

Returning a list (instead of the renderDataTable or renderTable output) fix the issue. I'm not sure why or if this is a good solution, maybe someone with more expertise in Shiny can explain it better.

library(shiny)

ui <- fluidPage(
  mainPanel(
    radioButtons("myfilter", choices = c("interactive", "static"), label = "Display Format"),
    uiOutput("contents")
    )
  )

server <- function(input, output) {
  output$contents <- renderUI({
    if(input$myfilter == "interactive"){
      list(DT::renderDataTable({mtcars}))
    } else {
      list(renderTable({ mtcars }))
    } 
})
}

shinyApp(ui, server)

Regards,