首页 > 解决方案 > “闪亮的应用程序”上传文件 -> 做某事 -> 输出文件

问题描述

我正在尝试编写我的第一个 Shiny 应用程序,它可以读取 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)

}

标签: rweb-scrapingshiny

解决方案


你能试试:

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
)    
})

推荐阅读