【问题标题】:Filter table with action button and 2nd table with crosstalk带有操作按钮的过滤表和带有串扰的第二个表
【发布时间】:2018-10-25 18:36:57
【问题描述】:

我有一个带有 1 个输入选择、一个操作按钮和两个表格的 flexdashboard。我希望使用由操作按钮触发的输入选择来过滤第一个表。然后我想根据表 1 中的选定行过滤第二个表。对于后者,我正在考虑使用串扰。

这行得通(使用示例来自:stackoverflow.com/questions/48581598/filter-two-tables-with-crosstalk):

df1 <- structure(list(owner = structure(c(1L, 2L, 2L, 2L, 2L), .Label = c   ("John", "Mark"), class = "factor"), hp = c(250, 120, 250, 100, 110), car =    structure(c(2L, 2L, 2L, 1L, 1L), .Label = c("benz", "bmw"), class = "factor"),  id = structure(1:5, .Label = c("car1", "car2", "car3", "car4", "car5"), class = "factor")), .Names = c("owner", "hp", "car", "id"), row.names = c(NA, -5L), class = "data.frame")

df2 <- structure(list(car = structure(c(1L, 2L, 1L, 2L), .Label = c  ("benz","bmw"), class = "factor"), owner = structure(c(1L, 1L, 2L, 2L), .Label  = c("John", "Mark"), class = "factor"), freq = c(0L,1L, 2L, 2L)), .Names = c  ("car", "owner", "freq"), row.names = c(NA, -4L), class = "data.frame")

shared_df1 <- SharedData$new(df1, ~owner, group = "Choose owner")
shared_df2 <- SharedData$new(df2, ~owner, group = "Choose owner")

filter_select("owner", "Car owner:", shared_df1, ~owner)

DT::datatable(shared_df1)
DT::datatable(shared_df2)
#---------------------------

但添加它不会:

selectInput(inputId = 'owner', label = NULL, choices =c("All", "John", "Mark"), selected ="All")

actionButton("Find", "Find owner ")

observeEvent(input$Find,{
df1<-df1%>%filter(a==input$owner|input$owner=="All")
})

请帮忙

【问题讨论】:

    标签: r datatable shiny flexdashboard


    【解决方案1】:

    不清楚您想对第二张表做什么。至于第一个,您可以尝试创建一个返回过滤数据帧的反应数据帧:

    df1_filtered = reactive({
      if(input$owner != "All"){
        df1 %>% filter(owner == input$owner)
      } else {
        df1
      }
    })
    
    df2_filtered = reactive({
      if(condition for second table){
        # If you wanted to filter the second owners in the first df
        df2 %>% filter(owner %in% df1_filtered()$owner)
      } else {
        df2
      }
    })
    
    
    observeEvent(input$Find,{
      renderTable(df1_filtered())
    })
    

    但是让我知道你想要做什么,我可以修改它。此外,我也不是 100% 确定旧的 if-else 框架是响应式数据帧的最佳实践,但它对我来说效果很好。

    【讨论】:

      猜你喜欢
      • 2018-07-12
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 2022-11-08
      • 2012-08-09
      • 2013-05-05
      • 2017-12-18
      • 2013-12-24
      相关资源
      最近更新 更多