为何我的R Markdown交互式Shiny HTML文档无法保存?
植被异常面积交互式分析文档生成问题
需求说明
- 基于包含区域名称(Nom)、异常类别(class)、异常面积(surface)的遥感数据表,用R Markdown生成带以下功能的HTML文档:
- 带搜索选择栏
- 可选择指定区域
- 选择区域后自动生成对应区域的异常类别面积柱状图
测试代码
--- title: "交互式植被异常分析报告" output: html_document runtime: shiny --- chooseCRANmirror(ind = 1) # 选择CRAN镜像 library(shiny) library(ggplot2) library(dplyr) anomalies_by_dept <- data.frame( Nom = c("AIN", "AIN", "AIN", "AIN", "HAUTE-SAVOIE", "HAUTE-SAVOIE", "HAUTE-SAVOIE", "HAUTE-SAVOIE", "SAVOIE", "SAVOIE", "SAVOIE", "SAVOIE"), class = c("1-轻度异常", "2-中度异常", "3-重度异常", "裸地", "1-轻度异常", "2-中度异常", "3-重度异常", "裸地", "1-轻度异常", "2-中度异常", "3-重度异常", "裸地"), surface = c(1935.2, 1470.8, 1104.9, 1245.4, 2153.9, 676.6, 1169.5, 948.9, 2.4, 1.3, 1.4, 0.7)) # 用户界面(UI) ui <- fluidPage( titlePanel("按区域分析植被异常"), sidebarLayout( sidebarPanel( shiny::selectInput("selected_dept", "选择区域", choices = unique(anomalies_by_dept$Nom)) ), mainPanel( plotOutput("anomalies_plot") ) ) ) # 服务器逻辑 server <- function(input, output) { output$anomalies_plot <- renderPlot({ selected_data <- anomalies_by_dept %>% dplyr::filter(Nom == input$selected_dept) ggplot(selected_data, aes(x = class, y = surface, fill = class)) + geom_bar(stat = "identity") + labs(title = paste(input$selected_dept, "区域各异常类别面积统计"), x = "异常类别", y = "面积(公顷)") + theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) }) } # 启动Shiny应用 shinyApp(ui = ui, server = server)
问题现象
代码在RStudio Viewer中可正常运行,但无法保存HTML文档,RStudio持续处于渲染状态,相关日志如下:
|............ | 22% [setup] processing file: bilan_Fordead.Rmd output file: C:/Users/camille.bernard/AppData/Local/Temp/RtmpcBbPtU/file17f836ff733a/bilan_Fordead.knit.md "C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/pandoc" +RTS -K512m -RTS "C:\Users\camille.bernard\AppData\Local\Temp\RtmpcBbPtU\file17f836ff733a\bilan_Fordead.knit.md" --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output pandoc17f836263746.html --lua-filter "C:\Users\camille.bernard\Documents\R\win-library\4.1\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\camille.bernard\Documents\R\win-library\4.1\rmarkdown\rmarkdown\lua\latex-div.lua" --variable bs3=TRUE --standalone --section-divs --template "C:\Users\camille.bernard\Documents\R\win-library\4.1\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --id-prefix section- --variable theme=bootstrap --mathjax --variable "mathjax-url=https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" --include-before-body "O:\Donnees_cartographiques\Teledetection\Surface_FORDEAD\header2.html" --include-after-body "O:\Donnees_cartographiques\Teledetection\Surface_FORDEAD\footer.html" Output created: C:/Users/camille.bernard/AppData/Local/Temp/RtmpcBbPtU/file17f836ff733a/bilan_Fordead.html
解决方案
核心问题原因
使用runtime: shiny的R Markdown文档,显式调用shinyApp()会启动一个持续运行的Shiny服务进程,导致RStudio无法终止渲染流程,无法完成HTML文档的保存。
修复步骤
- 移除
shinyApp()调用:R Markdown的Shiny文档无需显式启动应用,只需定义UI和渲染逻辑即可,rmarkdown会自动处理应用加载。 - 替换为搜索型选择框:用
selectizeInput替代selectInput,原生支持搜索过滤功能,满足"搜索选择栏"需求。 - 拆分代码块:将数据加载、UI组件、渲染逻辑拆分为独立代码块,符合R Markdown规范。
修复后的完整代码
--- title: "交互式植被异常分析报告" output: html_document runtime: shiny --- ```{r setup, include=FALSE} chooseCRANmirror(ind = 1) library(shiny) library(ggplot2) library(dplyr)
anomalies_by_dept <- data.frame( Nom = c("AIN", "AIN", "AIN", "AIN", "HAUTE-SAVOIE", "HAUTE-SAVOIE", "HAUTE-SAVOIE", "HAUTE-SAVOIE", "SAVOIE", "SAVOIE", "SAVOIE", "SAVOIE"), class = c("1-轻度异常", "2-中度异常", "3-重度异常", "裸地", "1-轻度异常", "2-中度异常", "3-重度异常", "裸地", "1-轻度异常", "2-中度异常", "3-重度异常", "裸地"), surface = c(1935.2, 1470.8, 1104.9, 1245.4, 2153.9, 676.6, 1169.5, 948.9, 2.4, 1.3, 1.4, 0.7))
植被异常面积分析
区域选择
selectizeInput("selected_dept", "选择区域(支持搜索)", choices = unique(anomalies_by_dept$Nom), options = list(placeholder = "输入区域名称搜索"))
异常面积统计图
renderPlot({ selected_data <- anomalies_by_dept %>% filter(Nom == input$selected_dept) ggplot(selected_data, aes(x = class, y = surface, fill = class)) + geom_bar(stat = "identity") + labs(title = paste(input$selected_dept, "区域各异常类别面积统计"), x = "异常类别", y = "面积(公顷)") + theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1), plot.title = element_text(hjust = 0.5)) })
### 效果说明 - 修复后的代码可正常渲染并保存HTML文档,同时保留所有交互功能。 - `selectizeInput`支持实时搜索,用户可快速定位目标区域。 - 渲染完成后RStudio会自动终止后台进程,不会出现持续渲染的情况。 内容的提问来源于stack exchange,提问作者Camille_brnd
相关产品推荐
相关产品推荐

