【问题标题】:Exporting multiple excel sheets in one workbook R shiny在一个工作簿中导出多个 excel 表 R 闪亮
【发布时间】:2016-10-26 05:36:51
【问题描述】:

link to data in xlsx file (data is in 4th sheet),link to data in csv file
图书馆(闪亮) 库(xlsx)

shinyUI(fluidPage(

  titlePanel("Tim O'Leary"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose File',
             accept=c('text/csv', 
                     'text/comma-separated-values,text/plain', 
                     c(".txt",'.csv'))),
  downloadButton('downloadData', 'Download'),
  tags$hr(),
  checkboxInput('header', 'Header', TRUE),
  radioButtons('sep', 'Separator',
               c(Comma=',',
                 Semicolon=';',
                 Tab='\t'),
               '\t'),
  radioButtons('quote', 'Quote',
               c(None='',
                 'Double Quote'='"',
                 'Single Quote'="'"),
               '"')

),
mainPanel(

  #tableOutput('contents')
  tabsetPanel(
    tabPanel("RawTable", tableOutput('contents')),
    tabPanel("Table1", tableOutput('a')),
    tabPanel("Table2", tableOutput("b")),
    tabPanel("Table3", tableOutput("c"))

  )
 )
  )
 ))

 library(shiny)
  library(xlsx)

 shinyServer(function(input, output) {

 rawData <- reactive({
 filet <- input$file1
   if(is.null(filet)) return()
   data <- read.csv(filet$datapath)
   })

  #dtableInput<- reactive({
       # if(is.null(rawData())) 
  #   return()
  #  data<-rawData()        
   #})

   a <- reactive({
     a <- subset(rawData(), AssertionString == "10046")
    a
 })

  b <- reactive({
   b <- subset(rawData(), AssertionString == "10074")
   b
  })

 c <- reactive({
   c <- subset(rawData(), AssertionString == "10179")
   c
 })

 # workBook <- reactive({
#    processor <- createWorkbook()
 #    page1 <- createSheet(wb=processor, sheetName="iam")
  #   page2 <- createSheet(wb=processor, sheetName="tim")
  #  page3 <- createSheet(wb=processor, sheetName="oleary")
   # page4 <- createSheet(wb=processor, sheetName="J")
   #addDataFrame(x=rawData(), sheet=page1)
#    addDataFrame(x=a(), sheet=page2)
 #   addDataFrame(x=b(), sheet=page3)
  #  addDataFrame(x=c(), sheet=page4)
  # wb <- saveWorkbook(processor,"processorData")
 #  wb
 #})

 output$contents <- renderTable({
       rawData()
      })

     output$a <- renderTable({
          a()
       })

       output$b <- renderTable({
       b()
        })

     output$c <- renderTable({
        c()
           })

        output$downloadData <- downloadHandler(
      filename = function() {paste("file_name", '.cvs')},
       content = function(file){
         write.csv(a(), file="file_name")
          #write.xlsx2(a(), file="file_name.xlsx", sheetName="sheet1")
            #write.xlsx2(b(), file="file_name.xlsx", sheetName="sheet2",                                    append=T)
       })
#rbind allows you to connect dfs in column like manner

     })

这些是我在 r shiny 中的 ui 和服务器脚本,我正在尝试将数据框 rawData、a、b 和 c 导出到 Excel 工作簿中,每个数据框都有自己的工作表。我曾尝试读取 csv 文件并以这种方式导出,但我找不到允许我以使用 write.csv 所需的方式导出这些文件的函数。然后我尝试导入为 .xlsx 并使用 write.xlsx2,因为 rawData 太大而无法仅 write.xlsx,当我在单击下载按钮时使用 write.xlsx2 时,它只会加载无穷无尽的时间但永远不会下载任何事物。任何帮助或建议将不胜感激,谢谢

【问题讨论】:

  • AssertonString 是我的数据集中列的名称,我正在通过这些 AssertionString 数字过滤我的数据集。我相信问题出在我的 downloadHandler 中,但我似乎无法正常工作
  • 请提供样本数据
  • 有没有办法在 cmets 中附加我正在使用的数据?
  • 您可以编辑您的原始帖子(编辑按钮在帖子下方)

标签: r excel shiny


【解决方案1】:

无法下载您的数据,但这是一个有效的示例。您可以上传带有标题的任何 csv 文件,并指定一个列,然后您可以下载一个 xlsx 文件,其中 csv 文件根据所选列中的每个唯一值拆分为多个选项卡。请注意,write.xlsx 函数相当慢,因此您可能需要等待一段时间,具体取决于您的 csv 文件大小。

library(shiny)

ui <- shinyUI(fluidPage(

   titlePanel("CSV Splitter"),

   sidebarLayout(
      sidebarPanel(
        fileInput("file", "Upload csv file", accept="text/csv"),
        uiOutput("column_ui"),
        downloadButton("download")
      ),

      mainPanel(
      )
   )
))

server <- shinyServer(function(input, output) {

    data <- reactive({
        if (is.null(input$file)) {
            return(NULL)
        } else {
            return(read.csv(input$file$datapath, header=TRUE))
        }
    })

    output$column_ui <- renderUI({
        selectInput("column", "Select a column to split by unique values", unique(names(data())))
    })

    output$download <- downloadHandler(
        filename = "result.xlsx",
        content = function(file) {
            column_data = data()[[input$column]]
            unique_values = unique(column_data)
            write.xlsx(data()[data()[[input$column]] == unique_values[1],], file, as.character(unique_values[1]))
            for (i in 2:length(unique_values)) {
                write.xlsx(data()[data()[[input$column]] == unique_values[i],], file, as.character(unique_values[i]), append = TRUE)
            }
        }
    )

})

# Run the application 
shinyApp(ui = ui, server = server)

【讨论】:

  • 谢谢!我试试看
猜你喜欢
  • 1970-01-01
  • 2021-08-09
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
  • 2021-04-19
  • 1970-01-01
相关资源
最近更新 更多