0

Hi i have a simple shiny app which consists of ui.r,server.r and kable.rmd files. I would like to import a csv in shiny app and then to be able to generate and download a table of this with kableExtra in pdf form with rmarkdown. I prefer kableExtra intead of DT because i think that is giving more options to format the final pdf table. Is this possible?

#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")

```
firmo23
  • 7,490
  • 2
  • 38
  • 114
  • So are you getting any errors/issues with the code you postet? Seems like you are already using `kableExtra` and `PDF = pdf_document()`. Maybe pass `input$file1$datapath` [as a parameter](http://rmarkdown.rstudio.com/developer_parameterized_reports.html). – Gregor de Cillia Feb 03 '18 at 12:11
  • it does not download anything. I will try with parameters – firmo23 Feb 03 '18 at 14:08

1 Answers1

0

Here is a minimal working version of what you try to accomplish. I used html as an output format instad of pdf however. You can create a csv file for testing with

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

Make sure that the three files are in the same directory!

File: 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)

File: 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")
```

Also notice that you can test the Rmd file seperately from the app by placing mtcars.csv in the same directory and using the "knit" button in RStudio.

Gregor de Cillia
  • 7,397
  • 1
  • 26
  • 43