2017-01-03 86 views
1

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

有关信息此应用程序是能够更新的输入功能的第一个输入的选择,例如如果我在第一个输入中选择“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) 
+0

我不确信整个你的目标是什么,但'updateSelectInput'电话似乎没有必要给我。如果你把它们拿出来,你会得到你想要的东西吗? – Benjamin

+0

不,我需要updateSelectInput更新我的选择,取决于最后的选择 –

回答

2

如果我理解了正确的问题,这应该工作。

UPDATE(添加第三个变量):

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) 
+0

谢谢,还有一个问题,如果我有一个更多的过滤器例如在一个变量“字母”上,我创建了一个df2 < - df1或不是?谢谢 –

+0

@DimitriPetrenko,不应该需要'df2'。我创建了一个副本来使用'updateSelectInput',以便您仍然拥有所有选项使用原始的'df'和'df1'来选择'Letters',只显示那些'Numbers',根据'filter1'的选择。希望这是有道理的。 – krish

+0

是的我同意你的观点,但如果我想过滤一个第三个变量(filter3)让我们拿“字母”,我必须在df2中复制df1,或者我误解了你?谢谢 –

2

只要改变selectInputs像下面,应该没有选择工作,“所有':

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) 
+0

不幸的是,这消除了在一次点击中选择所有字母或数字的能力。我不确定是否需要移除该功能。 – Benjamin

+0

@Benjamin我认为OP不希望这种功能,因为他想删除'全部'的选择。 –

+0

你是对的。我误解了这个问题。 – Benjamin