File not found with knitr::include_graphics in rmarkdown via RStudio when data and pdf-image present

huangapple go评论68阅读模式
英文:

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&#39; 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: &quot;TTT&quot;
author: &quot;aaa&quot;
date: &quot;2023&quot;
output: beamer_presentation
---

```{r setup, include=FALSE}
knitr::opts_knit$set(root.dir=r&quot;[C:\Users\xxx\Desktop\islr\data]&quot;)
knitr::opts_chunk$set(
  message = FALSE, 
  warning = FALSE,
  fig.align = &quot;center&quot;
)
library(knitr)
```

## data input

```{r}
ad = read.csv(&quot;Advertising.csv&quot;,row.names=1)
head(ad)
```

## image input

```{r}
getwd()
file.exists(&quot;../fig/2-1.pdf&quot;)
include_graphics(&#39;../fig/2-1.pdf&#39;)
```

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&#39; not found.

However, commands

getwd()
file.exists(&quot;../fig/2-1.pdf&quot;)

show

[1] &quot;C:/Users/xxx/Desktop/islr/data&quot;
[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: &quot;TTT&quot;
author: &quot;aaa&quot;
date: &quot;2023&quot;
output: beamer_presentation
---


```{r setup, include=FALSE}
knitr::opts_knit$set(root.dir = r&quot;[C:\Users\xxx\Desktop\islr\data]&quot;)
knitr::opts_chunk$set(
  message = FALSE, 
  warning = FALSE,
  fig.align = &quot;center&quot;
)
library(knitr)
```

## data input

```{r}
ad = read.csv(&quot;mtcars.csv&quot;,row.names=1)
head(ad)
```

## image input

```{r}
include_graphics(normalizePath(&quot;../fig/2-1.pdf&quot;), rel_path = FALSE)
```

File not found with knitr::include_graphics in rmarkdown via RStudio when data and pdf-image present

huangapple
  • 本文由 发表于 2023年4月19日 16:20:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定