Reputation: 8374
I have a list l
of data.frames
, and I want to dynamically select elements with selectInput
.
Once the user selects an element, I'd like to be able work with it under the hood, for example use it in renderTable
and output a table of that data.frame
.
Moreover I'd like to be able to select that element in the list, and apply lm
for example.
The problem I'm facing is here: l[[ElemInput()]]
how do I convert this classic R
code with a reactive element?
Here the complete shinydashboard
:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(
title = "My Dashboard"
),
dashboardSidebar(
sidebarMenu(
menuItem("aaa", tabName = "aaa", icon = icon("dashboard"))
)
),
dashboardBody(
tabItems(
# First tab content
tabItem(tabName = "aaa",
h2("Dashboard aaa"),
fluidRow(
box(
selectInput("input_name", "Select input:", choices = names(l))
),
box(
dataTableOutput('table')
)
)
)
)
)
)
server <- function(input, output) {
# create a fake list
set.seed(123)
l <- list(element1 = data.frame(aaa=1:5, bbb=sample(5)),
element2 = data.frame(ccc=6:10, ddd=sample(5)))
# reactive read element
ElemInput <- reactive({
input$input_name
})
# render table
output$table <- renderTable({
l[[ElemInput()]] # this part doesn't work
})
}
shinyApp(ui, server)
Possibly correlated questions but I couldn't figure out a solution for my case:
dynamically list 'choices' for selectInput from a user selected column
Store selectInput in a string/character object
Upvotes: 2
Views: 1711
Reputation: 7699
You should use renderTable
with tableOutput
in your ui
. So change dataTableOutput('table')
to tableOutput('table')
.
You can use dataTableOutput
if you use renderDataTable
though.
Also my I prefer to use an observer instead of a reactive function. It's a bit easier to read.
server <- function(input, output) {
observeEvent(input$input_name,{
# observe element change and render table
output$table <- renderTable({
data.frame(l[[input$input_name]])
})
})
}
Upvotes: 1