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

RStudio中knitr::include_graphics加载PDF图片提示文件未找到

解决R Markdown加载PDF图片的LaTeX路径问题

可能的修复方案

  • 替换为绝对路径:Windows下相对路径在LaTeX编译阶段可能出现解析异常,直接使用绝对路径调用图片,注意路径用正斜杠/:
    include_graphics("C:/Users/xxx/Desktop/islr/fig/2-1.pdf")
    
  • 清理缓存并禁用缓存:若R Markdown开启了缓存,旧缓存的路径可能干扰编译。在调用图片的代码块添加cache=FALSE,或手动删除生成的demo_cache文件夹后重新编译。
  • 配置knitr的图形路径:在R Markdown头部的YAML设置中指定图片根路径,之后直接调用文件名:
    output: pdf_document
    knitr:
      fig.path: "../fig/"
    
    include_graphics("2-1.pdf")
    
  • 用here包管理路径:避免手动切换工作目录引发的冲突,here包会基于R Markdown文件位置自动定位资源:
    library(here)
    # 读取数据
    ad_data <- read_csv(here("data", "Advertising.csv"))
    # 加载图片
    include_graphics(here("fig", "2-1.pdf"))
    
  • 确认LaTeX支持PDF图片:在R Markdown的LaTeX预amble中手动引入graphicx包(部分发行版可能需显式声明):
    output:
      pdf_document:
        includes:
          in_header: preamble.tex
    
    在preamble.tex中添加\usepackage{graphicx}

内容的提问来源于stack exchange,提问作者John Stone

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 10:12:10