David_Cola
David_Cola

Reputation: 45

Is it possible to use updateSelectInput that selects a variable name?

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

Answers (1)

user12256545
user12256545

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

Related Questions