2017-02-27 118 views
1

我有一個閃亮的應用程序使用UpdateselectInput,我想添加一個actionButton,因爲有一些bug單獨updateselectInput。 它似乎沒有工作,我要顯示該表只如果我操作的按鈕 我的應用程序是與此類似:在一個閃亮的應用程序中使用actionButton和updateSelectInput

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)), 
       actionButton("go_button", "GO !")), 

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

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

    output$tableprint <- DT::renderDataTable({ 
    input$go_button 
    # 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

不太清楚你在找什麼。你想添加一個新的'actionButton'並且只在你點擊按鈕時才顯示錶格嗎?另外'updateSelectInput'有什麼問題? – krish

+0

是的,我只想點擊按鈕就顯示我的表格。但在我的例子中,該表自動... 我的真實數據是相當大的,當我過濾它時,出現了一些錯誤,當我嘗試改變變量篩選器中的模態時,應用程序卡住了,我必須關閉它來解決問題。我認爲一個動作按鈕可以完成這項工作 –

回答

0

您是否在尋找類似的東西?只有當您點擊Go按鈕時,表格纔會顯示。過濾器的工作方式是一樣的。

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)), 
       actionButton("go_button", "GO !")), 

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

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


    goButton <- eventReactive(input$go_button,{ 
    # 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) 
    }) 

    output$tableprint <- DT::renderDataTable({ 
    goButton() 

    }) 
} 

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

我已經移動所述過濾器代碼到一個eventReactive功能。所以當你點擊按鈕時,它會根據過濾器對數據進行分類。而output$tableprint函數調用這個反應函數,所以只有當你點擊按鈕時纔會看到表格。

+0

非常感謝你,但我想保留'updateselectinput()'函數來更新我的過濾器,例如我想在過濾器2選擇中只顯示1和27,如果我選擇A in第一過濾器 –

相關問題