2017-10-28 5 views
0

私は2つのコードを統合してシステムにしています。システムは、RStudioを使用してシステムを開発するAudit Sample Selection Systemです。システムは次のように動作します。異なるファイルタイプの2つのファイルアップロードフィールドを1つに結合するにはどうすればよいですか?

  1. ユーザーアップロードExcelファイルまたはPDFファイル。
  2. ユーザーが[送信]ボタンをクリックします。
  3. ファイル内のテーブルの行数に応じて、一定数の監査サンプルが自動的に選択されます。
  4. 選択した監査サンプルが表示されます。

これは、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 
}) 
} 

問題は、私ドンでありますこの2つのコードをどのように組み合わせて、ExcelファイルとPDFファイルの両方に使用できるファイルアップロードフィールドが1つしかないようにする方法を知っています。

答えて

0

.xlsxと.pdfのインポートプロセスを2つの別々の関数でラップすることをお勧めします。つまり、基本的にはload_xlsx()load_pdf()です。私はコードを試してみましたが、このエラーは「内の場合(grepl( 『* XLSX。』、INFILEを)== TRUE){警告

if (grepl("*.xlsx",inFile[1]) == TRUE){ 
    load_xlsx(inFile) 
} else if (grepl("*.pdf",inFile[1]) == TRUE){ 
    load_pdf(inFile) 
} 
+0

: 条件を次にだけのようなもので正規表現を使用してファイルの拡張子をチェック長さ> 1で、最初の要素のみが使用されます ' – Shasha

+0

'inFile'は文字列ではありません。 – Mako212

+0

私の更新を見て、忘れてしまったのは、入力がリストに返されていることです。 – Mako212

関連する問題