Home > OS >  Save the contents of a selectize input after the button has been clicked
Save the contents of a selectize input after the button has been clicked

Time:05-17

My goal is to save the selected countries in a variable, after the button is clicked. Below is a snippet of my code, could anyone help me complete it?

# ui.R

library(shiny)

shinyUI(fluidPage(
    selectizeInput(
        "elements",
        "",
        choices = c("Greece", "Italy", "France", "Belgium", "Latvia"),
        multiple = TRUE
    ),
    hr(),
    actionButton(
        "enter",
        "Enter"
    ) 
))

# server.R

library(shiny)

shinyServer(function(input, output, session) {
  # selected_countries <- reactive({ ... })
  # rest of the code
})

I need the list of the chosen countries because later I will have to open a tab for each of them.

CodePudding user response:

Please try the following code.

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectizeInput(
        "elements",
        "",
        choices = c("Greece", "Italy", "France", "Belgium", "Latvia"),
        multiple = TRUE
      ),
      hr(),
      actionButton(
        "enter",
        "Enter"
      ) 
    ),
    mainPanel(
      h3("Countries selected:"),
      br(),
      verbatimTextOutput("country")
    )
  )
)

server <- function(input, output, session) {

  country <- reactiveValues(sel = NULL)
  
  observeEvent(input$enter, {
    country$sel <- input$elements
  })

  output$country <- renderPrint({
    country$sel
  })
}

shinyApp(ui, server)
  • Related