2017-12-10 4 views
0

システムは、RStudioを使用してシステムを開発する監査サンプル選択システムです。システムは次のように動作します。表示された出力をダウンロードできるようにRShinyのダウンロードボタンを作る方法は?

  1. ユーザーアップロードExcelファイルまたはPDFファイル。
  2. ユーザーは、2つのラジオボタンの中から1つを選択する必要があります.1つは「低リスク」で、もう1つは「高リスク」です。
  3. ユーザーが[送信]ボタンをクリックします。
  4. ファイル内のテーブルの行数に応じて、一定数の監査サンプルが自動的に選択されます。
  5. 選択した監査サンプルの数は、「低リスク」と「高リスク」で異なります。
  6. 選択した監査サンプルが表示されます。
  7. ユーザーは、表示された選択した監査サンプルをダウンロードできます。

    library(shiny) 
    library(xlsx) 
    library(xlsxjars) 
    library(rJava) 
    library(pdftools) 
    library(tabulizer) 
    
    ui <- fluidPage(
    titlePanel("Audit Sample Selection System"), 
    sidebarLayout(
    sidebarPanel(
        fileInput("file1", "Choose file", accept = c(".xlsx", ".pdf")), 
        radioButtons("select", "Level of Risk", choices=list("Low Risk" = "low","High Risk" = "high")), 
        actionButton("submit", "Submit") 
    ), 
    mainPanel(
    tableOutput("contents"), 
    downloadButton("download", "Download") 
    ) 
    ) 
    ) 
    
    server <- function(input, output){ 
    
    mydf <- eventReactive(input$submit, { 
    
    # check for required values (for truthfulness)/ensure the values are available 
    req(input$select) 
    req(input$file1) 
    
    inFile <- input$file1 
    
    if (grepl("*.xlsx",inFile[1]) == TRUE){ 
    file.rename(inFile$datapath, paste(inFile$datapath, ".xlsx", sep = ""))   
    wb <- read.xlsx(paste(inFile$datapath, ".xlsx", sep = ""), 1) 
    
    nrow(wb) -> rows 
    if (input$select == "low") { 
    # sample for low risk (xlsx) 
    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), ] 
          } 
    } else { 
    # sample for high risk (xlsx) 
    if (rows == 1) { 
        outdf <- wb[sample(rows, 1), ] 
    } else 
        if (rows >= 2 & rows <= 4) { 
        outdf <- wb[sample(rows, 2), ] 
        } else 
        if (rows >= 5 & rows <= 12) { 
         outdf <- wb[sample(rows, 3), ] 
        } else 
         if (rows >= 13 & rows <= 52) { 
         outdf <- wb[sample(rows, 8), ] 
         } else 
         if (rows >= 53 & rows <= 365) { 
          outdf <- wb[sample(rows, 25), ] 
         } else 
          if (rows > 365) { 
          outdf <- wb[sample(rows, 40), ] 
          } 
    } 
    } else if (grepl("*.pdf",inFile[1]) == TRUE) { 
        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 (input$select == "low") { 
        # sample for low risk (pdf) 
    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), ] 
          } 
        } else { 
        # sample for high risk (pdf) 
    if (rows == 1) { 
        outdf <- df[sample(rows, 1), ] 
    } else 
        if (rows >= 2 & rows <= 4) { 
        outdf <- df[sample(rows, 2), ] 
        } else 
        if (rows >= 5 & rows <= 12) { 
         outdf <- df[sample(rows, 3), ] 
        } else 
         if (rows >= 13 & rows <= 52) { 
         outdf <- df[sample(rows, 8), ] 
         } else 
         if (rows >= 53 & rows <= 365) { 
          outdf <- df[sample(rows, 25), ] 
         } else 
          if (rows > 365) { 
          outdf <- df[sample(rows, 40), ] 
          } 
        } 
        } else { 
        NULL 
        } 
        }) 
    
        output$contents <- renderTable({ 
        mydf() 
        }) 
        } 
    
        shinyApp(ui = ui, server = server) 
    

問題は、私は「ダウンロード」ボタンをユーザーがクリックは、表示され、選択した監査サンプルがダウンロードされる際にその仕事にダウンロードボタンを作成する方法がわからないです。

答えて

0

ダウンロードボタンを使用する代わりに、DTパッケージを使用して、素敵なテーブルとそのテーブルをダウンロードできる「ボタン」拡張子をレンダリングすることができます。

library(DT) 
# in server: 
output$contents <- DT::renderDataTable({ 
    datatable(mydf(), 
      extensions = 'Buttons', 
      options = list(dom = 'Bfrtip', buttons = 'excel')) 
}) 
# in ui: 
DT::dataTableOutput("contents") 

は、 "ボタン" 拡張子の詳細はhttps://rstudio.github.io/DT/003-tabletools-buttons.htmlを参照してください。

関連する問題