单击按钮后保存选择输入的内容

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

我的目标是在单击按钮后将所选国家/地区保存在变量中。下面是我的代码片段,谁能帮我完成它?

# 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
})

我需要所选国家/地区的列表,因为稍后我必须为每个国家/地区打开一个选项卡。

请尝试以下代码。

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)