意外行为选择全部并取消选择所有操作按钮(R闪亮)

时间:2017-05-27 20:26:32

标签: r shiny

我遇到了意想不到的行为。我打算做的是: - 当用户点击"全选时," "汇总表"中的所有行被选中。这个工作。但是,下面的代码不会被调用。

data <- eventReactive(input$selectAll,{
      print("Select All - restore data")
      rawdata

  })

- 另一方面,当用户点击&#34;取消全选时,&#34; &#34;汇总表&#34;中的所有行取消选择。这个WORKS和GETS下面的代码叫做。

# Restore data when users click 'Deselect All'
  data <- eventReactive(input$deselectAll,{
      print("Deselect All - restore data")
      rawdata
  })

知道为什么吗?

这是我的完整代码:

DATASET

colA <- c('A','B','C','D','E')
colB <- c(1,2,3,4,5)
rawdata <- as.data.frame(cbind(colA,colB))
View(rawdata)

server.R

function(input, output, session) {

  # Activate tab 'Result' when users click 'Run'
  observeEvent(input$runButton, {
      updateTabsetPanel(session, "allResults", 'result')
  })

  # Create a dataset based on users' selected variables
  data <- eventReactive(input$inputVars_rows_selected,{
      print("Select Some Vars")
      rawdata[, c(input$inputVars_rows_selected)]
  })

  # Restore data when users click 'Select All'
  data <- eventReactive(input$selectAll,{
      print("Select All - restore data")
      rawdata
  })

  # Restore data when users click 'Deselect All'
  data <- eventReactive(input$deselectAll,{
      print("Deselect All - restore data")
      rawdata
  })

  ### VARIABLE SELECTION ####

  var <- reactiveValues()

  # Select all vars
  observeEvent(input$selectAll,{
      print("SelectAll ObserveEvent")
      var$selected <- 1:nrow(rawdata)
      print(var$selected)
  })

  # Deselect all vars
  observeEvent(input$deselectAll,{
      print("deselectAll ObserveEvent")
      var$selected <- 0
      print(var$selected)
      print(data())
  })

  ### RESULT TAB ###

  result <- eventReactive (input$runButton, {
      head(data(),2)
  })

  ### RENDERING FUNCTIONS ###

  # Default SummaryTable
  output$inputVars <- DT::renderDataTable({
      if (input$selectAll==0 & input$deselectAll==0) {
          print("Default Summary Table")
          DT::datatable(rawdata, options = list(paging = FALSE, searching = FALSE))
      } 
      else {
          DT::datatable(rawdata, options = list(paging = FALSE, searching = FALSE), selection = list(target = 'row', selected = var$selected))
      }
  })

  # Display results
  output$result <- DT::renderDataTable({
      DT::datatable(result(), options = list(paging = FALSE, searching = FALSE))
  })

  output$temp <- renderPrint({
      print(input$selectAll)
      print(input$deselectAll)
  })
}

ui.R

fluidPage(

  sidebarPanel(
      actionButton("runButton", strong("Run!"))
  ),

  mainPanel(
      tabsetPanel(id = "allResults",
        tabPanel(value='inputVars',title='Variable Selection', 
                  verticalLayout(
                      DT::dataTableOutput('inputVars'),
                      br(),
                      fluidRow(align="bottom", 
                             column(2, actionButton("selectAll"  , strong("Select All"))),
                             column(3, actionButton("deselectAll", strong("Deselect All")))
                      )
                  )
                ),
        tabPanel(value='result',title='Result', DT::dataTableOutput('result')),
        tabPanel(value='temp',title="TEMP", verbatimTextOutput("temp"))
      )
  )

)

更新的Server.R#2: @Mike和@HubertL,我认为你是对的:问题是由eventReactive有缓存值引起的。在此更新版本中,observeEvent对应于Select All和Deselect All按预期工作。但是,现在eventReactive对应于输入$ inputVars_rows_selected NEVER被调用。知道为什么吗?

function(input, output, session) {

  # Activate tab 'Result' when users click 'Run'
  observeEvent(input$runButton, {
    updateTabsetPanel(session, "allResults", 'result')
  })

  data <- reactiveValues()

  # Create a dataset based on users' selected variables
   data <- eventReactive(input$inputVars_rows_selected,{
       print("Select Some Vars")
       print(input$inputVars_rows_selected)
       rawdata[, c(input$inputVars_rows_selected)]
  })


  ### VARIABLE SELECTION ####

  var <- reactiveValues()

  # Select all vars
  observeEvent(input$selectAll,{
    print("SelectAll ObserveEvent")
    data <- rawdata
    var$selected <- 1:nrow(rawdata)
    print(var$selected)
    print(data)

  })

  # Deselect all vars
  observeEvent(input$deselectAll,{
    print("deselectAll ObserveEvent")
    data <- rawdata
    var$selected <- 0
    print(var$selected)
    print(data)

  })

  ### RESULT TAB ###

  result <- eventReactive (input$runButton, {
    head(data(),2)
  })

  ### RENDERING FUNCTIONS ###

  # Default SummaryTable
  output$inputVars <- DT::renderDataTable({
    if (input$selectAll==0 & input$deselectAll==0) {
      print("Default Summary Table")
      DT::datatable(rawdata, options = list(paging = FALSE, searching = FALSE))
    } 
    else {
      DT::datatable(rawdata, options = list(paging = FALSE, searching = FALSE), selection = list(target = 'row', selected = var$selected))
    }
  })

  # Display results
  output$result <- DT::renderDataTable({
    DT::datatable(result(), options = list(paging = FALSE, searching = FALSE))
  })

  output$temp <- renderPrint({
    print(input$selectAll)
    print(input$deselectAll)
    print(input$inputVars_rows_selected)

  })
}

2 个答案:

答案 0 :(得分:1)

一个原因是因为eventReactive被&#34;懒惰评估&#34;而不是立即评估的observeEvent

因此,在您的情况下,与observeEvent对应的deselectAll实际使用data(),以便reactiveEvent被触发。

  # Deselect all vars
  observeEvent(input$deselectAll,{
    print("deselectAll ObserveEvent")
    var$selected <- 0
    print(var$selected)
    print(data())
  })

但与observeEvent对应的selectAll不使用data(),因此reactiveEvent不会被触发:

  # Select all vars
  observeEvent(input$selectAll,{
    print("SelectAll ObserveEvent")
    var$selected <- 1:nrow(rawdata)
    print(var$selected)
  })

我建议进行以下更改

  • 如果您在此处添加print(data()),则会获得某些行为 正在寻求。

  • 但这仍然不完全正确,因为HubertL评论说 被覆盖的data的一个定义也是有效的 - 和 请注意,要判断数据被拉出的位置并不容易。 这是因为eventReactive已缓存了值,因此您print 如果正在使用缓存值,则可能不会显示 - 您的代码需要 执行以拉data()

  • 所以无论如何我肯定建议使用不同的名字(和 更具描述性而不仅仅是重复&#34;数据&#34;避免混淆。

  • 此外,您可能无需使用eventReactive 想要一个简单的reactive。如果你,通常需要eventReactive 我想避免&#34;反应&#34;来自所有其他反应变量 代码,我在这里看不到需要。

  • 我还建议将rawdata放入reactiveValues这样的内容:rv <- reactiveValues(rawdata=rawdata),然后将其用作rv$rawdata。这使得它具有反应性,然后使用它的东西将被触发并重新计算,如果它发生变化。

请参阅此链接(observeEvent vs. eventReactive) ,了解&#34; laziness&#34;那些命令。

答案 1 :(得分:1)

以下是可行的代码。 @Mike和@HubertL是对的。原因是因为反应是懒惰而不是观察事件。谢谢大家的帮助!

function(input, output, session) {

  # Activate tab 'Result' when users click 'Run'
  observeEvent(input$runButton, {
    updateTabsetPanel(session, "allResults", 'result')
  })  

  data <- reactive({
     print("Select Some Vars")
     print(input$inputVars_rows_selected)
     rawdata[input$inputVars_rows_selected,]
  })

  ### VARIABLE SELECTION ####

  var <- reactiveValues()

  # Select all vars
  observeEvent(input$selectAll,{
    print("SelectAll --- ObserveEvent")
    var$selected <- 1:nrow(rawdata)
    print(var$selected)
    print(input$inputVars_rows_selected)
  })

  # Deselect all vars
  observeEvent(input$deselectAll,{
    print("deselectAll --- ObserveEvent")
    var$selected <- 0
    print(var$selected)
  })

  ### RESULT TAB ###

  result <- eventReactive (input$runButton, {
    head(data(),5)
  })

  ### RENDERING FUNCTIONS ###

  # Default SummaryTable
  output$inputVars <- DT::renderDataTable({
    if (input$selectAll==0 & input$deselectAll==0) {
      print("Default Summary Table")
      DT::datatable(rawdata, options = list(paging = FALSE, searching = FALSE))
    } 
    else {
      DT::datatable(rawdata, options = list(paging = FALSE, searching = FALSE), selection = list(target = 'row', selected = var$selected))
    }
  })

  # Display results
  output$result <- DT::renderDataTable({
    DT::datatable(result(), options = list(paging = FALSE, searching = FALSE))
  })

  output$temp <- renderPrint({
    print(input$selectAll)
    print(input$deselectAll)
    print(input$inputVars_rows_selected)

  })
}