闪亮的相互依赖滤波器值

时间:2018-01-11 14:27:51

标签: r shiny

无法使过滤器的相似功能相互依赖。这意味着如果用户从一个过滤器中选择一个输入,则所有其他过滤器都应该更新。

我已经尝试了多种闪亮的方法但无法这样做但是在stackoverflow上找到了一些具有类似功能的代码。唯一的挑战是我不想将表格显示为输出,不幸的是,如果我们不将输出传递给#tableprint [表格的id],则代码不起作用。

任何帮助都会非常感激。

 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)

1 个答案:

答案 0 :(得分:2)

你可以这样做:它更清洁,更容易阅读。请注意,我添加了shinyWidgets包,其中包含预先构建的Select-All按钮。您可以在其他被动反应中使用名为v$df的变量,如您所说 I dont want to show the table as output

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

# Install shinyWidgets
# From CRAN
#install.packages("shinyWidgets")

# From Github
# install.packages("devtools")
#devtools::install_github("dreamRs/shinyWidgets")

df <- tibble(LETTERS = rep(LETTERS, 2), Numbers = as.character(1:52),letters = paste(LETTERS, Numbers, sep = ""))
ui <- fluidPage(
  titlePanel("Title"),

  sidebarLayout(
    sidebarPanel(width=3,
                 pickerInput("filter1", "Filter 1", choices = LETTERS, options = list(`actions-box` = T), multiple = T),
                 pickerInput("filter2", "Filter 2", choices = df$Numbers, options = list(`actions-box` = T), multiple = T),
                 pickerInput("filter3", "Filter 3", choices = letters, options = list(`actions-box` = T), multiple = T)),

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

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

  v <- reactiveValues()

  observe({
    dt <- df$Numbers[df$LETTERS %in% input$filter1]
    updatePickerInput(session, "filter2", choices = dt,selected = dt)
  })

  observe({
    dt <- df$letters[df$Numbers %in% input$filter2]
    updatePickerInput(session, "filter3", choices = dt,selected = dt)
  })

  output$tableprint <- DT::renderDataTable({
    df <- df[df$LETTERS %in% input$filter1,]
    df <- df[df$Numbers %in% input$filter2,]
    df <- df[df$letters %in% input$filter3,]
    v$df <- df
    datatable(df)
  })
}

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