2

I am trying to generate a pdf report of my carbon footprint calculator. I am retrieving reactive values, making them static and trying to put them in the simple report:

First part:

generate_pdf <- function() {
    output_file <- "my_shiny_app_output.pdf"
    render("C:/Users/timgr/OneDrive/Namizje/Tim/IZO/SustainableNGOs/R/RMD_file.Rmd", output_file = output_file)
    return(output_file)
  }
  
  # Event handler for the download button
  output$download_pdf <- downloadHandler(
    filename = function() {
      "my_shiny_app_output.pdf"
    },
    content = function(file) {
      pdf_file <- generate_pdf()
      file.rename(pdf_file, file)
    }
  )

Second part:

output$report <- downloadHandler(
    filename = function() {
      paste("report-", Sys.Date(), ".pdf", sep="")
    },
    content = function(file) {
      # Extract current values from your reactive functions
      totEm_food <- total_food_emissions()
      totEm_hotel <- total_hotel_stays_emissions()
      totEm_business <- total_business_travel_emissions()
      totEm_work <- total_office_work_emissions()
      totEm_commuting <- total_commuting_emissions()
      
      # Pass these values as params to the Rmd
      rmarkdown::render("RMD_file.Rmd", 
                        output_file = file,
                        params = list(
                          total_commuting = paste(totEm_commuting, "(kg CO2e)"),
                          total_work = paste(totEm_work, "(kg CO2e)"),
                          total_business = paste(totEm_business, "(kg CO2e)"),
                          total_hotel = paste(totEm_hotel, "(kg CO2e)"),
                          total_food = paste(totEm_food, "(kg CO2e)")
                        )
      )
    }
  )

RMD:

---
title: "Emission Report"
output: pdf_document
params:
  total_commuting: "NA"
  total_work: "NA"
  total_business: "NA"
  total_hotel: "NA"
  total_food: "NA"
---
## Emission Report

- Total Commuting Emissions: `r params$total_commuting`
- Total Work Emissions: `r params$total_work`
- Total Business Travel Emissions: `r params$total_business`
- Total Hotel Stay Emissions: `r params$total_hotel`
- Total Food Emissions: `r params$total_food`

I checked if reactive values work and they do, but PDF always generates NA result. What is wrong?

Timko99
  • 21
  • 3

1 Answers1

0

Not sure, what you are trying specifically without a proper reprex, but looking at your code stubs it seems that you are missing the params in one of your download handler (why are there two? again a proper reprex would help tremendously).

Having said that, I created a small reprex, which shows how to render the pdf dynamically:

report_template.Rmd

---
title: "Emission Report"
output: pdf_document
params:
  total_commuting: "NA"
  total_work: "NA"
  total_business: "NA"
  total_hotel: "NA"
  total_food: "NA"
---
## Emission Report

- Total Commuting Emissions: `r params$total_commuting`
- Total Work Emissions: `r params$total_work`
- Total Business Travel Emissions: `r params$total_business`
- Total Hotel Stay Emissions: `r params$total_hotel`
- Total Food Emissions: `r params$total_food`

app.R

library(shiny)

ui <- fluidPage(
   fluidRow(
      numericInput("val", "Value to be placed in pdf report", 1, 10, 1),
      downloadButton("dwnld", "Download pdf")
   )
)

server <- function(input, output, session) {
   output$dwnld <- downloadHandler(
      filename = function() {
         paste0("report", Sys.Date(), ".pdf")
      },
      content = function(file) {
         rmarkdown::render("report_template.Rmd",
                           output_file = file,
                           params = list(
                              total_commuting = paste(input$val, "(kg CO2e)"),
                              total_work = paste(input$val, "(kg CO2e)"),
                              total_business = paste(input$val, "(kg CO2e)"),
                              total_hotel = paste(input$val, "(kg CO2e)"),
                              total_food = paste(input$val, "(kg CO2e)")
                           ))
      }
   )
}

shinyApp(ui, server)

When you press Download pdf you will get a pdf to be downloaded where all fields are set properly.

thothal
  • 16,690
  • 3
  • 36
  • 71