英文:
File not found with knitr::include_graphics in rmarkdown via RStudio when data and pdf-image present
问题
我使用win10
系统,文件树如下:
C:\Users\xxx\Desktop\islr
C:\Users\xxx\Desktop\islr\data\Advertising.csv
C:\Users\xxx\Desktop\islr\fig\2-1.pdf
我有一些来自<https://www.statlearning.com/resources-first-edition>的**.csv数据集和.pdf图表**;或者您可以使用任何手头上可用的.csv数据
和.pdf图表
来测试我的demo.rmd
。
我的demo.rmd
如下所示:
---
title: "TTT"
author: "aaa"
date: "2023"
output: beamer_presentation
---
```{r setup, include=FALSE}
knitr::opts_knit$set(root.dir=r"[C:\Users\xxx\Desktop\islr\data]")
knitr::opts_chunk$set(
message = FALSE,
warning = FALSE,
fig.align = "center"
)
library(knitr)
数据输入
ad = read.csv("Advertising.csv", row.names=1)
head(ad)
图片输入
getwd()
file.exists("../fig/2-1.pdf")
include_graphics('../fig/2-1.pdf')
当我测试上述demo.rmd
时,数据输入部分似乎没问题(您可以在最后的代码块中添加eval=F
来测试它),而图片输入部分出现错误:
! LaTeX Error: File `../fig/2-1' not found.
然而,以下命令
getwd()
file.exists("../fig/2-1.pdf")
显示
[1] "C:/Users/xxx/Desktop/islr/data"
[1] TRUE
这让我非常困惑!我是否忽视了什么?
英文:
I use win10
system and have the following file tree
C:\Users\xxx\Desktop\islr
C:\Users\xxx\Desktop\islr\data\Advertising.csv
C:\Users\xxx\Desktop\islr\fig\2-1.pdf
I have some .csv data sets and .pdf figures from <https://www.statlearning.com/resources-first-edition>; or you can test my demo.rmd
using any .csv data
and .pdf figure
available at hand.
My demo.rmd
reads as follows:
---
title: "TTT"
author: "aaa"
date: "2023"
output: beamer_presentation
---
```{r setup, include=FALSE}
knitr::opts_knit$set(root.dir=r"[C:\Users\xxx\Desktop\islr\data]")
knitr::opts_chunk$set(
message = FALSE,
warning = FALSE,
fig.align = "center"
)
library(knitr)
```
## data input
```{r}
ad = read.csv("Advertising.csv",row.names=1)
head(ad)
```
## image input
```{r}
getwd()
file.exists("../fig/2-1.pdf")
include_graphics('../fig/2-1.pdf')
```
When I test above demo.rmd
, the data input part seems ok (you can test it by adding eval=F
in the last code chunk), while the image input part gives error as
! LaTeX Error: File `../fig/2-1' not found.
However, commands
getwd()
file.exists("../fig/2-1.pdf")
show
[1] "C:/Users/xxx/Desktop/islr/data"
[1] TRUE
which makes me very confused! Did I neglect anything?
答案1
得分: 2
问题在于通过 root.dir
设置的路径仅适用于代码块的工作目录。然而,对于图片,根路径是 Rmd
文件的路径。请参阅R代码块的工作目录。对我有效的一个选择是将相对路径包装在 normalizePath()
中并设置 rel_path=FALSE
,从而使用绝对文件路径。
knitr::opts_knit$set(root.dir = normalizePath(r"[C:\Users\xxx\Desktop\islr\data]"))
include_graphics(normalizePath("../fig/2-1.pdf"), rel_path = FALSE)
英文:
The issue is that the path set via root.dir
sets the working directory for the code chunks. However, for images the root path is the path of the Rmd
. See The working directory for R code chunks. One option which worked for me would be to use an absolute file path by wrapping your relative path in normalizePath()
and setting rel_path=FALSE
.
```
---
title: "TTT"
author: "aaa"
date: "2023"
output: beamer_presentation
---
```{r setup, include=FALSE}
knitr::opts_knit$set(root.dir = r"[C:\Users\xxx\Desktop\islr\data]")
knitr::opts_chunk$set(
message = FALSE,
warning = FALSE,
fig.align = "center"
)
library(knitr)
```
## data input
```{r}
ad = read.csv("mtcars.csv",row.names=1)
head(ad)
```
## image input
```{r}
include_graphics(normalizePath("../fig/2-1.pdf"), rel_path = FALSE)
```
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论