【问题标题】:Import csv in a shiny app and the generate and download pdf document with a table using kableExtra在闪亮的应用程序中导入 csv,并使用 kableExtra 生成和下载带有表格的 pdf 文档
【发布时间】:2018-07-13 14:40:49
【问题描述】:

您好,我有一个简单的闪亮应用程序,它由 ui.r、server.r 和 kable.rmd 文件组成。我想在闪亮的应用程序中导入一个 csv,然后能够使用kableExtra 生成并下载一个带有rmarkdown 的pdf 格式的表格。我更喜欢kableExtra 而不是DT,因为我认为这为格式化最终的 pdf 表格提供了更多选项。这可能吗?

#ui.r
library(shiny)

fluidPage(

  # App title ----
  titlePanel(div("CLINICAL TABLE",style = "color:blue")),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Select a file ----
      fileInput("file1", "Input CSV-File",
                multiple = TRUE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),

      # Horizontal line ----
      tags$hr(),

      # Input: Checkbox if file has header ----
      checkboxInput("header", "Header", TRUE),

      # Input: Select separator ----
      radioButtons("sep", "Separator",
                   choices = c(Comma = ",",
                               Semicolon = ";",
                               Tab = "\t"),
                   selected = ","),


      # Horizontal line ----
      tags$hr(),

      # Input: Select number of rows to display ----
      radioButtons("disp", "Display",
                   choices = c(Head = "head",
                               All = "all"),
                   selected = "head"),
      radioButtons('format', 'Document format', c('PDF', 'CSV'),
                   inline = TRUE),
      downloadButton('downloadReport')



    ),
    # Main panel for displaying outputs ----
    mainPanel(

      )
  ))
#server.r
function(input, output) {

  output$downloadReport <- downloadHandler(
    filename = function() {
      paste('my-report', sep = '.', switch(
        input$format, PDF = 'pdf', CSV = 'csv'
      ))
    },

    content = function(file) {
      src <- normalizePath('kable.Rmd')

      # temporarily switch to the temp dir, in case you do not have write
      # permission to the current working directory
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'kable.Rmd', overwrite = TRUE)

      library(rmarkdown)
      out <- render('kable.Rmd', switch(
        input$format,
        PDF = pdf_document(), CSV = csv_document()
      ))
      file.rename(out, file)
    }
  )

}
#kable.rmd
---
title: "Clinical Table"
author: Ek
date: January 29, 2018
output: 
  pdf_document: 
    keep_tex: yes
    latex_engine: lualatex
mainfont: Calibri Light
sansfont: Calibri Light
fontsize: 10
urlcolor: blue
---


```{r echo=FALSE,message=FALSE,include=FALSE}
library(knitr)
## Warning: package 'knitr' was built under R version 3.4.3
library(kableExtra)
library(rmarkdown)
library(shiny)
library(readr)


```


```{r nice-tab, tidy=FALSE,echo=FALSE,message=FALSE}

knitr::kable(
  req(input$file1)

    csvdata <- read.csv(input$file1$datapath,
                        header = input$header
    )
    , caption = 'REPORT TABLE',
  booktabs = TRUE,longtable = T,format = "latex",escape = F
)%>%
kable_styling(latex_options = "striped",full_width = T,font_size = 10 )%>%
row_spec(row = 0,bold = T,background = "gray")

```

【问题讨论】:

  • 那么您发布的代码是否有任何错误/问题?好像您已经在使用kableExtraPDF = pdf_document()。也许通过input$file1$datapathas a parameter
  • 它不下载任何东西。我会尝试使用参数

标签: pdf shiny r-markdown kableextra


【解决方案1】:

这是您尝试完成的工作的最小工作版本。但是,我使用html 作为pdf 的输出格式。您可以创建一个csv 文件用于测试

write.csv(mtcars, "mtcars.csv")

确保三个文件在同一个目录下!

文件:app.R

library(shiny)
library(rmarkdown)

ui <- fluidPage(sidebarLayout(
  sidebarPanel(
    fileInput("file1", "Input CSV-File"),
    downloadButton('downloadReport')
  ),
  mainPanel(tableOutput("table"))
))

server <- function(input, output) {
  output$downloadReport <- downloadHandler(
    filename = "my-report.html",
    content = function(file) {
      src <- normalizePath('kable.Rmd')

      # temporarily switch to the temp dir, in case you do not have write
      # permission to the current working directory
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'kable.Rmd', overwrite = TRUE)

      out <- render('kable.Rmd', params = list(file = input$file1$datapath))
      file.rename(out, file)
    }
  )

  output$table <- renderTable({
    inFile <- req(input$file1)
    read.csv(inFile$datapath)
  })
}

shinyApp(ui, server)

文件:kable.Rmd

---
params:
  file: "mtcars.csv"
---

```{r echo = FALSE, message = FALSE, include = FALSE}
library(kableExtra)
library(knitr)
```

```{r nice-tab, tidy = FALSE, echo = FALSE, message = FALSE}
csvdata <- read.csv(file = params$file)

kable(csvdata, "html", caption = 'REPORT TABLE',
      booktabs = TRUE, longtable = TRUE, escape = FALSE) %>%
  kable_styling(full_width = T, font_size = 10 ) %>%
  row_spec(row = 0, bold = T, background = "gray")
```

另请注意,您可以通过将mtcars.csv 放在同一目录中并使用 RStudio 中的“编织”按钮,从应用程序中单独测试 Rmd 文件。

【讨论】:

  • 问题是我想要 pdf 作为输出而不是 html。我无法让它与 pdf 功能一起使用。
  • 还有为什么 file: "mtcars.csv" ? csv可能每次都不一样
猜你喜欢
  • 2018-07-24
  • 2020-01-08
  • 2016-08-29
  • 2022-01-04
  • 2019-01-08
  • 1970-01-01
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多