[英]R Shiny Datatable: prevent deselection of an already selected row when you click on it again
[英]R shiny observe for row deselection dataTable
我有一个闪亮的应用程序,它有一个 DT::renderDataTable,用户可以在数据表中选择一行。
The following bit of code will only print FALSE (when a row is selected):
observeEvent(input$segment_library_datatable_rows_selected, {
print(is.null(input$segment_library_datatable_rows_selected))
})
当一行也被取消选择时,如何让它打印? (打印值为 TRUE)
observeEvent(input$selected,ignoreNULL = FALSE,{...})
ignoreNULL
默认为TRUE
。 设置为FALSE
以在取消选择时观察事件。
据我所知一个工作最小的例子是下面的( 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)
或者,您可以使用observe() 来响应对input$datatable_rows_selected 的任何命中,包括NULL。
重新利用 Kristoffer WB 的代码:
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)
需要注意的几点:
1)我发现最好在挂起模式下启动观察者,这样在程序初始化时它就不会启动。 您可以随时打开它……观察……(例如在渲染数据表之后,或在您想要开始跟踪选择之前)。
2)使用isolate来阻止观察者跟踪多个元素。 在这种情况下,观察者应该只对 input$testtable_rows_selected 做出反应,而不是其他所有发生的事情。 此问题的症状是您的观察者在一次更改时多次触发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.