Shinyapps.io部署Shiny App无法生成可下载Quarto HTML文档求助
问题描述
本地运行正常的Shiny App部署到shinyapps.io后无法工作,核心是quarto::quarto_render()调用的文件写入问题(实例间无需持久化,可接受临时写入)。想知道:
- shinyapps.io是否支持Quarto渲染?
- 若不支持,是否支持RMarkdown?转换为RMarkdown后能否正常运行?
可复现代码
app.R
library(shiny) library(quarto) ui <- fluidPage( titlePanel("Reproducable Example"), sidebarLayout( sidebarPanel( textInput(inputId = "user.name", label = "User name:"), br(), downloadButton(outputId = "report", label = "Generate Report:") ), mainPanel( ) ) ) server <- function(input, output) { output$report <- downloadHandler( filename = "Reprod_ex.html", content = function(file) { quarto::quarto_render("myquarto.qmd", execute_params = list(username = input$user.name)) file.copy("qmd_output.html", file) } ) } # Run the application shinyApp(ui = ui, server = server)
myquarto.qmd
--- format: html: toc: false anchor-sections: false fig-cap-location: bottom tbl-cap-location: top number-sections: false smooth-scroll: true self-contained: true css: my-style.css output-file: "qmd_output.html" params: username: NA --- # Heading My name is `r params$username`.
解答
- shinyapps.io对Quarto的支持情况
shinyapps.io支持Quarto渲染,你遇到的问题并非不支持,而是工作目录只读导致的文件写入失败。shinyapps.io的默认工作目录是只读的,只有临时目录(tempdir())允许写入操作。
修改server端代码,将Quarto输出指定到临时目录即可解决:
server <- function(input, output) { output$report <- downloadHandler( filename = "Reprod_ex.html", content = function(file) { # 使用临时目录存放输出文件 temp_dir <- tempdir() quarto::quarto_render( input = "myquarto.qmd", execute_params = list(username = input$user.name), output_dir = temp_dir ) # 从临时目录复制到下载目标文件 file.copy(file.path(temp_dir, "qmd_output.html"), file) } ) }
同时要确保my-style.css被包含在部署文件中,避免Quarto渲染时找不到样式文件。
- RMarkdown的支持情况
shinyapps.io完全支持RMarkdown渲染,转换格式后调整代码即可正常运行:
- 将
myquarto.qmd改为myreport.Rmd,内容调整为RMarkdown格式:
--- title: "Report" output: html_document: toc: false anchor_sections: false fig.cap: bottom tbl.cap: top number_sections: false smooth_scroll: true self_contained: true css: my-style.css params: username: NA --- # Heading My name is `r params$username`.
- 修改server端代码使用
rmarkdown::render():
server <- function(input, output) { output$report <- downloadHandler( filename = "Reprod_ex.html", content = function(file) { temp_dir <- tempdir() rmarkdown::render( input = "myreport.Rmd", params = list(username = input$user.name), output_dir = temp_dir ) file.copy(file.path(temp_dir, "myreport.html"), file) } ) }
总结:你的问题核心是未使用可写的临时目录,而非Quarto不被支持。调整路径后Quarto版本可正常运行;RMarkdown版本同样支持,转换后需遵循相同的临时目录规则。
内容的提问来源于stack exchange,提问作者RegalPlatypus
相关产品推荐
相关产品推荐

