r - 尝试在 Shiny App 中使用 RMarkdown 下载 PDF
问题描述
我正在尝试从 Shiny App 中生成 PDF 格式的报告。我有两个问题:
- 报告没有以 PDF 格式下载,仅以 HTML 格式下载。我如何下载它的PDF?
- 报告未显示渲染的内容(在本例中为 ggplotly 图像),仅显示文本。如何使该 PDF 包含 Shiny App 中显示的图像?
我究竟做错了什么?有任何想法吗?
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)
解决方案
推荐阅读
- selenium - 如何在 qaf 中使用 edgeDriver
- c++ - C++强制实现方法?
- javascript - 单击时更改背景颜色(使用 javascript 中的地图功能)
- excel - 设置用户表单边框颜色属性
- html - 在 HTML 页面中显示经过身份验证的用户
- css - :active 保持动画即使我们释放鼠标按钮
- jupyter-notebook - 尝试以 PDF 格式下载 Jupyter Notebook 时无法运行“xelatex .\notebook.tex -quiet”命令
- amazon-web-services - Aws Elastic Beanstalk 不执行 cron.yaml 文件
- python - 使用 Keras (2.3.1) 时卡在这里
- git - 如何使用 Azure DevOps REST Api 获取提交给定的标签 ID?