在一个工作簿中导出多个Excel床单



链接到XLSX文件中的数据(数据在第4页),链接到CSV文件中的数据
图书馆(闪亮) 库(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
     })

这些是我的UI和服务器脚本,在R Shiny中,我正在尝试将DataFrames Rawdata,A,B和C导出到Excel工作簿中,每个数据帧都有其自己的工作表。我尝试在CSV文件中读取并以这种方式导出它,但是我找不到一个允许我以使用Write.csv的方式导出这些功能。然后,我尝试将导入为.xlsx并使用write.xlsx2,因为rawdata太大了,无法write.xlsx,当我使用write.xlsx2时,在单击我的下载按钮时,它只会加载无限的时间,但永远不要下载任何事物。任何帮助或建议将不胜感激

无法下载您的数据,但这是有效的示例。您可以使用标头上传任何CSV文件,并指定列,然后可以下载一个xlsx文件,其中CSV文件根据所选列中的每个唯一值将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)

最新更新