0

My goal is to export data into PDF. To do so, i used this code. The expected result is to export the PDF output without any error in compiling. Actually, there is a problem in displaying the PDF output.

I installed TinyTex with this command install.packages('tinytex') and tinytex::install_tinytex().

If someone could help me to resolve this, i will be thankful

The R console diplay a warning message :

output file: file1e7c53f754f1.knit.md

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS file1e7c53f754f1.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output pandoc1e7c2fc133f5.tex --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --lua-filter "C:/Users/stagiaire/Documents/R/R-4.0.2/library/rmarkdown/rmd/lua/pagebreak.lua" --lua-filter "C:/Users/stagiaire/Documents/R/R-4.0.2/library/rmarkdown/rmd/lua/latex-div.lua" --variable "geometry:margin=1in" 
This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020/W32TeX) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
Warning: Error in : LaTeX failed to compile C:\Users\STAGIA~1\AppData\Local\Temp\RtmpclIBQw/report.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips.

Here is the code :

library(shiny)
library(DT)
library(base64enc)
library(rmarkdown)

js <- '
Shiny.addCustomMessageHandler("download", function(b64) {
  const a = document.createElement("a");
  document.body.append(a);
  a.download = "report.pdf";
  a.href = b64;
  a.click();
  a.remove();
})
'

ui <- basicPage(
  tags$head(
    tags$script(HTML(js))
  ),
  br(),
  DTOutput("dtable")
)

server <- function(input, output, session){
  
  dat <- iris[1:13, ] 
  
  output[["dtable"]] <- renderDT({
    datatable(
      dat,
      extensions = "Buttons",
      options = list(
        dom = "Bfrtip",
        buttons = list(
          list(
            extend = "collection",
            text = "Report",
            action = JS(
              "function ( e, dt, node, config ) {",
              "  var array = dt.data().toArray();",
              "  var tarray =  Object.keys(array[0]).map(function(c) {",
              "    return array.map(function(r) { return r[c]; });",
              "  });",
              "  var df = {};",
              "  for(var i = 1; i <= tarray.length; ++i) {",
              "    df['V' + i] = tarray[i-1];",
              "  }",
              "  Shiny.setInputValue('report', df);",
              "}")
          )
        )
      )
    )
  }, server = FALSE)
  
  observeEvent(input[["report"]], {
    df <- 
      setNames(as.data.frame(lapply(input[["report"]][-1], unlist)), names(dat))
    showNotification("Creating report...", type = "message")
    tmpReport <- tempfile(fileext = ".Rmd")
    file.copy("report.Rmd", tmpReport)
    outfile <- file.path(tempdir(), "report.pdf")
    render(
      tmpReport, 
      output_file = outfile,
      params = list(
        data = df
      )
    )
    b64 <- dataURI(
      file = outfile,
      mime = "application/pdf"
    )
    session$sendCustomMessage("download", b64)
  })
}

shinyApp(ui, server)

File report.Rmd:

---
title: "My awesome report"
author: "John Doe"
date: "21/10/2020"
output: pdf_document
params:
  data: "x"
---
  
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = FALSE)```
 ```{r}
kable(params[["data"]], row.names = FALSE)```
Aimane CAF
  • 47
  • 7

2 Answers2

0

Just a guess, but maybe this needs to be named:

```{r name}
kable(params[["data"]], row.names = FALSE)
```
hachiko
  • 671
  • 7
  • 20
0

In fact, the code works, i have just a problem in this line :

outfile <- file.path(tempdir(), "report.pdf")

Example : C:\Users\Desktop/report.pdf

Thank you for your collaboration !

Aimane CAF
  • 47
  • 7