2016-02-12 32 views
0

我的目標是上傳兩個或更多文件,其中每個文件都有不同的分隔符。下面的ui.R和server.R代碼提供了一個在兩個文件都被讀入並具有相同分隔符時運行良好的示例。但是,如果我在file1中讀取,說作爲.csv,然後用另一個分隔符在file2中讀取,那麼一旦更改分隔符,file1就會受到影響並丟失其結構。使用不同分隔符上傳多個文件

目標是讀取file1及其預定的分隔符,然後在file2中讀取,使其對file1沒有影響。

server.R文件

options(shiny.maxRequestSize = 9*1024^2) 
    shinyServer(function(input, output) { 

    output$contents1 <- renderTable({ 
    inFile <- input$file1 
    if (is.null(inFile)) 
     return(NULL) 
    read.csv(inFile$datapath, header = input$header, 
      sep = input$sep, quote = input$quote) 
    }) 

    output$contents2 <- renderTable({ 
    inFile <- input$file2 
    if (is.null(inFile)) 
     return(NULL) 
    read.csv(inFile$datapath, header = input$header, 
      sep = input$sep, quote = input$quote) 
    }) 
    }) 

ui.R

shinyUI(fluidPage(
titlePanel("Uploading Files"), 
sidebarLayout(
     sidebarPanel(
     fileInput('file1', 'Choose file 1 to upload', 
     accept = c('text/csv','text/comma-separated-values', 
     'text/tab-separated-values', 'text/plain', 
     '.csv', '.tsv') 
     ), 
     fileInput('file2', 'Choose file 2 to upload', 
     accept = c('text/csv','text/comma-separated-values', 
     'text/tab-separated-values','text/plain', 
     '.csv','.tsv') 
     ),  
     tags$hr(), 
     checkboxInput('header', 'Header', TRUE), 
     radioButtons('sep', 'Separator', 
     c(Comma=',',Semicolon=';', 
     Tab='\t',Pipe='|'), 
     ','), 
     radioButtons('quote', 'Quote', 
     c(None='','Double Quote'='"', 
     'Single Quote'="'"), 
     '"'), 
     tags$hr() 
     ), 
     mainPanel(
     tableOutput('contents1'), 
     tableOutput('contents2') 
     ) 
     ) 
     )) 
+0

看起來你可能需要單獨的單選按鈕作爲分隔符。 – GK89

回答

0

我知道這是不是一個直接的解決方案,在一次與兩個工作,但你可以創建第二個對單選按鈕與您輸入的第二個文件關聯?

這就是我的意思。

Shiny

什麼是好的,如果你能以某種方式把相鄰引述鄰近分離器的第二分離器,和第二個報價。我不擅長如何做一個應用程序的佈局,但下面的鏈接可能會幫助解決這個問題。

shiny 4 small textInput boxes side-by-side

嘗試看看,如果你能玩具圍繞這個用單選按鈕替換爲textInput語句。

我有閃亮的問題接受一個輸入到ui中的多個輸入或對象之前,這是一種繞過它的方法。我不確定爲什麼閃亮有這個問題。這是一個比我更合格的人的問題。

+0

謝謝。我能夠創建第二套單選按鈕,僅適用於單獨的數據文件。它確實有效,儘管它在美學上擁擠不堪。功能是我的主要目標,所以我可以從這裏工作。但是,我很好奇,如果有一個更優雅的解決方案w.r.t.視覺上令人愉悅 –

相關問題