У меня есть блестящее приложение, я хотел реагировать на выбор входов и отображать данные, когда я нажимаю кнопку «Go». Для входных данных я хочу иметь выбор между «Все значения» моей переменной и каждым значением. У меня проблема с исправлением моего приложения.
Первая попытка
library(shiny)
library(dplyr)
library(DT)
# my data
bdd <- tibble(BA = rep(LETTERS, 2), MA = as.character(1:52),
YES = paste(BA, MA, sep = ""))
ui <- fluidPage(
titlePanel("BA"),
column(4,
uiOutput("filter1"),
uiOutput("filter2"),
uiOutput("filter3"),
actionButton("button", "GO")),
column(8,
DT::dataTableOutput("my_table"))
)
server <- function(input, output, session) {
All_BA <- reactive({
bdd %>% distinct(BA)
})
# my reactives inputs for filter 1
output$filter1 <- renderUI({
selectInput("filter1", "Filtre numéro 1",
choices = c("All_BA", bdd %>% select(BA)))
})
All_MA <- reactive({
bdd %>% filter(BA %in% input$filter1) %>%
distinct(MA)
})
# my reactives inputs for filter 2
output$filter2 <- renderUI({
selectInput("filter2", "Filtre numéro 2",
choices = c("All_MA", bdd %>% filter(BA %in% input$filter1) %>% select(MA)),
selected = "All_MA")
})
All_Y <- reactive({
bdd %>% filter(BA %in% input$filter1 |
MA %in% input$filter2) %>% distinct(YES)
})
# my reactives inputs for filter 3
output$filter3 <- renderUI({
selectInput("filter3", "Filtre numéro 3",
choices = c("All_Y", bdd %>% filter(BA %in% input$filter1,
MA %in% input$filter2) %>% select(YES)),
selected = "All_Y")
})
df <- eventReactive(input$button, {
bdd %>% filter(BA %in% input$filter1,
MA %in% input$filter2,
YES %in% input$filter3)
})
output$my_table <- DT::renderDataTable({
df()
})
}
# Run the application
shinyApp(ui = ui, server = server)
вторая попытка (не сработала из-за проблемы с реактивностью, и код не кажется оптимизированным)
library(shiny)
library(dplyr)
library(DT)
# my data
bdd <- tibble(BA = rep(LETTERS, 2), MA = as.character(1:52),
YES = paste(BA, MA, sep = ""))
ui <- fluidPage(
titlePanel("BA"),
column(4,
uiOutput("filter1"),
uiOutput("filter2"),
uiOutput("filter3"),
actionButton("button", "GO")),
column(8,
DT::dataTableOutput("my_table"))
)
server <- function(input, output, session) {
All_BA <- reactive({
bdd %>% distinct(BA)
})
# my reactives inputs for filter 1
if(input$filter1 == "All_BA"){
bdd <- reactive({
bdd %>%
filter(BA %in% All_BA())
})
}else{
bdd <- reactive({
bdd %>%
filter(BA %in% input$filter1)
})
}
output$filter1 <- renderUI({
selectInput("filter1", "Filtre numéro 1",
choices = c("All_BA", bdd() %>% select(BA)))
})
All_MA <- reactive({
bdd() %>% filter(BA %in% input$filter1) %>%
distinct(MA)
})
# my reactives inputs for filter 2
if(input$filter2 == "All_MA"){
bdd2 <- reactive({
bdd() %>%
filter(MA %in% All_MA())
})
}else{
bdd2 <- reactive({
bdd() %>%
filter(MA %in% input$filter2)
})
}
output$filter2 <- renderUI({
selectInput("filter2", "Filtre numéro 2",
choices = c("All_MA", bdd2() %>% select(MA)),
selected = "All_MA")
})
All_Y <- reactive({
bdd2 %>% filter(BA %in% input$filter1 |
MA %in% input$filter2) %>% distinct(YES)
})
# my reactives inputs for filter 3
if(input$filter3 == "All_Y"){
bdd3 <- reactive({
bdd2() %>%
filter(YES %in% All_Y())
})
}else{
bdd3 <- reactive({
bdd2() %>%
filter(YES %in% input$filter3)
})
}
output$filter3 <- renderUI({
selectInput("filter3", "Filtre numéro 3",
choices = c("All_Y", bdd3() %>% select(YES)),
selected = "All_Y")
})
df <- eventReactive(input$button, {
bdd %>% filter(BA %in% input$filter1,
MA %in% input$filter2,
YES %in% input$filter3)
})
output$my_table <- DT::renderDataTable({
df()
})
}
# Run the application
shinyApp(ui = ui, server = server)