r - “闪亮的应用程序”上传文件 -> 做某事 -> 输出文件
问题描述
我正在尝试编写我的第一个 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)
}
解决方案
你能试试:
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
)
})
推荐阅读
- php - move_uploaded_file() 函数不会将上传的文件保存在服务器上
- validation - 如何使用特定字段组合将表限制为 5 条记录
- r - 更改 R Markdown 中的参考书目样式
- python - 如何选择性地只允许使用烧瓶同时访问几个方法?
- regex - Google Cloud Firestore 安全规则中正确的正则表达式语法是什么?
- javascript - AJAX 忽略 .Net 会话变量
- android - Android执行不同的http请求卡住
- java - java运行时异常的主要目标和原因以及规避java运行时异常的最佳实践
- python - 循环遍历 pandas 分组数据索引
- jquery - 呈现数据表列以忽略字母并按数字排序