简体   繁体   中英

obtain the values from a checkboxgroup only once in shiny

I have made the following program:

ui.r

library(shiny)
shinyUI(fluidPage(
    mainPanel(
    textOutput("text1"),
    checkboxGroupInput("checkGroup", 
                       label = h3("Alternatives"), 
                       choices = list("A" = 1, 
                                      "B" = 2, 
                                      "C" = 3,
                                      "D" = 4),
                       selected = NULL),
    actionButton("action", label = "Next")
    )
))

server.r

library(shiny)
shinyServer(function(input, output) {
  observe({
    if(input$action>0 & input$action<=2){
      valores<-renderText({
        input$checkGroup
      })
      data<-unlist(strsplit(valores(), split=" "))
      print(data)
    }
  })
})

I have managed to capture the values selected and put them into a vector, so I have solved an issue I got before.

The problem that I have now is that all the time it outputs which values I select or deselect from the checkbox groups, but I would like to only capture the last values selected after I press the Next button.

Any help? I have checked the documentation, but it is pretty scarce.

Here's one way to skin that cat.

We use isolate and reactive with the actionButton to ensure nothing is evaluated until the button is hit. By isolating the input itself from any further formatting we can call it any number of times for any purpose. Note that without the isolate it will evaluate as the inputs are changed.

server.R

library(shiny)
shinyServer(function(input, output) {

  # store as a vector to be called wherever desired
  # evaluated whenever inputs change
  datasetInput <- reactive({
    perm.vector <- as.vector(input$checkGroup)
    perm.vector
  }) 

  # call as a text output
  output$textOut <- renderText({
    input$action # makes sure nothing moves till the button is hit
    # isolate prevents datasetInput from reactively evaluating
    isolate(datasetInput()) 
  })

  # call in a different place as a data table output
  output$dataTableOut <- renderDataTable({
    input$action # makes sure nothing moves till the button is hit
    isolate(df <- data.frame(inputs = datasetInput()))
    df   
  }, options = list(
    lengthMenu = list(c(5, 15, -1), c('5', '15', 'All')),
    pageLength = 15)
  )

})

ui.R

library(shiny)
shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("checkGroup", 
                         label = h3("Alternatives"), 
                         choices = list("A" = 1, 
                                        "B" = 2, 
                                        "C" = 3,
                                        "D" = 4),
                         selected = NULL),
      actionButton("action", label = "Next")
    ),

    mainPanel(
      h4("Print as text"), textOutput("textOut"),
      h4("Print as data table"), dataTableOutput("dataTableOut")
    )
  )
))

在此输入图像描述

I found in: http://www.inside-r.org/packages/cran/shiny/docs/isolate

that it was possible to isolate a group of sentences, so the following works nicely:

library(shiny)
shinyServer(function(input, output) {
  observe({
    if(input$action>0 & input$action<=2){
      isolate({
        values<-renderText({
          input$checkGroup
        })
        data<-unlist(strsplit(valores(), split=" "))
        print(data)
      })
    }
  })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM