R shiny - save chosen values in selectInput

前端 未结 1 779
滥情空心
滥情空心 2020-12-21 18:31

I have 10 \'Edit\' actionButtons. On each \'Edit\' action buttons appears three selectInputs. After I click on \'Save\' button below selectInputs, I want to remember selecte

相关标签:
1条回答
  • 2020-12-21 19:01

    you can do it by storing values into reactivevValues values stored onlyfor one session

    library(shinyjs) # needed for hide and show
    ui <- shinyUI(fluidPage(
    
      titlePanel("Update Select Inputs"),
    
      mainPanel(
        useShinyjs(),
        tags$head(tags$script(HTML("$(document).on('click', '.needed', function () {
                                   Shiny.onInputChange('last_btn',this.id);
                                   });"))),
            column(width = 6,
                   uiOutput('ValuesAndButtons')),
        column(width = 6,hidden(wellPanel(id="det",
                                          lapply(1:3,function(i){
                                            fluidRow(
                                              selectInput(
                                                paste0("Details", i), 
                                                label = i, 
                                                choices = c("","a","b","c"),
                                                selected = ""
    
                                              )
                                            )
    
                                          }),
                                          fluidRow(
                                            actionButton("Save","Save")
                                          )
        ))
        ))
      )
    )
    
    server <- shinyServer(function(input, output,session) {
      saved_values=reactiveValues(data=NULL)
    
      output$ValuesAndButtons <- renderUI({
    
        lapply(1:10,function(i){
          fluidRow(
            column(
              width = 4,
              tags$span(i)
            ),
            column(
              width = 3,
              actionButton(paste0('ActionButton', i),"Edit",class="needed"  )
            )
          )
        }
        )
      })
      observe({
        if(!is.null(input$last_btn)){
          observeEvent(input[[input$last_btn]],{
            shinyjs::show("det")
          }
                       )
    
        }
    
      })
      observeEvent(input$last_btn,{
    
        ll<-saved_values[[input$last_btn]]
        shinyjs::show("det")
    
        if(!is.null(ll) ){
    
          for(i in 1:length(ll)){
            updateSelectInput(session = session,inputId = names(ll)[[i]],selected = ll[[i]])
    
          }
        }else{
          shinyjs::reset("det")
        }
    
      })
    
    
      observeEvent(input$Save,{
    
        saved_values[[input$last_btn]]=lapply(names(input)[which(substring(names(input),1,7)=="Details")],function(i){
          input[[ i]]
        })
        names(saved_values[[input$last_btn]])=names(input)[which(substring(names(input),1,7)=="Details")]
        shinyjs::hide("det")
      })
    
    
    })
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    
    0 讨论(0)
提交回复
热议问题