Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

shiny - Get only InputIDs that have changed

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 possible to somehow get the inputId of that widget?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...