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

R Markdown导出Word文档时leaflet地图无法显示的问题求助

问题:Leaflet地图在officedown生成的Word文档中不显示

我使用R Markdown结合officedown生成包含leaflet地图的Word文档时遇到问题:在RStudio中leaflet地图可正常显示,但点击Knit导出Word后地图不显示。近期更换电脑并重装了RStudio,此前该功能可正常运行,已尝试重启电脑和重装leaflet包。相关代码配置如下:

---
author: "Autor"
title:  "Informe"
subtitle: "Programa"
date: "`r format(Sys.time(), '%d de %B, %Y')`"
output:
  officedown::rdocx_document:
    plots:
      style: Normal
      align: center
      caption:
        style: Image Caption
        pre: 'Figura '
        sep: ': '
    tables:
      style: Table
      layout: autofit
      width: 1.0
      caption:
        style: Table Caption
        pre: 'Tabla '
        sep: ': '
      conditional:
        first_row: true
        first_column: false
        last_row: false
        last_column: false
        no_hband: false
        no_vband: true
  page_size:
    width: 8.3
    height: 11.7
    orient: "portrait"
  page_margins:
    bottom: 1
    top: 1
    right: 1.25
    left: 1.25
    header: 0.5
    footer: 0.5
    gutter: 0.5
bibliography: referencias.bib
csl: apa-6th-edition.csl
always_allow_html: true
editor_options: 
  markdown: 
    wrap: 72
  chunk_output_type: console
---
library(leaflet)
library(officedown)
library(officer)
leaflet::leaflet() %>% addTiles()

解决方法

1. 修正YAML配置缩进错误

你的YAML里page_size和page_margins缩进层级错误,它们应该属于officedown::rdocx_document的子配置项,当前缩进会导致配置失效,甚至影响HTML内容的解析逻辑。修正后的YAML如下:

---
author: "Autor"
title:  "Informe"
subtitle: "Programa"
date: "`r format(Sys.time(), '%d de %B, %Y')`"
output:
  officedown::rdocx_document:
    plots:
      style: Normal
      align: center
      caption:
        style: Image Caption
        pre: 'Figura '
        sep: ': '
    tables:
      style: Table
      layout: autofit
      width: 1.0
      caption:
        style: Table Caption
        pre: 'Tabla '
        sep: ': '
      conditional:
        first_row: true
        first_column: false
        last_row: false
        last_column: false
        no_hband: false
        no_vband: true
    page_size:
      width: 8.3
      height: 11.7
      orient: "portrait"
    page_margins:
      bottom: 1
      top: 1
      right: 1.25
      left: 1.25
      header: 0.5
      footer: 0.5
      gutter: 0.5
bibliography: referencias.bib
csl: apa-6th-edition.csl
always_allow_html: true
editor_options: 
  markdown: 
    wrap: 72
  chunk_output_type: console
---

2. 将Leaflet地图转为图片嵌入Word

Word原生不支持渲染交互式HTML地图,officedown对HTML内容的兼容有限,可通过将地图导出为图片的方式嵌入:

library(leaflet)
library(officedown)
library(officer)
library(htmlwidgets)
library(magick)

# 生成地图对象
map <- leaflet() %>% addTiles()

# 临时保存为HTML再转为图片
temp_html <- tempfile(fileext = ".html")
saveWidget(map, temp_html)
map_image <- image_read(temp_html)

# 在Word文档中插入图片
body_add_img(doc, src = map_image, width = 6, height = 4)

3. 匹配兼容的包版本

新环境下包版本可能存在兼容性问题,可安装过往正常运行时的版本:

# 卸载现有包
remove.packages(c("leaflet", "officedown", "officer", "htmlwidgets"))

# 安装指定兼容版本(可根据实际调整版本号)
install.packages("leaflet", version = "2.2.0")
install.packages("officedown", version = "0.3.0")
install.packages("officer", version = "0.6.2")
install.packages("htmlwidgets", version = "1.6.2")

4. 调整代码块选项

在Leaflet代码块中添加results='asis',确保HTML内容被正确解析:

```{r leaflet-map, results='asis', echo=FALSE}
leaflet::leaflet() %>% addTiles()
---

内容的提问来源于stack exchange,提问作者Camilo Montes
相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 10:11:18