2015-12-14 30 views
1

我正在尋找允許用戶輸入他們有興趣上傳的文件的數量,然後讓用戶界面做出反應,以提供許多文件輸入按鈕。如何允許從R用戶的動態數量的文件輸入?

我不確定如何做到這一點。即使一旦完成,我也不確定如何最好地處理這種動態變量。

#ui.R 

    shinyUI(fluidPage(
    titlePanel("Upload your files"), 
    fluidRow(

     column(3, wellPanel(
     numericInput("user_num_2", 
        label = h4("Number of Old Files"), 
        value = 1) 
    )), 

     column(4, wellPanel(
     # This outputs the dynamic UI component 
     uiOutput("ui1") 
    )) 
    ), 
    fluidRow(

     column(3, wellPanel(
     numericInput("user_num_2", 
        label = h4("Number of New Files"), 
        value = 1) 
    )), 


     column(4, wellPanel(
     # This outputs the dynamic UI component 
     uiOutput("ui2") 
    )) 
    ), 
    fluidRow(

     column(3, wellPanel(

     selectInput("input_type", 
        label = h4("Geography Assignment"), 
        c("Zip", "County", "Zip-County", "Custom Assignment" 
        ) 
     ) 
    )) 
    ) 
)) 

因此,用戶可以輸入他們想要的文件上傳按鈕的數量。這個想法是將它們合併在一起。 multiple選項不起作用,因爲這些文件可能位於不同的位置。

screen shot

+0

你應該包括一個你想要實現的重複例子。 – MLavoie

+0

一個最小的閃亮的代碼將是有用的,但問題是足夠清晰IMO。 – Dason

+0

@MLavoie我希望你沒有找到這個論點,但是如果我有能力做到這一點,我就不需要尋求幫助。 – Reck

回答

0

好,我希望你喜歡這個。想法來自這篇文章:Shiny R renderPrint in loop usinf RenderUI only update the output

您可以輕鬆地將從每個文件輸入創建的對象存儲在一個列表中,以便用於進一步分析。

服務器

shinyServer(function(input, output, session) { 
    output$fileInputs=renderUI({ 
    html_ui = " " 
    for (i in 1:input$nfiles){ 
     html_ui <- paste0(html_ui, fileInput(paste0("file",i), label=paste0("file",i))) 
    } 
    HTML(html_ui) 
    }) 

}) 

UI

shinyUI(pageWithSidebar(
    headerPanel('Variable files'), 
    sidebarPanel(
    numericInput("nfiles", "number of files", value = 2, min = 1, step = 1), 
    uiOutput("fileInputs") 
), 
    mainPanel() 
)) 

一些解釋,有光澤通過HTML工作。如果您在控制檯中運行以下代碼行,您將看到創建對象html_ui後面的邏輯,該對象可以包含可變數量的這些HTML元素。

fileInput(paste0("file",1), label=paste0("file",1)) 

# <div class="form-group shiny-input-container"> 
# <label>file1</label> 
# <input id="file1" name="file1" type="file"/> 
#  <div id="file1_progress" class="progress progress-striped active shiny-file-input-progress"> 
#  <div class="progress-bar"></div> 
# </div> 
# </div> 
相關問題