How can I insert an image from internet to the pdf file produced by R bookdown in a smart way?

a 夏天 提交于 2019-12-04 12:13:32

You can check the current output format in knitr::opts_knit$get('rmarkdown.pandoc.to'). If it is 'html', you use the URL of the image, otherwise use the file path, e.g.

cover_url = 'https://bookdown.org/yihui/bookdown/images/cover.jpg'
if (!file.exists(cover_file <- 'cover.jpg'))
  download.file(cover_url, cover_file, mode = 'wb')
knitr::include_graphics(if (identical(knitr:::pandoc_to(), 'html')) cover_url else cover_file)

Here knitr:::pandoc_to() is an internal wrapper function of knitr::opts_knit$get('rmarkdown.pandoc.to').

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!