我有两个代码要集成成一个系统。该系统是审计样本选择系统,我使用RStudio开发系统。系统行为如下:
这是从Excel文件中选择审核样本的代码:
library(shiny)
library(xlsx)
library(xlsxjars)
library(rJava)
ui <- fluidPage(
titlePanel(img(src = "kpmg.png", height = 60, width = 130)),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose Excel file',
accept = c(".xlsx")
),
actionButton('submit', "Submit")
),
mainPanel(
tableOutput("contents")
)
)
)
server <- function(input, output) {
output$contents <- renderTable({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
file.rename(inFile$datapath, paste(inFile$datapath, ".xlsx", sep = ""))
wb <- read.xlsx(paste(inFile$datapath, ".xlsx", sep = ""), 1)
nrow(wb) -> rows
if (rows == 1) {
outdf <- wb[sample(rows, 1), ]
} else
if (rows >= 2 & rows <= 4) {
outdf <- wb[sample(rows, 1), ]
} else
if (rows >= 5 & rows <= 12) {
outdf <- wb[sample(rows, 2), ]
} else
if (rows >= 13 & rows <= 52) {
outdf <- wb[sample(rows, 5), ]
} else
if (rows >= 53 & rows <= 365) {
outdf <- wb[sample(rows, 15), ]
} else
if (rows > 365) {
outdf <- wb[sample(rows, 25), ]
}
outdf
})
}
shinyApp(ui = ui, server = server)
这是从PDF文件中选择审核样本的代码:
library(shiny)
library(rJava)
library(tabulizer)
ui <- fluidPage(
titlePanel(img(src = "kpmg.png", height = 60, width = 130)),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose PDF file',
accept = c(".pdf")
),
actionButton('submit', "Submit")
),
mainPanel(
tableOutput("contents")
)
)
)
server <- function(input, output) {
output$contents <- renderTable({
inFile <- input$file1
if(is.null(inFile))
return(NULL)
outtable <- extract_tables(inFile$datapath)
outtable[[1]] <- outtable[[1]][-c(1,1),] # Remove header from the table on the first page
df <- do.call(rbind, outtable) # Turn matrix into data frame
nrow(df) -> rows
if (rows == 1) {
outdf <- df[sample(rows, 1), ]
} else
if (rows >= 2 & rows <= 4) {
outdf <- df[sample(rows, 1), ]
} else
if (rows >= 5 & rows <= 12) {
outdf <- df[sample(rows, 2), ]
} else
if (rows >= 13 & rows <= 52) {
outdf <- df[sample(rows, 5), ]
} else
if (rows >= 53 & rows <= 365) {
outdf <- df[sample(rows, 15), ]
} else
if (rows > 365) {
outdf <- df[sample(rows, 25), ]
}
outdf
})
}
问题是我不知道如何组合这两个代码,因此它只有一个文件上传字段,可用于Excel和PDF文件。
答案 0 :(得分:0)
我建议将.xlsx和.pdf的导入过程包装在两个单独的函数中,因此您基本上拥有load_xlsx()
和load_pdf()
。然后使用RegEx检查文件扩展名,例如:
if (grepl("*.xlsx",inFile[1]) == TRUE){
load_xlsx(inFile)
} else if (grepl("*.pdf",inFile[1]) == TRUE){
load_pdf(inFile)
}