Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

obtain the values from a checkboxgroup only once in shiny

Tags:

r

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.

like image 260
Little Avatar asked Dec 10 '25 00:12

Little


1 Answers

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")
    )
  )
))

enter image description here

like image 131
mlegge Avatar answered Dec 12 '25 17:12

mlegge