Reputation: 45
I have some selectInputs, "qunits", "rbunits", and "EBEDunits". When a file is uploaded I want the selectInputs to update to select whatever units are in the excel file. I do this by running the line observe({updateSelectInput(session, "rbunits", choices=c("cm", "m", "mm", "in", "ft") selected=paramdat()$units[6])})
. However, when the updateSelectInput is run, it replaces the select input with nothing. I believe the problem is that "selected" has to be a subset of "choices". Is it possible to have these two be separated? I don't know any other way to update the selectInput with data from the excel file, so any ideas would be helpful. Thank you.
ui <- fluidPage(theme=shinytheme("united"),
fileInput("file1", "Choose .xlsx File", accept = ".xlsx"),
fluidRow(
column(3,
selectInput("qunits", "", c("meq/L")),
selectInput("rbunits", "", c("cm", "m", "mm", "in", "ft")),
selectInput("EBEDunits", "", c("")))
))
server <- function(input, output, session) {
paramdat<-reactiveVal()
observe({
file <- input$file1
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(input$file1$type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Please select xlsx"))
params<-read_xlsx(file$datapath, sheet=1)
paramdat(params)
})
observe({updateSelectInput(session, "rbunits", choices=c("cm", "m", "mm", "in", "ft") selected=paramdat()$units[6])})
Upvotes: 0
Views: 25
Reputation: 3002
Hi i dont have excel or any excel files but i think it doesn't matter for this question.
you can use updateSelectInput
library(shiny)
ui <- fluidPage(
titlePanel("Conditional SelectInput Update"),
mainPanel(
selectInput("Dataset","Select Dataset", choices = c("mtcars","iris"),selected = "mtcars",multiple = FALSE),
selectInput("cols","Select Columns", choices = c(""),multiple = TRUE),
dataTableOutput("table")
)
)
server <- function(input, output,session) {
observeEvent(input$Dataset,{
x<-get(input$Dataset)
dfnames<-names(x)
updateSelectInput(session, "cols",choices = dfnames)
output$table<-renderDataTable(x[input$cols])
})
}
shinyApp(ui = ui, server = server)
Upvotes: 1