att_expat
att_expat

Reputation: 1

The pdf report generated by my shiny app doesn´t show the output but the code

Im working in my first shiny app and currently I am trying to download a pdf report that includes the output table. However, when I download the report, it doesnt show the data but the code and a null error. Could you give me some advice to solve this issue?

My shiny code looks like this:

u

i <- fluidPage(
  
  theme = shinytheme("united"),
  navbarPage(
    title = tags$h4("Costs:"),
    
    tabPanel(
      title = tags$h4("Datos"),
      fluidRow(column(4, 
                      textInput("txt1", "Name", ""),
                      textInput("txt2", "NIT", ""),
                      textInput("txt3", "Phone", ""),
                      textInput("txt4", "Email", ""),
                      dateInput("date", label = "Date", value = "2023-01-01")),
               
              )
      )),
    
    tabPanel(
      title = tags$h4("Cost"),
      
     
      fluidRow(
        column(12,
               h4(tags$strong("Info")),
               tableOutput("client_info_table")
        )
      ),
      
      fluidRow(
        column(12,
               br(),
               downloadButton("report", "Downloadpdf"),
        ))
      
    )
  )


server <- function(input, output, session) {
 
  output$client_info_table <- shiny::renderTable({

    df <- data.frame(Date = format(input$date, format = "%B %d, %Y"), Name = input$txt1, NIT = input$txt2, Phone = input$txt3, Email = input$txt4)
    
    return(df)
  })
  
  
  output$report <- downloadHandler(
    filename = "Cost.pdf",
    
    {
      paste0("report_", ".pdf")
    },
    
    
    content = function(file) {
      

      params <- list(info = input$client_info_table)
      

      output$report <- downloadHandler(
        filename = "Cotizacion.pdf",
        content = function(file) {
          rmarkdown::render(
            input = "report.Rmd",
            output_file = file,
            params = params,
            envir = new.env(parent = globalenv())
          )
        }
      )
      
      
      id <- showNotification(
        "Rendering report...", 
        duration = NULL, 
        closeButton = FALSE
      )
      on.exit(removeNotification(id), add = TRUE)
      
    
      rmarkdown::render(input = "report.Rmd", output_file = file,
                        params = params,
                        envir = new.env(parent = globalenv())
      )
    }
    
    
  )
}


shinyApp(ui = ui, server = server)

My .Rmd file looks like this:

---
title: "Cost"
output: pdf_document
latex_engine: tinytex
params:
  info: ""
---
```{r}
params$info
```

And the pdf shows the following:

params$info
## NULL

Thanks in advance for your help!

When running it shows the folloring error:

"C:/Program Files/RStudio/bin/quarto/bin/tools/pandoc" +RTS -K512m -RTS report.knit.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output pandoc219094a40c8.tex --lua-filter "C:\Users\bio\AppData\Local\R\win-library\4.2\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\bio\AppData\Local\R\win-library\4.2\rmarkdown\rmarkdown\lua\latex-div.lua" --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --variable "geometry:margin=1in" 
[WARNING] Deprecated: --self-contained. use --embed-resources --standalone

Upvotes: 0

Views: 338

Answers (1)

stefan
stefan

Reputation: 124048

Instead of exporting the result of renderTable store your dataframe as a reactive which could then be used inside renderTable and passed to your Rmd report template like so:

library(shiny)
library(shinythemes)

ui <- fluidPage(
  theme = shinytheme("united"),
  navbarPage(
    title = tags$h4("Costs:"),
    tabPanel(
      title = tags$h4("Datos"),
      fluidRow(column(
        4,
        textInput("txt1", "Name", ""),
        textInput("txt2", "NIT", ""),
        textInput("txt3", "Phone", ""),
        textInput("txt4", "Email", ""),
        dateInput("date", label = "Date", value = "2023-01-01")
      ), )
    )
  ),
  tabPanel(
    title = tags$h4("Cost"),
    fluidRow(
      column(
        12,
        h4(tags$strong("Info")),
        tableOutput("client_info_table")
      )
    ),
    fluidRow(
      column(
        12,
        br(),
        downloadButton("report", "Downloadpdf"),
      )
    )
  )
)

server <- function(input, output, session) {
  df <- reactive({
    data.frame(Date = format(input$date, format = "%B %d, %Y"), 
               Name = input$txt1, 
               NIT = input$txt2, 
               Phone = input$txt3, 
               Email = input$txt4)
  })
  
  output$client_info_table <- shiny::renderTable({
    df()
  })

  output$report <- downloadHandler(
    filename = "Cost.pdf",
    {
      paste0("report_", ".pdf")
    },
    content = function(file) {
      params <- list(info = df())

      output$report <- downloadHandler(
        filename = "Cotizacion.pdf",
        content = function(file) {
          rmarkdown::render(
            input = "report.Rmd",
            output_file = file,
            params = params,
            envir = new.env(parent = globalenv())
          )
        }
      )

      id <- showNotification(
        "Rendering report...",
        duration = NULL,
        closeButton = FALSE
      )
      on.exit(removeNotification(id), add = TRUE)

      rmarkdown::render(
        input = "report.Rmd", output_file = file,
        params = params,
        envir = new.env(parent = globalenv())
      )
    }
  )
}

shinyApp(ui = ui, server = server)

enter image description here

enter image description here

Upvotes: 1

Related Questions