“发光的应用程序”上传文件->做某事->输出文件

时间:2019-02-28 14:43:51

标签: r web-scraping shiny

我正在尝试编写我的第一个Shiny App,该应用程序读取PDF文件,提取表并将其保存到Excel文档中。

我无法生成合适的代码。到目前为止,我有: 1)对于UI

    shinyUI(fluidPage(   
        titlePanel("CMM Report"),      
        sidebarPanel(
               fileInput("file", "Upload Report")
     ),

     downloadButton("dl", "Download")

))

2)对于服务器

library(shiny)
library (tabulizer)
library(writexl) 

shinyServer(function(input, output) {       
    data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()}
    file1 <- ExtractTable (file1)        
})
## Download
output$dl <- downloadHandler(
  filename = function() { "ae.xlsx"}, 
  content = function(file) {write_xlsx(data, path = file)}  
)    
})

我不确定是否需要将提取表的代码放在函数中以及在何处调用函数才能使其正常工作。任何帮助都非常感谢。
示例的数据文件从这里 报告<-“ http://www.stat.ufl.edu/~athienit/Tables/Ztable.pdf

提取数据的功能

ExtractTable <- function (report){
  lst <- extract_tables(report, encoding="UTF-8") 

  # Delete blank columns
  lst[[1]] <- lst[[1]][, -3]
  lst[[2]] <- lst[[2]][, -4]

  # Bind the list elements 
  table <- do.call(rbind, lst)
  table <- as.data.frame(table[c(2:37, 40:nrow(table)), ],
                         stringsAsFactors=FALSE) # ...w/o obsolete rows

  # Take over colnames, cache rownames to vector
  colnames(table) <- table[1, ]
  rn <- table[2:71, 1]
  table <- table[-1,-1] # and bounce them out of the table

  #  Coerce to numeric 
  table <- as.data.frame(apply(table[1:70,1:10], 2, 
                               function(x) as.numeric(as.character(x))))
  rownames(table) <- rn 

  return(table)

}

1 个答案:

答案 0 :(得分:1)

您可以尝试:

shinyServer(function(input, output) {       
    data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()}
    ExtractTable(file1$datapath) # $datapath was missing       
})
## Download
output$dl <- downloadHandler(
  filename = function() { "ae.xlsx"}, 
  content = function(file) {write_xlsx(data(), path = file)} # parentheses () were missing
)    
})