Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
意外行为全选和取消全选操作按钮(R)_R_Shiny - Fatal编程技术网

意外行为全选和取消全选操作按钮(R)

意外行为全选和取消全选操作按钮(R),r,shiny,R,Shiny,我遇到了意想不到的行为。我打算做的是: -当用户单击全选时,汇总表中的所有行都被选中。这很有效。但是,下面的代码不会被调用 data <- eventReactive(input$selectAll,{ print("Select All - restore data") rawdata }) 已更新服务器。R 2: @Mike和@HubertL,我认为你是对的:问题是由缓存值引起的。在此更新版本中,observeEvent对应于按预期选择全部和取消选择所有

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

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

  })
已更新服务器。R 2: @Mike和@HubertL,我认为你是对的:问题是由缓存值引起的。在此更新版本中,observeEvent对应于按预期选择全部和取消选择所有工作。但是,现在不会调用与输入$inputVars\u rows\u selected相对应的eventReactive。知道为什么吗

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)

  })
}
一个原因是,EventResponsive是惰性评估的,而ObserveeEvent是立即评估的

因此,在您的示例中,与DeceloseAll相对应的observeEvent实际使用数据,因此会触发reactiveEvent

  # Deselect all vars
  observeEvent(input$deselectAll,{
    print("deselectAll ObserveEvent")
    var$selected <- 0
    print(var$selected)
    print(data())
  })
我建议进行以下修改

如果您在此处添加printdata,您将获得您需要的一些行为 我们正在寻找

但这仍然不是完全正确的,因为HubertL的评论 覆盖数据的一个定义也是有效的,并且 注意,要知道数据被拉到哪里并不容易。 这是因为eventReactive具有缓存的值,所以打印 如果正在使用缓存值,则可能不会显示-您的代码需要 执行以提取该数据

所以在任何情况下,我都会建议使用不同的名称和名称 比重复数据更具描述性,以避免混淆

另外,您可能不需要在这里使用EventResponsive 想要一个简单的反应。如果您需要,则通常需要EventResponsive 希望避免中所有其他反应变量的反应 代码,我觉得这里没有必要


我还建议将rawdata放入一个reactiveValues中,如下所示:rv是有效的代码@迈克和胡贝尔是对的。原因是被动是懒惰的,而观察者不是。谢谢大家的帮助

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)

  })
}

一个名称只有一个对象:不能将多个反应函数分配给同一个变量名,这里只考虑最后一个分配。考虑做一个单一的事件反应函数,并使用if语句返回一个不同的选择,根据输入的,似乎不是真的…实际上可能是真实的,但很难说哪些数据被拉因为事件反应缓存。仍然无法触发reactiveEvent。我把更新后的代码放在我原来的帖子里。谢谢奇怪的是,我从未收到过关于这一评论的通知。现在才看到。今晚晚些时候或明天早上我会看一看。很高兴你把它用上了。理解它是如何工作的是一个挑战。
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"))
      )
  )

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

  })
}
  # Deselect all vars
  observeEvent(input$deselectAll,{
    print("deselectAll ObserveEvent")
    var$selected <- 0
    print(var$selected)
    print(data())
  })
  # Select all vars
  observeEvent(input$selectAll,{
    print("SelectAll ObserveEvent")
    var$selected <- 1:nrow(rawdata)
    print(var$selected)
  })
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)

  })
}