2017-01-09 67 views
1

我想你喲建立一個動態的應用程序可以過濾數據表,我想要的是當我選擇我的第一個變量的形式我的下一個過濾器將更新,目的只有模式對應於我的第一個過濾器和第三個過濾器相同 我開始了一個反應的應用程序,但它似乎不工作,因爲我必須始終保持「全部」的選擇,以顯示其他模態,然後刪除它...有可能這樣做嗎? 所以我決定添加一個動作按鈕,但似乎不能很好地與我的更新輸入閃亮的過濾器更新並不顯示所有變量的形式

我該如何解決它?由於

我的應用程序的一個例子:

library(shiny) 
library(dplyr) 
library(DT) 

ui <- fluidPage(

    titlePanel("Title"), 

    sidebarLayout(
    sidebarPanel(width=3, 
       selectInput("filter1", "Filter 1", multiple = T, choices = c("All", LETTERS)), 
       selectInput("filter2", "Filter 2", multiple = T, choices = c("All", as.character(seq.int(1, length(letters), 1)))), 
       selectInput("filter3", "Filter 3", multiple = T, choices = c("All", letters)), 
       actionButton("goButton", "Go!"), 
       p(class = 'text-center', downloadButton('dl', 'Download Data')) 
    ), 

    mainPanel(
     DT::dataTableOutput("tableprint") 
    ) 
) 
) 

server <- function(input, output, session) { 

    output$tableprint <- DT::renderDataTable({ 

    # Data 
    df <- tibble(LETTERS = rep(LETTERS, 2), Numbers = as.character(1:52), 
       letters = paste(LETTERS, Numbers, sep = "")) 

    df1 <- df 

    if("All" %in% input$filter1){ 
     df1 
    } else if (length(input$filter1)){ 
     df1 <- df1[which(df1$LETTERS %in% input$filter1),] 
    } 

    if("All" %in% input$filter2){ 
     df1 
    } else if (length(input$filter2)){ 
     df1 <- df1[which(df1$Numbers %in% input$filter2),] 
    } 

    if("All" %in% input$filter3){ 
     df1 
    } else if (length(input$filter3)){ 
     df1 <- df1[which(df1$letters %in% input$filter3),] 
    } 

    input$goButton 

    # Update selectInput choices based on the filtered data. Update 'selected' to reflect the user input. 
    updateSelectInput(session, "filter1", choices = c("All", df$LETTERS), selected = input$filter1) 
    updateSelectInput(session, "filter2", choices = c("All", df1$Numbers), selected = input$filter2) 
    updateSelectInput(session, "filter3", choices = c("All", df1$letters), selected = input$filter3) 

    datatable(df1) 

    }) 

    output$dl <- downloadHandler('mydata.csv', content = function(file) { 

    # Data 
    df <- tibble(LETTERS = rep(LETTERS, 2), Numbers = as.character(1:52), 
       letters = paste(LETTERS, Numbers, sep = "")) 

    df1 <- df 

    if("All" %in% input$filter1){ 
     df1 
    } else if (length(input$filter1)){ 
     df1 <- df1[which(df1$LETTERS %in% input$filter1),] 
    } 

    if("All" %in% input$filter2){ 
     df1 
    } else if (length(input$filter2)){ 
     df1 <- df1[which(df1$Numbers %in% input$filter2),] 
    } 

    if("All" %in% input$filter3){ 
     df1 
    } else if (length(input$filter3)){ 
     df1 <- df1[which(df1$letters %in% input$filter3),] 
    } 


    # Update selectInput choices based on the filtered data. Update 'selected' to reflect the user input. 
    updateSelectInput(session, "filter1", choices = c("All", df$LETTERS), selected = input$filter1) 
    updateSelectInput(session, "filter2", choices = c("All", df1$Numbers), selected = input$filter2) 
    updateSelectInput(session, "filter3", choices = c("All", df1$letters), selected = input$filter3) 

    datatable(df1) 

    write.csv(df1, file) 
    }) 
} 

# Run the application 
shinyApp(ui = ui, server = server) 
+0

不知道你想要什麼:你的過濾器如預期更新 – HubertL

+0

在第一個過濾器中選擇A,然後嘗試在第二個過濾器中選擇2個模式,我無法修復它,我需要添加「全部」方式。當我選擇1時,我不能選擇27 ... –

+0

因此,只需在第一個過濾器之後和另外兩個過濾器之前移動'updateSelectInput',以便df1仍然包含所有模態。但添加過濾器是一個AND – HubertL

回答

3

當我Filter1選擇A和B,該數據集是LETTERSAB子集。 Filter2中的選項是1,2,27,28Filter3A1, B2, A27, B28。當我在Filter2中選擇1時,Filter3中的選項爲A1,當Filter2中也選擇2時,更新爲A1A27作爲選項。您沒有All選件Filter2Filter3。這是你所期望的嗎?

library(shiny) 
library(dplyr) 
library(DT) 

ui <- fluidPage(

    titlePanel("Title"), 

    sidebarLayout(
    sidebarPanel(width=3, 
       selectInput("filter1", "Filter 1", multiple = TRUE, choices = c("All", LETTERS)), 
       selectInput("filter2", "Filter 2", multiple = TRUE, choices = c("All", as.character(seq.int(1, length(letters), 1)))), 
       selectInput("filter3", "Filter 3", multiple = TRUE, choices = c("All", letters)) ), 

    mainPanel(
     DT::dataTableOutput("tableprint") 
    ) 
) 
) 

server <- function(input, output, session) { 

    output$tableprint <- DT::renderDataTable({ 

    # Data 
    df <- tibble(LETTERS = rep(LETTERS, 2), Numbers = as.character(1:52), 
       letters = paste(LETTERS, Numbers, sep = "")) 

    df1 <- df 

    if("All" %in% input$filter1){ 
     df1 
    } else if (length(input$filter1)){ 
     df1 <- df1[which(df1$LETTERS %in% input$filter1),] 
    } 

    # Update selectInput choices based on the filtered data. Update 'selected' to reflect the user input. 
    updateSelectInput(session, "filter1", choices = c("All", df$LETTERS), selected = input$filter1) 
    updateSelectInput(session, "filter2", choices = c("All", df1$Numbers), selected = input$filter2) 



    if("All" %in% input$filter2){ 
     df1 
    } else if (length(input$filter2)){ 
     df1 <- df1[which(df1$Numbers %in% input$filter2),] 
    } 
    updateSelectInput(session, "filter3", choices = c("All", df1$letters), selected = input$filter3) 

    if("All" %in% input$filter3){ 
     df1 
    } else if (length(input$filter3)){ 
     df1 <- df1[which(df1$letters %in% input$filter3),] 
    } 
    datatable(df1) 

    }) 
} 

# Run the application 
shinyApp(ui = ui, server = server) 

此代碼不包括操作按鈕。

+0

謝謝是的,這是它的功能性,只是我想也有「所有」的選擇un過濾器 –

+1

@DimitriPetrenko,更新了代碼。 – krish

+0

非常感謝你:D –