2016-02-27 47 views
2

我們閃亮的頁面有多個selectizeInput控件,其中一些在其下拉框中有長列表。因此,初始加載時間非常長,因爲它需要爲所有selectizeInput控件預填充下拉框。R Shiny - 使用updateSelectizeInput優化頁面加載時間

編輯:請參閱下面的示例顯示如何加載長列表影響頁面加載時間。請複製下面的代碼並直接運行以查看加載過程。

library(shiny) 
library(shinydashboard) 

ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"), 
dashboardSidebar(
selectizeInput("a","filter 1",choices = sample(1:100000, 10000),multiple = T), 
selectizeInput("b","filter 2",choices = sample(1:100000, 10000),multiple = T), 
selectizeInput("c","filter 3",choices = sample(1:100000, 10000),multiple = T), 
selectizeInput("d","filter 4",choices = sample(1:100000, 10000),multiple = T), 
selectizeInput("e","filter 5",choices = sample(1:100000, 10000),multiple = T), 
selectizeInput("f","filter 6",choices = sample(1:100000, 10000),multiple = T) 
       ), 
dashboardBody() 
) 

server <- function(input, output) { 
} 

shinyApp(ui, server) 

所以我想用戶點擊特定的複選框像see more filters後更新這些selectizeInput。但是,我不知道如何檢測它是否已經加載了列表。

爲了更清楚地解釋這一點,請參閱下面的解決方案來加載多個數據文件。

#ui 
checkboxInput("loadData", "load more data?", value = FALSE) 

#server 
#below runs only if checkbox is checked and it hasn't loaded 'newData' yet 
#So after it loads, it will not load again 'newData' 

if((input$loadData)&(!exists("newData"))){ 
    newData<<- readRDS("dataSample.rds") 
} 

但是,如果它是更新selectizeInput choises

#ui 
selectizeInput("filter1","Please select from below list", choices = NULL, multiple = TRUE) 

如何找到一個條件像我一樣用於檢測是否對象exsits exists("newData")?我試過is.null(input$filter1$choises)但它不正確。

感謝有關此情況的任何建議。

提前致謝!

+0

你能發表一個[可重現的例子](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) - 即編輯您的問題與示例數據和一個完整的'shinydashboard'結構,有人可以用來幫助回答你的問題 – SymbolixAU

+0

它不一定是你的整個代碼,只是一個最小的例子來重新創建你的問題 – SymbolixAU

+0

@Symbolix感謝您的提醒。我用一個例子更新了我的問題。 –

回答

4

最後,我從RStudio上的帖子中找到了解決方案。 http://shiny.rstudio.com/articles/selectize.html

# in ui.R 
selectizeInput('foo', choices = NULL, ...) 

# in server.R 
shinyServer(function(input, output, session) { 
updateSelectizeInput(session, 'foo', choices = data, server = TRUE) 
}) 

當我們在輸入框中鍵入,selectize將開始尋找那部分我們輸入的字符串相匹配的選項。當所有可能的選項都寫在HTML頁面上時,可以在客戶端執行搜索(默認行爲)。它也可以在服務器端完成,使用R匹配字符串並返回結果。這在選擇數量非常大時特別有用。例如,當對於選擇輸入有100,000個選擇時,將它們全部寫入頁面會很慢,但是我們可以從空選擇輸入開始,並且只獲取我們可能需要的選擇,這可以要快得多。我們將在下面介紹兩種類型的選擇輸入。