You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RStudio knit flexdashboard时遇pandoc错误1455和251求助

解决flexdashboard Knit为HTML时的Pandoc错误1455和251问题

问题描述

作为R/RStudio新手,制作结合地理空间边界文件与Excel数据的flexdashboard(基于rmarkdown),单独运行所有代码块时预览正常,但将文档knit为HTML格式的最终阶段,出现Pandoc错误1455和251。

运行环境

  • Windows 10
  • R版本4.3.1(2023-06-16 ucrt)
  • RStudio版本2023.09.0+463 "Desert Sunflower"

已尝试操作

  • 将代码块拆分为更小单元
  • 在leaflet地图代码块中设置self.contained=FALSE参数
  • 下载Windows版最新PANDOC 3.1.11并在独立代码块调用
  • 定位到引发错误的代码部分

可复现代码

---
title: "OBSERVATORIO DATOS"
output: 
  flexdashboard::flex_dashboard:
    logo: "LogoReducido.png"
    orientation: "column"
    vertical_layout: "fill"
    
---
CLASIFICACIÓN
=======================================================================

Column
-----------------------------------------------------------------------

```{r, setup, include=FALSE}
rmarkdown::find_pandoc(version = '3.1.11')
setwd("C:/Users/BigO/Documents/Reporteria")

library(tmap) 
library(dplyr)  
library(tidyverse) 
library(tidycensus)
library(readxl) 
library(knitr) 
library(kableExtra)
library(leaflet) 
library(leaflet.extras)
library(leafpop)
library(raster) 
library(rgdal)
library(gridExtra) 
library(janitor) 
library(sf) 
library(viridis) 
library(osrm) 
library(htmltools)
library(htmlwidgets)
library(DT)
library(sp)
library(magrittr)
library(pdftools)
library(rJava)
library(tabulizer)
library(purrr)
library(tidyr)
library(tibble)
library(PDE)
library(googlesheets4)
library(googledrive)
library(plotrix)
library(formattable)
map_distritos <- geojsonio::geojson_read("C:/Users/BigO/Documents/Panama_District_Boundaries_2016.geojson", what = "sp")

names(map_distritos@data)[5] <- "MUNICIPIO"
map_distritos$MUNICIPIO[35] <- "Santa Fe Darien"

municipios_pob <- read_excel("C:/Users/BigO/Documents/Reporteria/Poblacion_sexo.xlsx", sheet = "TABLA1")
municipios_pob <- municipios_pob[-c(27, 83),]

map_dist_pob <- sp::merge(map_distritos, municipios_pob, by.x = "MUNICIPIO", by.y= "MUNICIPIO")

map_dist_pob1 <- st_as_sf(map_dist_pob)

MAPA

map_metro <- map_dist_pob1 %>% dplyr::filter(CANT_POB>=250000) %>% arrange(desc(CANT_POB)) %>% mutate(CLASIF = "Metropolitano")
map_urbano <- map_dist_pob1 %>% dplyr::filter(CANT_POB %in% 40001:250000) %>% arrange(desc(CANT_POB)) %>% mutate(CLASIF = "Urbano")
map_semiurbano <- map_dist_pob1 %>% dplyr::filter(CANT_POB %in% 40000:6001) %>% arrange(desc(CANT_POB)) %>% mutate(CLASIF = "Semiurbano")
map_rural <- map_dist_pob1 %>% dplyr::filter(CANT_POB %in% 6000:1) %>% arrange(desc(CANT_POB)) %>% mutate(CLASIF = "Rural")

map_dist_pob2 <- rbind(map_metro, map_urbano, map_semiurbano, map_rural)

pal <- colorNumeric("YlOrRd", domain = map_dist_pob2$CANT_POB)

#pal <- colorNumeric("viridis", NULL)

rr <- tags$div(
  HTML('<a href:" "> Fuente: IGNTG, INEC Y AMUPA <br> Elaboracion: AMUPA </a>')
)

label_1 <- paste0(
  "Provincia:", map_dist_pob2$PROVINCIA, "<br>",
  "Municipio:", map_dist_pob2$MUNICIPIO, "<br>",
  "Población:", map_dist_pob2$CANT_POB, "<br>",
  "Clasificación:", map_dist_pob2$CLASIF, "<br>") %>%
  lapply(htmltools::HTML)

leaflet(map_dist_pob2) %>%
  addTiles() %>%
  addPolygons(data = map_dist_pob2 , stroke = TRUE, smoothFactor = 0.3, fillOpacity = 1, color = "black", weight = 1, opacity = 0.5, fillColor = ~pal(CANT_POB), label = ~label_1) %>% 
  setView(lat = 8.457803, lng = -79.818159, zoom = 7) %>%
  addLegend("bottomright",pal = pal, values = ~CANT_POB, title = "Población", opacity = 1.0) %>%
  addControl(rr, position = "bottomleft") %>%
  addResetMapButton()
## 解决方案建议
### 1. 修正Pandoc路径配置
不要在setup代码块中调用`rmarkdown::find_pandoc`,直接在RStudio中手动配置:
- 打开`Tools -> Global Options -> R Markdown`
- 在Pandoc选项中指定你下载的Pandoc 3.1.11的安装路径(例如`C:\Program Files\Pandoc\pandoc.exe`)
- 重启RStudio后重新尝试knit

### 2. 调整代码块与YAML参数
- 将`self_contained: false`移至YAML的output设置中,而非代码块参数:
  ```yaml
  output: 
    flexdashboard::flex_dashboard:
      logo: "LogoReducido.png"
      orientation: "column"
      vertical_layout: "fill"
      self_contained: false
  • 移除地图代码块中的results='asis'参数,该参数会干扰leaflet的HTML输出解析

3. 统一地理数据格式

避免同时混用sp和sf对象,全程使用sf格式简化流程:

  • 修改地理数据读取代码:
    map_distritos <- geojsonio::geojson_read("C:/Users/BigO/Documents/Panama_District_Boundaries_2016.geojson", what = "sf")
    
  • 用dplyr::left_join替代sp::merge完成数据合并,保持sf对象一致性:
    map_dist_pob <- dplyr::left_join(map_distritos, municipios_pob, by = "MUNICIPIO")
    

4. 修复HTML控件语法错误

修正rr控件中的无效链接语法,改为:

rr <- tags$div(
  HTML('Fuente: IGNTG, INEC Y AMUPA <br> Elaboracion: AMUPA')
)

无效的href:" "语法会导致Pandoc解析HTML时出错。

5. 清理未使用的库

移除代码中未实际用到的库(如tidycensus、osrm、pdftools、rJava等),减少环境冲突,只保留必要库:

library(dplyr)  
library(tidyverse) 
library(readxl) 
library(leaflet) 
library(leaflet.extras)
library(htmltools)
library(sf) 
library(viridis) 

内容的提问来源于stack exchange,提问作者Olmedo Alonso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 00:17:32