0

嗨,我有一个简单的闪亮应用程序,它由 ui.r、server.r 和 kable.rmd 文件组成。我想在闪亮的应用程序中导入一个 csv,然后能够生成并下载一个带有kableExtra.pdf 格式的表格rmarkdown。我更喜欢kableExtraintead,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")

```
4

1 回答 1

0

这是您尝试完成的工作的最小工作版本。但是,我用作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")
```

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

于 2018-02-03T16:59:33.300 回答