Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Update updateselectInput() based on values of another updateSelectInput()

In the app below I want to update the second updateSelectInput() I have in order to exclude the values selected in the first updateSelectInput() after uploading a file. Normally the file uploads the dataset used for shiny widgets’ values.

## app.R ##
library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    fileInput("file1", "Choose CSV File", accept = ".csv"),

    selectInput("col","Pick a column to change its name",
                choices = colnames(iris)[-c(1)]),
    selectInput("col2","Pick a column to change its name",
                choices = colnames(iris)[-c(1)])

  ),
  dashboardBody(
    
    )
)

server <- function(session,input, output) {
  
  observeEvent(input$file1, {
    updateSelectInput(session, "col", label = "Select target country", choices = colnames(iris)[-c(1)])
    ex_names <- colnames(iris)[-1]
    ex_names <- ex_names[! ex_names %in% input$col]
    updateSelectInput(session, "col2", label = "Select reference countries", choices =colnames(iris)[-c(1)] )  

    
  })
  
 
  
}

shinyApp(ui, server)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Adding an observeEvent for col would update col2 every time a change has been made on the selected values of col

observeEvent(input$col, {
    ex_names <- colnames(iris)[-1]
    ex_names <- ex_names[!ex_names %in% input$col]
    updateSelectInput(session, "col2", label="Select reference countries", choices=ex_names)  
  })
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading