【发布时间】:2021-03-19 07:58:31
【问题描述】:
我正在尝试使用其他反应对象“#3”过滤反应对象“#4”,但没有成功,我认为问题在于它们都依赖于另一个反应对象“#2”。这张照片应该有帮助:
这里是reprex:
library(shiny)
library(DT)
library(dplyr)
dat <- as.data.frame( list(
X = c("A", "A", "B", "B", "C"),
Y = c(1,2,3,4,5)
))
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("myinput", "Input:",min = 1, max = 5,value = 3)
),
mainPanel(
br(),
fluidRow(column(2, DTOutput('table_data'))),
br(),
fluidRow(column(2, DTOutput('table_filtered'))),
br(),
fluidRow(column(2, DTOutput('table_filtered_not_A'))),
br(),br(),
fluidRow(column(2, DTOutput('table_grouped')))
)
)
)
server <- function(input, output) {
dat_rv <- reactiveValues(df = dat)
dat_filtered <- reactive({
dat_rv$df %>%
filter(
!isTruthy( input$myinput ) | Y <= input$myinput
)
})
dat_not_A <- reactive({
dat_not_A <- dat_filtered() %>%
filter(X != "A") %>%
select(X)
})
dat_grouped <- reactive({
dat_grouped <- dat_filtered() %>%
filter(X %in% dat_not_A()) %>% # HERE IS THE PROBLEM?
group_by(X) %>%
summarise(Y = sum(Y))
return(dat_grouped)
})
output$table_data = renderDT(dat, options = list(dom = 't'), rownames = FALSE)
output$table_filtered = renderDT(dat_filtered(), options = list(dom = 't'), rownames = FALSE)
output$table_filtered_not_A = renderDT(dat_not_A(), options = list(dom = 't'), rownames = FALSE)
output$table_grouped = renderDT(dat_grouped(), options = list(dom = 't'), rownames = FALSE)
}
shinyApp(ui = ui, server = server)
我也尝试过使用隔离,但没有奏效。我错过了什么吗?
最好的问候。
【问题讨论】:
-
您不应该从
X过滤吗?filter(X %in% unique(dat_not_A()$X)) .... -
好收获!现在它可以工作了......我需要看看真实的例子,因为它没有
-
我将编辑问题并添加答案,以便所有这些都有意义。谢谢你的收获!
标签: r shiny shiny-reactivity