【问题标题】:R blogdown fails to find image filesR blogdown 找不到图像文件
【发布时间】:2020-06-19 15:41:06
【问题描述】:

我(拥有)一个使用 blogdown 制作的工作网站。几个月来我第一次尝试重建该站点,但我遇到了错误,因为它显然找不到我的图像。

为了进行reprex,我创建了一个新项目,并选择了“带有 blogdown 的网站”选项并采用了所有默认值。该网站的构建没有 blogdown:::serve_site() 的问题。我把一张名为 x.png 的图片放到了静态目录中,并添加了:

```{r x, fig.cap='x', tidy=FALSE}
knitr::include_graphics("x.png")
```

进入 2015-07-23-r-rmarkdown.Rmd 文件。当我告诉它 blogdown:::serve_site() 我得到:

Error in knitr::include_graphics("x.png") : 
  Cannot find the file(s): "x.png"
Calls: local ... withCallingHandlers -> withVisible -> eval -> eval -> <Anonymous>
Execution halted

然后我尝试使用“插件”菜单添加图像并选择Insert Image 并将其指向文件。它复制了文件并将预期的降价写入 2015-07-23-r-rmarkdown.Rmd:

![](/post/2015-07-23-r-rmarkdown_files/x.png)

这引发了这个错误:

pandoc: /post/2015-07-23-r-rmarkdown_files/x.png: openBinaryFile: does not exist (No such file or directory)
Error: pandoc document conversion failed with error 1
Execution halted

你有什么想法吗?

> sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.3

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.3           later_1.0.0          compiler_3.6.2       pillar_1.4.3        
 [5] prettyunits_1.1.1    tools_3.6.2          digest_0.6.25        packrat_0.5.0-25    
 [9] pkgbuild_1.0.6       jsonlite_1.6.1       evaluate_0.14        lifecycle_0.2.0     
[13] tibble_2.1.3         gtable_0.3.0         pkgconfig_2.0.3      rlang_0.4.5         
[17] cli_2.0.2            rstudioapi_0.11      parallel_3.6.2       yaml_2.2.1          
[21] blogdown_0.18        xfun_0.12            loo_2.2.0            gridExtra_2.3       
[25] stringr_1.4.0        dplyr_0.8.4          knitr_1.28           tidyselect_1.0.0    
[29] stats4_3.6.2         grid_3.6.2           glue_1.3.1           inline_0.3.15       
[33] R6_2.4.1             processx_3.4.2       fansi_0.4.1          rmarkdown_2.1       
[37] bookdown_0.18        rstan_2.19.3         servr_0.16           purrr_0.3.3         
[41] magrittr_1.5         callr_3.4.2          ggplot2_3.3.0        promises_1.1.0      
[45] matrixStats_0.55.0   scales_1.1.0         ps_1.3.2             StanHeaders_2.21.0-1
[49] htmltools_0.4.0      rsconnect_0.8.16     assertthat_0.2.1     mime_0.9            
[53] colorspace_1.4-1     httpuv_1.5.2         stringi_1.4.6        munsell_0.5.0       
[57] crayon_1.3.4  
> rmarkdown::pandoc_version()
[1] ‘2.7.3’

【问题讨论】:

  • 我建议您使用@allan 的答案,但您的文件未显示在静态文件夹中的原因是因为您当前的工作目录是您的博客文章的位置。在调用 png 文件之前,您需要指定静态目录。将图片放在与您的帖子相同的目录中会容易得多

标签: r blogdown


【解决方案1】:

使用插件的第二种方法:你不应该编织这个 RMD 文件。如果您只使用插件“服务站点”,它会按预期工作。当然如果你编织它是行不通的,因为文件被复制到“static/post/....”并且你的 RMD 文件在“content/post/...”中

【讨论】:

  • 非常感谢。知道为什么 knitr::include_graphics("x.png") 无法发现静态目录中的文件吗?
  • @J_F 是正确的,您不应该编写 Rmd 文件(这在 blogdown 书中提到:bookdown.org/yihui/blogdown/workflow.html)。如果x.pngstatic/ 目录下,您应该很好地使用knitr::include_graphics("/x.png", error = FALSE)Serve Site
【解决方案2】:

尝试将您的图片放在“内容/帖子”中。您可以使用插件“新帖子”并为每个帖子“content/post/my_incredible_post”创建一个子目录,然后您的文件将全部位于同一个位置。然后你可以使用:

knitr::include_graphics("x.png")

![](x.png)

【讨论】:

  • 谢谢艾伦。我将图像添加到“内容/帖子”并留下代码 {r x, fig.cap='x', tidy=FALSE} knitr::include_graphics("x.png") 。使用serve_site() 可以正常工作,但include_graphics 调用没有显示图像。我使用了插件,它复制了图像,并显示为serve_site()![](/post/2015-07-23-r-rmarkdown_files/x.png)。我在include_graphics() 中尝试了该路径,但它抛出了我原来的错误:找不到文件:“/post/2015-07-23-r-rmarkdown_files/x.png”。
  • 我认为您需要了解内容文件夹结构。阅读这篇文章,了解 blogdown 中的一些常见功能。 andreaczhang.rbind.io/post/my-1st-blogpost
【解决方案3】:

以下是我如何组织博客文章的示例:

site/content/blog/veterans/ 包含我的博客文章index.md

site/content/blog/veterans/gallary 包含我在博文中使用的图片。 要在博文中调用我的图片,我使用![png](./gallery/uspop.png)

如果它和我的博文在同一个目录下,我会使用![png](./uspop.png)

如果有帮助,您可以查看我的 GitHub 存储库: https://github.com/datasurfergtx/site/tree/master/content/blog/veterans

【讨论】:

    【解决方案4】:

    谢谢大家。我原来的网站需要添加“error = FALSE”选项。我认为由于 knitr 1.28 中所做的更改,此代码失败:

    knitr::include_graphics("/post/2019-12-10-installing-r-rstudio-on-windows_files/marginWin.png")
    

    但这有效:

    knitr::include_graphics("/post/2019-12-10-installing-r-rstudio-on-windows_files/marginWin.png", error = FALSE)
    

    我在制作 reprex 时引入了第二个问题。 sigh... include_graphics() 调用中的路径需要以 / 开头

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-30
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      相关资源
      最近更新 更多