没有“全部”的闪亮输入选择

时间:2017-01-03 12:49:49

标签: r shiny

我有一个类似于这个例子的闪亮应用程序,我想要的是在没有“全部”选择​​的情况下在输入中获得选择。 例如,在这里,如果我删除“所有”所有其他选择消失,我不想要这个......

有关信息,此应用程序能够根据第一个输入更新输入选项,例如,如果我在第一个输入中选择“A”,我将在第二个输入选择中仅看到“1”。

@Benjamin:是的,这是保持我的更新功能,就像我的例子

一样

提前致谢

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

ui <- fluidPage(

  titlePanel("Title"),

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

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

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

  output$tableprint <- DT::renderDataTable({

    # Data
    df <- tibble(Letters = LETTERS, Numbers = as.character(seq.int(1, length(letters), 1)))

    # Create filters based on inputs
    f1 <- if("All" %in% input$filter1) LETTERS else input$filter1
    f2 <- if("All" %in% input$filter2) as.character(seq.int(1, length(letters), 1)) else input$filter2

    # Filter data
    filtered_df <- filter(df, 
                          Letters %in% f1,
                          Numbers %in% f2)

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

    datatable(filtered_df)

  })
}

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

2 个答案:

答案 0 :(得分:2)

只需像下面这样更改selectInput,它就可以无需选择即可工作&#39;所有&#39;:

selectInput("filter1", "Filter 1", multiple = T, choices = LETTERS, selected = NULL),
selectInput("filter2", "Filter 2", multiple = T, 
            choices = as.character(seq.int(1, length(letters), 1)), selected = NULL)

答案 1 :(得分:2)

如果我理解正确的问题,这应该有用。

更新(添加第三个变量):

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

ui <- fluidPage(

  titlePanel("Title"),

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

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

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

  output$tableprint <- DT::renderDataTable({

    # Data
    df <- tibble(LETTERS = LETTERS, Numbers = as.character(seq.int(1, length(letters), 1)),
                 letters = letters)

    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)

  })
}

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