使用 click.dt 和滑块在点击后不更新进行过滤

using click.dt with sliders to filter after a click not updating

请运行此代码。当您单击数据table 中的一行时,您会看到绘制了"a" 列中的数字。这是使用回调和 observeEvent 完成的。那很好用。

library(shiny)
library(DT)
runApp(shinyApp(
  ui = fluidPage(
                   DT::dataTableOutput('table')  ,
                   selectInput("NUMBER", "Number:", c(1,100,1000000),selected = 1),
                   plotOutput("p")
                   ),
  server = function(input, output, session) {

    NUMBER = reactive({
      input$NUMBER
    })

    output$table <- DT::renderDataTable({
      datatable(data.frame(a = c(85,7),b=c(444,2)), rownames = FALSE, selection = 'none', callback=

      JS("table.on('click.dt', 'tr', function() {
           var data=table.row(this).data();
           Shiny.onInputChange('rows',data[0]);
          });")
      )}
    )

    observeEvent(input$rows, {

      print( input$rows)   
      print( class( input$rows)  )
      a = as.numeric(input$rows)

      print(a)
      print(NUMBER())
      new_number = a      #THIS WORKS!!!
      #new_number = a  * as.numeric(NUMBER())    #THIS DOES NOT WORK     multiple a by the number in the slider when the SLIDER Is CHANGED

      output$p = renderPlot({

        # browser()
        plot( new_number )
      })

    })}
))

但您还会看到一个 "NUMBER" 滑块输入。当用户更改滑块时,我希望绘图更新,以便绘图显示滑块中数字 select 的倍数。所以:

注释掉这一行:

new_number = a 

并取消注释这一行:

new_number = a  * as.numeric(NUMBER()) 

现在重新运行 代码,您会看到 select 来自滑块的不同数字,但没有任何反应。这是因为

new_number = a  * as.numeric(NUMBER()) 

位于 ObserveEvent 函数内部,table 未被单击...只是更改了滑块。那么如何提供 NUMBER() 才能使其有效?

谢谢。

您所要做的就是将一个 observeEvent 放入您的 observeEvent :) 这样在渲染新绘图时 R 会获取更新的输入。希望这对您有所帮助!

 library(shiny)
    library(DT)

    runApp(shinyApp(

  ui = fluidPage(
    DT::dataTableOutput('table')  ,
    selectInput("NUMBER", "Number:", c(1,100,1000000), selected = 1),
    plotOutput("p")
  ),

  server = function(input, output, session) {

    NUMBER = reactive({
      input$NUMBER
    })

    output$table <- DT::renderDataTable({
      datatable(data.frame(a = c(85,7),b=c(444,2)), rownames = FALSE, selection = 'none', callback=

                  JS("table.on('click.dt', 'tr', function() {
                     var data=table.row(this).data();
                     Shiny.onInputChange('rows',data[0]);
    });")
      )}
      )

    observeEvent(input$rows, {

      print( input$rows)   
      print( class( input$rows)  )
      a = as.numeric(input$rows)
      new_number <- a

      observeEvent(input$NUMBER, {

        print(a)
        print(NUMBER())

        number = a  * as.numeric(NUMBER())    

        output$p = renderPlot({

          # browser()
          plot( number )
        })

      })

      output$p = renderPlot({

        # browser()
        plot( new_number )

      })

    })

    }
   ))