0

Hi I have a simple shiny app from which i wish to download a pdf table after importing a csv file into it. I suspect that i use the parameters incorrectly as i take :

Error : 'file' must be a character string or connection

#ui.r
library(shiny)
library(rmarkdown)

fluidPage(sidebarLayout(
  sidebarPanel(
    fileInput("file1", "Input CSV-File"),
    downloadButton(
      outputId = "downloader",
      label = "Download PDF"
    )
  ),
  mainPanel(tableOutput("table"))
))
#server.r
function(input, output) {

  thedata <- reactive({
    inFile <- req(input$file1)
    read.csv(inFile$datapath)
  })
  output$table <- renderTable({
    thedata()
  })
  output$downloader <- downloadHandler(
    filename = "Student-report.pdf",
    content = function(file){
      out = rmarkdown::render("kable.Rmd")
      file.rename(out, file)
    }
  )
}
#kable.rmd
---

output: pdf_document
params:
  table: 'NULL'
  file: 'NULL'
---

```{r echo = FALSE, message = FALSE, include = FALSE}

library(knitr)
```

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

```
```{r}
params[["table"]]
```
firmo23
  • 7,490
  • 2
  • 38
  • 114

1 Answers1

1

The issue was with using file in your Rmd file. And because you've got the dataframe in a reactive function, you don't have to pass the file separately. Please see the updated code:

#ui.r
library(shiny)
library(rmarkdown)

ui <- fluidPage(sidebarLayout(
  sidebarPanel(
    fileInput("file1", "Input CSV-File"),
    downloadButton(
      outputId = "downloader",
      label = "Download PDF"
    )
  ),
  mainPanel(tableOutput("table"))
))
#server.r
server <- function(input, output) {

  thedata <- reactive({
    inFile <- req(input$file1)
    read.csv(inFile$datapath)
  })
  output$table <- renderTable({
    thedata()
  })
  output$downloader <- downloadHandler(
    filename = "Student-report.pdf",
    content = function(file){
      out <- rmarkdown::render("kable.Rmd", pdf_document())
      file.rename(out, file)
    }
  )
}

shinyApp(ui,server)

Kable.Rmd

---
output: pdf_document
---

```{r echo = FALSE, message = FALSE, include = FALSE}

library(knitr)
```


```{r}
kable(thedata())
```
amrrs
  • 6,215
  • 2
  • 18
  • 27
  • Hi,although your answer seems to be right i take this error when trying to upload some csvs. – firmo23 Feb 14 '18 at 17:19
  • "C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS kable.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output kable.pdf --template "C:\Users\makis23\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in" ! Undefined control sequence. l.93 \rowcolors – firmo23 Feb 14 '18 at 17:20
  • pandoc.exe: Error producing PDF Warning: running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS kable.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output kable.pdf --template "C:\Users\makis23\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"' had status 43 Warning: Error in : pandoc document conversion failed with error 43 – firmo23 Feb 14 '18 at 17:21
  • You get this error because you don't have the required latex package to render a pdf. Either you have to install an application like MiTex or you have to render it as html – amrrs Feb 14 '18 at 17:37
  • Html is not an option so i have to install MikTex i suppose – firmo23 Feb 14 '18 at 17:50
  • Is it possible the reactivity with shiny to be the cause of the problem? Im asking this because the rmd file works when i run it outside of the shiny app – firmo23 Feb 14 '18 at 20:41
  • Does it generate pdf? – amrrs Feb 14 '18 at 20:51
  • only when i knit it outside of the app – firmo23 Feb 14 '18 at 21:05
  • That in combination with your answer solves the problem. https://stackoverflow.com/questions/46080853/why-does-rendering-a-pdf-from-rmarkdown-require-closing-rstudio-between-renders – firmo23 Feb 15 '18 at 23:27