仅获取已更改的InputID

时间:2018-10-22 13:17:43

标签: shiny

是否可以仅选择/获取已更改的小部件的输入名称?假设我有一个闪亮的应用程序,并且取消选中了一个checkboxGroupInput框。可以通过某种方式获取该小部件的inputId吗?

1 个答案:

答案 0 :(得分:0)

这是一个解决方案:

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("changedInputs")
    ),
    mainPanel(
      plotOutput("distPlot1"),
      plotOutput("distPlot2")
    )
  )
)
server <- function(input, output) {

  inputStatus <- vector(mode = "character")

  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')
  })

  observe({
    myInputs <- reactiveValuesToList(input)

    if(identical(inputStatus, character(0))){
      inputStatus <<- unlist(myInputs)
    } else {
      changedInputs <- names(inputStatus)[inputStatus != unlist(myInputs)]
      output$changedInputs <- renderText(paste("Changed inputs:", changedInputs))
      print(paste("Changed inputs:", changedInputs))
      inputStatus <<- unlist(myInputs)
    }
  })

}

shinyApp(ui = ui, server = server)