I am trying to generate a report in PDF out of a Shiny App. I have two issues:
- Report not downloading in PDF, just HTML. How do I download it on PDF?
- Report not showing what was rendered (ggplotly image in this case), just showing text. How do I make that PDF to contain the images shown in the Shiny App?
What am I doing wrong? Any ideas?
library(ggplot2)
library(plotly)
library(gapminder)
library(shiny)
library(shinydashboard)
library(rmarkdown)
bodyplotly <- dashboardBody(
fluidRow(
column(12,
box(width=10,
title = "ggplotly",
plotlyOutput("selected_plot"),
box(
width = 2, height=250,
sliderInput("year", label=("Choose year"),
min = 1950, max = 2000, value = 1977),
downloadButton("report", "Generate report")
)))))
ui <- navbarPage("Test",tabPanel("plotly",
bodyplotly))
server <- function(input, output,session) {
output$selected_plot <- renderPlotly({
p <- gapminder %>%
filter(year==input$year) %>%
ggplot( aes(gdpPercap, lifeExp, size = pop, color=continent)) +
geom_point() +
theme_bw()
ggplotly(p)
})
output$downloadreport <- downloadHandler(
# For PDF output, change this to "report.pdf"
filename = "report.html",
content = function(file) {
# Copy the report file to a temporary directory before processing it, in
# case we don't have write permissions to the current working dir (which
# can happen when deployed).
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
library(rmarkdown)
# Set up parameters to pass to Rmd document
params <- list(input$year)
# Knit the document, passing in the `params` list, and eval it in a
# child of the global environment (this isolates the code in the document
# from the code in this app).
rmarkdown::render(tempReport, pdf_document(),
#output_options=self_contained,
#params = params,
envir = new.env(parent = globalenv())
)
}
)
}
shinyApp(ui = ui, server = server)