0
votes

I'm trying to display a table in the mainpanel based on selection by the user from drop down.

Ex. The "selectInput" dropdown will have the values from table789 (options are 123, xyz, ...) If user selects 123 then "table123" has to be displayed else "table456" to be displayed.

I'm very new to shiny, tried the below and got "attempt to replicate an object of type 'closure'" error. I'm not sure what I'm missing. Can anyone help?

Server:

    server = function(input, output, session) {
  
  output$outputtable = reactive({ifelse(input$selction == '123', DT::renderDataTable(table123), DT::renderDataTable(table456)) })
  
}

UI:

    ui <- fluidPage(
  
  sidebarPanel(selectInput("selction", "Select", choices = table789$column1, selected = "xyz")),
  
  mainPanel(
    tabsetPanel(
      tabPanel("select", DT::dataTableOutput("outputtable")),
    )
  )
)
1

1 Answers

0
votes

I think you're mixing things up here, you shouldn't be putting renderDataTable into reactive, this should work:

library(shiny)

table789 <- mtcars
table456 <- iris
table123 <- mtcars
table789$column1 <- sample(c("123","456"),nrow(table789),replace = T)


ui <- fluidPage(
    
    sidebarPanel(selectInput("selection", "Select", choices = unique(table789$column1), selected = "xyz")),
    
    mainPanel(
        tabsetPanel(
            tabPanel("select", 
                     DT::dataTableOutput("outputtable")
            )
        )
    )
)

server = function(input, output, session) {
    
    
    data <- eventReactive(input$selection,{
        if(input$selection == "123"){
            return (table123)
        }
        table456
    })
    
    output$outputtable <- DT::renderDataTable(
        data()
    )
    
}

#Run the app
shinyApp(ui = ui, server = server)