【问题标题】:Uploading data to Shiny for analyses将数据上传到 Shiny 进行分析
【发布时间】:2017-08-01 23:56:29
【问题描述】:

我正在尝试将文件上传到 Shiny 以对其进行处理,然后查看它。

我已经能够使用 CSV 文档创建表格,但输出 myData 不能用作箱线图或任何其他图表的输入。

服务器

    shinyServer(function(input, output, session){
  myData <- reactive({
    inFile <- input$file1
    if (is.null(inFile)) return(NULL)
    data <- read.csv(inFile$datapath, header = TRUE)
    data
  })

  output$contents <- renderTable({
    myData()

  })


}

用户界面

library(shiny)

write.csv(data.frame(a = 1:10, b = letters[1:10]), 'test.csv')

shinyUI(fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv',
                         'text/comma-separated-values,text/plain',
                         '.csv'))
    ),
    mainPanel(
      tableOutput('contents'),
      plotOutput('distPlot')
    )
  )
)
)

如何使用我从函数 myData 上传的文件中的数据?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您应该使用 DT 包中的函数 renderDataTable 和 dataTableOutput 来呈现您的表格。您的代码可以像这样正常工作:

    library(shiny)
    library(DT)
    
    server <- function(input, output, session){
      myData <- reactive({
        inFile <- input$file1
        if (is.null(inFile)) return(NULL)
        data <- read.csv(inFile$datapath, header = TRUE)
        data
      })
    
      output$contents <- DT::renderDataTable({
        DT::datatable(myData())       
      })
    }
    
    write.csv(data.frame(a = 1:10, b = letters[1:10]), 'test.csv')
    
    ui<- shinyUI(fluidPage(
      titlePanel("Uploading Files"),
      sidebarLayout(
        sidebarPanel(
          fileInput('file1', 'Choose CSV File',
                    accept=c('text/csv',
                             'text/comma-separated-values,text/plain',
                             '.csv'))
        ),
        mainPanel(
          DT::dataTableOutput('contents'),
          plotOutput('distPlot')
        )
      )
    )
    )
    
    shinyApp(ui,server)
    

    另见以下文章:

    也可以让用户将 csv 上传到您的 Shiny 应用程序。下面的代码显示了一个关于如何实现这一点的小示例。它还包括一个单选按钮输入,因此用户可以交互地选择要使用的分隔符。

    library(shiny)
    library(DT)
    
    # Define UI
    ui <- shinyUI(fluidPage(
    
      fileInput('target_upload', 'Choose file to upload',
                accept = c(
                  'text/csv',
                  'text/comma-separated-values',
                  '.csv'
                )),
      radioButtons("separator","Separator: ",choices = c(";",",",":"), selected=";",inline=TRUE),
      DT::dataTableOutput("sample_table")
    )
    )
    
    # Define server logic
    server <- shinyServer(function(input, output) {
    
      df_products_upload <- reactive({
        inFile <- input$target_upload
        if (is.null(inFile))
          return(NULL)
        df <- read.csv(inFile$datapath, header = TRUE,sep = input$separator)
        return(df)
      })
    
      output$sample_table<- DT::renderDataTable({
        df <- df_products_upload()
        DT::datatable(df)
      })
    
    }
    )
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2015-04-17
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 2018-02-11
      相关资源
      最近更新 更多