2014-10-27 112 views
0

我有一個閃亮的selectInput面板。直到現在,我只處理selectInput中的選項的固定值。根據閃亮的其他條件改變selectinput中的選項R

現在我想要根據閃亮的Ui中的其他條件來改變這些選擇。

例子:

Ui.R

shinyUI(fluidPage(
fluidRow(column(3, 
wellPanel(
        h4("Data Upload"), 
        fileInput('file1', h5('Choose Your Model Data'), accept=c('text/csv','text/comma-separated-values,text/plain','.OUT')), 
        fileInput('file2', h5('Choose Your Observation Data'), accept=c('text/csv','text/comma-separated-values,text/plain','.xlsx'))  
       ), 
wellPanel(uiOutput("check")))) 

Server.R

shinyServer(function(input, output) { 
output$check <- renderUI({ 
    selectInput("check", label = h4("Dataset Selection"), choices = c("Model" = 1, "Observation" = 2, "Both" = 3), selected = 1, multiple = F) 
    }) 
a <- reactive({ 
    fileinput1 <- input$file1 
    if (is.null(fileinput1)) 
    return(NULL) 
    read.table(fileinput1$datapath, header = TRUE, col.names = c("Ei","Mi","hours","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","par","Temp","Sal","co2atm","u10","dicfl","co2ppm","co2mol","pH")) 
}) 

#Upload Observation Data 

b <- reactive({ 
    fileinput2 <- input$file2 
    if (is.null(fileinput2)) 
    return(NULL) 
    #xlfile <- list.files(pattern = ".xlsx") 
    xlfile <- fileinput2[1] 
    wb <- loadWorkbook(xl_file) 
    sheet_ct <- wb$getNumberOfSheets() 
    b <- rbindlist(pblapply(1:sheet_ct, function(x) { 
    res <- read.xlsx(xl_file, x) 
    }), fill=TRUE) 
    b <- b [-c(1),] 
    print (b) 
    }) 

現在,我想使基於文件輸入selectInput動態的選擇。

+0

見'?updateSelectInput'及其家人的函數來處理這些問題。再看看'?conditionalPanel'。 – nicola 2014-10-27 10:06:26

+0

我已經嘗試updateselectinput並沒有實時更新選擇列表....它就像用戶上傳文件1我需要更新選擇列表基於那只是「模型」...如果他然後上傳第二個文件然後提供所有三個選項..如果只是第二個文件上傳,然後選擇=只是「觀察」。請讓我知道如何實現這一點。 – statisticalbeginner 2014-10-27 14:27:08

回答

1

我試圖糾正server.R文件中的一些問題。請注意,我也跟着下面的算法

  1. 如果file1上傳第一,然後選擇「模式」
  2. 如果文件2隨後被上傳,然後選擇應該是「模型」,「觀察」,「兩者」
  3. 如果文件2第一次上傳的選擇是「觀察」
  4. 如果file1上傳隨後再選擇應該是「模型」,「觀察」,「兩者」

天秤座RY(閃亮)庫(XLSX)

shinyServer(function(input, output) { 

    a <- reactive({ 
    fileinput1 <- input$file1 
    if (is.null(fileinput1)) 
     return(NULL) 
    #read.table(fileinput1$datapath, header = TRUE, col.names = c("Ei","Mi","hours","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","par","Temp","Sal","co2atm","u10","dicfl","co2ppm","co2mol","pH")) 

    #Please change this part back to your code as I dont have your file based on the column names above 
    read.table(fileinput1$datapath, header= TRUE) 
    }) 

    #Upload Observation Data 

    b <- reactive({ 
    fileinput2 <- input$file2 
    if (is.null(fileinput2)) 
     return(NULL) 
    #xlfile <- list.files(pattern = ".xlsx") 
    xlfile <- fileinput2$datapath 
    wb <- loadWorkbook(xlfile) 
    sheet_ct <- wb$getNumberOfSheets() 
    b <- rbind(list(lapply(1:sheet_ct, function(x) { 
     res <- read.xlsx(xlfile, x) 
    }))) 
    b <- b [-c(1),] 
    print(b) 
    }) 

    getModel <- reactive({ 
    if(!is.null(a()) & !is.null(b())) 
    { 
     c("Model", "Observation", "Both") 
    } 
    else if(!is.null(a())) 
    { 
     "Model" 
    } 
    else if(!is.null(b())) 
    { 
     "Observation" 
    } 


    }) 
    output$check <- renderUI({ 
    selectInput("check", label = h4("Dataset Selection"), choices = as.list(getModel()), multiple = F) 
    }) 


}) 
+0

感謝您的回答。只是想知道有沒有辦法將閃亮(ui.R)中的fileInput的進度條與無功分量b計算關聯起來。因爲如您所知,在xlsx中合併表單需要一點時間。 – statisticalbeginner 2014-10-28 15:46:51

+0

我想你可以改變這種'B' - rbind(名單(lapply(1:sheet_ct,函數(X){ 資源< - read.xlsx(xlfile,X) })))' 到 ' b < - rbind(list(lapply(1:sheet_ct,function(x){ res < - read.xlsx(xlfile,x) withProgress(message ='Calculation in progress', detail ='這可能需要一段時間...',值= 0,{ 爲(I在1:sheet_ct){ incProgress(1/sheet_ct) Sys.sleep(0.25) } }) })))' – 2014-10-28 17:15:30

+0

感謝。 ?withProgress - >沒有這樣的功能。與上面的代碼,它將引發錯誤:意外的符號。 – statisticalbeginner 2014-10-28 17:28:03

相關問題