Alternatively you can use observe() which will respond to any hits to the input$datatable_rows_selected, including NULL.
To repurpose Kristoffer W. B.'s code:
library(shiny)
library(DT)
ui <- fluidPage(
DT::dataTableOutput("testtable")
)
server <- function(input, output) {
# Output iris dataset
output$testtable<- DT::renderDataTable(iris, selection = "single")
# Observe function that will run on NULL values
an_observe_func = observe(suspended=T, {
input$testtable_rows_selected
isolate({
#do stuff here
print(input$testtable_rows_selected)
})
})
#start the observer, without "suspended=T" the observer
# will start on init instead of when needed
an_observe_func$resume()
shinyApp(ui, server)
A few things to note:
1) I found it best to start the observer in suspended mode, that way it doesn't start when the program initializes. You can turn it on whenever you want it to... observe... (such as after you render the datatable, or before you'd like to start tracking selections).
2) Use isolate to stop the observer from tracking multiple elements. in this case the observer should only react to input$testtable_rows_selected, instead of everything else occurring. The symptom of this problem is that your observer fires multiple times on a single change.
observeEvent
does not trigger wheninput$segment_library_datatable_rows_selected
isNULL
. Have you tried a standard observe or reactive function (e.g.sel <- reactive({is.null(input$segment_library_datatable_rows_selected)})
). Also please specify if your DT has selection of multiple rows enabled and if you want to detect any changes in the row selection (e.g. also going from 3 to 2 selected rows). – Clientageprint(!is.null(input$segment_library_datatable_rows_selected))
– Holub