Get only InputIDs that have changed

后端 未结 1 1809
悲&欢浪女
悲&欢浪女 2020-12-18 17:06

Is it possible to select/get only the input names of the widgets that have changed? Say that I have a Shiny App and that I deselect a box of a checkboxGroupInput. Is it poss

相关标签:
1条回答
  • 2020-12-18 17:31

    Here is a solution using basic shiny:

    library(shiny)
    
    ui <- fluidPage(
      titlePanel("Old Faithful Geyser Data"),
      sidebarLayout(
        sidebarPanel(
          sliderInput("bins1",
                      "Number of bins 1:",
                      min = 1,
                      max = 50,
                      value = 30),
          sliderInput("bins2",
                      "Number of bins 2:",
                      min = 1,
                      max = 50,
                      value = 30),
          textOutput("printChangedInputs")
        ),
        mainPanel(
          plotOutput("distPlot1"),
          plotOutput("distPlot2")
        )
      )
    )
    
    server <- function(input, output) {
    
      output$distPlot1 <- renderPlot({
        x    <- faithful[, 2] 
        bins <- seq(min(x), max(x), length.out = input$bins1 + 1)
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
      })
    
      output$distPlot2 <- renderPlot({
        x    <- faithful[, 2] 
        bins <- seq(min(x), max(x), length.out = input$bins2 + 1)
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
      })
    
      previousInputStatus <- NULL
    
      changedInputs <- reactive({
        currentInputStatus <- unlist(reactiveValuesToList(input))
        if(is.null(previousInputStatus)){
          previousInputStatus <<- currentInputStatus
          changedInputs <- NULL
        } else {
          changedInputs <- names(previousInputStatus)[previousInputStatus != currentInputStatus]
          print(paste("Changed inputs:", changedInputs))
          previousInputStatus <<- currentInputStatus
        }
        return(changedInputs)
      })
    
      output$printChangedInputs <- renderText({paste("Changed inputs:", changedInputs())})
    
    }
    
    shinyApp(ui = ui, server = server)
    

    Edit: Another way would be to listen for the JavaScript event shiny:inputchanged:

    library(shiny)
    
    ui <- fluidPage(
      tags$head(
        tags$script(
          "$(document).on('shiny:inputchanged', function(event) {
          if (event.name != 'changed') {
            Shiny.setInputValue('changed', event.name);
          }
        });"
        )
      ),
      titlePanel("Old Faithful Geyser Data"),
      sidebarLayout(
        sidebarPanel(
          sliderInput("bins1",
                      "Number of bins 1:",
                      min = 1,
                      max = 50,
                      value = 30),
          sliderInput("bins2",
                      "Number of bins 2:",
                      min = 1,
                      max = 50,
                      value = 30),
          textOutput("changedInputs")
        ),
        mainPanel(
          plotOutput("distPlot1"),
          plotOutput("distPlot2")
        )
      )
    )
    
    server <- function(input, output) {
    
      output$distPlot1 <- renderPlot({
        x    <- faithful[, 2] 
        bins <- seq(min(x), max(x), length.out = input$bins1 + 1)
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
      })
    
      output$distPlot2 <- renderPlot({
        x    <- faithful[, 2] 
        bins <- seq(min(x), max(x), length.out = input$bins2 + 1)
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
      })
    
      output$changedInputs <- renderText({paste("Changed inputs:", input$changed)})
    
    }
    
    shinyApp(ui = ui, server = server)
    

    Please see this for more information.

    0 讨论(0)
提交回复
热议问题