I have a shiny interface where I have two DT tables. I want the second table to render based on the output of a reactive expression (which works) combined with the selected data from the first table. However, when I write the observer for the second DT renderTable expression the entire observer does not react to changes in selected rows. I guess this is because the input$table_selected_rows is nowhere defined in the code but in some way inherent to the DT package (I only define input$table). The code for the second table is
row<-reactive({
print("check")
input$table_selected_rows
})
observe({
a=row()
print(a)
if(length(a)>0){
res=dtf()
ID=res[a,]
res=res[which(res$ID==ID),]
output$table2=DT::renderDataTable(dplyr::distinct(res),
selection=list(mode="single",target="row"))
}
})
observe({
row()
})
In the code above "check" is only printed once at the initialization of the code and not when I select rows from the first table in my session. How do I force shiny to monitor input$table_selected_rows so that the second table is reactive to the first table?
The problem in your code is, that you switched the input name:
table_selected_rows
should be table_rows_selected
. Please see chapter 2.1.1 here .
Here is a simple reproducible example:
library(shiny)
library(DT)
library(datasets)
ui <- fluidPage(
DTOutput("Tab1"),
DTOutput("Tab2")
)
server <- function(input, output, session) {
output$Tab1 <- renderDT({iris}, selection=list(mode = "single", target = "row"))
row <- reactive({input$Tab1_rows_selected})
output$Tab2 <- renderDT({data.frame(row())})
}
shinyApp(ui, server)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.