闪亮:根据另一个数据表上的click事件创建动态数据表

时间:2019-03-28 10:35:15

标签: r shiny shinydashboard reactive

我正在创建一个具有反应性值的闪亮应用,当我选择一行时,
1.)它将获取节点并
2.)在整个数据框中找到与该节点关联的所有路由
3.)通过以上所选路线过滤原始df。

df <- data.frame("Route" = c("R2","R2","R2","R3","R2","R1","R2","R3","R1"),
             "Nodes" = c("N1","N2","N2","N3","N2","N1","N3","N3","N2"),
             "X1" = c(81,65,61,64,59,51,69,95,99),
             "X2" = c(7,8,15,1,5,10,4,7,8))


 library(shiny)
 library(shinydashboard)
 library(DT)



 ui <- dashboardPage(
 dashboardHeader(title = "Dynamic Table"),
 dashboardSidebar(sidebarMenu(menuItem("Route", tabName = "tab_01"))),
 dashboardBody(tabItems(
 tabItem(tabName = "tab_01",
        tabsetPanel(
          tabPanel("T1", DT::DTOutput("table_01")),
          tabPanel("T2", DT::DTOutput("table_02"))
        )
        )))
 )


server <- function(input, output){
output$table_01 <- renderDT(datatable(data = df, selection = "single"))




output$table_02 <- renderDT(datatable({
s=input$table_01_rows_selected
n <- unique(df[s,c("Nodes")])
data = df[df$Nodes %in% n,]
}))

}

shinyApp(ui, server)

所以基本上,如果我选择第4行,即节点“ N3”。与“ N3”关联的路由是“ R2”和“ R3”。最后,我必须过滤df中具有“ R2”和“ R3”的所有路由。

1 个答案:

答案 0 :(得分:0)

这是您要寻找的吗?

output$table_02 <- renderDT({
    #  selected row from tab1
    selected_row=input$table_01_rows_selected
    # store its node value
    node <- unique(df[selected_row, "Nodes"])
    # find all the routes that are linked with the stored node value
    routes =  df[df$Nodes %in% node,]
    # show only those observation having the route id that we found in the previous step
    datatable(
      df[df$Route %in% routes$Route,]
    )
  })