【问题标题】:Upload Multiple Files with Different Delimiters in Shiny在 Shiny 中上传具有不同分隔符的多个文件
【发布时间】:2016-02-12 02:00:52
【问题描述】:

我的目标是上传两个或多个文件,每个文件都有不同的分隔符。下面的 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')
        )
        )
        ))

【问题讨论】:

  • 看起来您可能需要单独的单选按钮作为分隔符。

标签: r csv shiny


【解决方案1】:

我意识到这不是同时使用两者的直接解决方案,但是您可以创建第二对单选按钮来与您输入的第二个文件相关联吗?

这就是我的意思。

如果您能以某种方式将第二个分隔符与分隔符相邻,并将第二个引号与引号相邻,那该多好。我不擅长如何进行应用布局,但下面的链接可能有助于解决这个问题。

shiny 4 small textInput boxes side-by-side

试着看看你是否可以通过用单选按钮替换 textInput 语句来玩弄这个。

我之前在 ui 中接受一个输入到多个输入或对象时遇到过问题,这是一种绕过它的方法。我不确定为什么闪亮有这个问题。这是一个比我更有资格的人的问题。

【讨论】:

  • 谢谢。我能够创建第二组单选按钮以仅应用于单独的数据文件。它确实有效,尽管在美学上它挤满了页面。功能是我的主要目标,所以我可以从这里开始工作。但是,我很好奇是否有一个更优雅的解决方案。视觉上令人愉悦
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-13
  • 2012-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多