Learn R Programming

knitr (version 1.48)

download_image: Download an image from the web and include it in a document

Description

When including images in non-HTML output formats such as LaTeX/PDF, URLs will not work as image paths. In this case, we have to download the images. This function is a wrapper of xfun::download_file() and include_graphics().

Usage

download_image(
  url,
  path = xfun::url_filename(url),
  use_file = !pandoc_to("html"),
  ...
)

Arguments

url

The URL of an image.

path

The download path (inferred from the URL by default). If the file exists, it will not be downloaded (downloading can take time and requires Internet connection). If you are sure the file needs to be downloaded again, delete it beforehand.

use_file

Whether to use the URL or the download path to include the image. By default, the URL is used for HTML output formats, and the file path is used for other output formats.

...

Other arguments to be passed to include_graphics().

Examples

Run this code
if (FALSE) { # interactive()
knitr::download_image("https://www.r-project.org/Rlogo.png")
}

Run the code above in your browser using DataLab