R 闪亮观察行取消选择数据表

Zer*_*ack 7 r observable shiny dt

我有一个闪亮的应用程序,它有一个 DT::renderDataTable,用户可以在数据表中选择一行。

以下代码将仅打印 FALSE(当选择一行时):

observeEvent(input$segment_library_datatable_rows_selected, {
  print(is.null(input$segment_library_datatable_rows_selected))
})
Run Code Online (Sandbox Code Playgroud)

当一行也被取消选择时,如何打印它?(打印值将为 TRUE)

小智 15

observeEvent(input$selected,ignoreNULL = FALSE,{...})
Run Code Online (Sandbox Code Playgroud)

ignoreNULL默认为TRUE. 设置为FALSE在取消选择时观察事件。

  • 不知道为什么你的答案被否决了 - 当我根据从 DataTable 表中选择的行添加和删除绘图中的跟踪时,这非常有帮助。如果没有“ignoreNULL = FALSE”,我的代码将删除直到最后一行的所有跟踪,这将无法删除跟踪,因为“_row_selected”不会触发 (2认同)

Kri*_*ing 3

据我了解,一个工作的最小示例如下(如果选择了sel()一行,则反应为 TRUE ):datatable

library(shiny)
library(DT)
ui <- fluidPage(
  DT::dataTableOutput("datatable"),
  textOutput("any_rows_selected")
)
server <- function(input, output) {
  # Output iris dataset
  output$datatable <- DT::renderDataTable(iris, selection = "single")
  # Reactive function to determine if a row is selected
  sel <- reactive({!is.null(input$datatable_rows_selected)})  
  # Output result of reactive function sel
  output$any_rows_selected <- renderText({
    paste("Any rows selected: ", sel())
  })
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)