Skip to content

Commit

Permalink
close #2274: add a function download_image() to include an image via …
Browse files Browse the repository at this point in the history
…a URL for non-HTML output formats
  • Loading branch information
yihui committed Sep 7, 2023
1 parent 7bc8b39 commit 43dd3ae
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: knitr
Type: Package
Title: A General-Purpose Package for Dynamic Report Generation in R
Version: 1.43.15
Version: 1.43.16
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
person("Abhraneel", "Sarma", role = "ctb"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export(convert_chunk_header)
export(current_input)
export(dep_auto)
export(dep_prev)
export(download_image)
export(engine_output)
export(extract_raw_output)
export(fig_chunk)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

- Updated the package vignette `vignette('knit_print', 'knitr')` to mention that package authors no longer have to make **knitr** a hard dependency if they want to define S3 methods for `knitr::knit_print` with R >= 3.6.0 (thanks, @cderv, #1929).

- Added a new function `download_image()` to download an image from a URL and include it via `include_graphics()`. This is mainly for including online images when the output format is not HTML (e.g., LaTeX), because the URL will not work as the image path, and it has to be downloaded beforehand (thanks, @bayeslearner, #2274).

## BUG FIXES

- Make the internal function `add_html_caption()` work with Quarto <= v1.3.353 (thanks, @giabaio, #2261).
Expand Down
23 changes: 23 additions & 0 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,29 @@ include_graphics = function(
structure(path, class = c('knit_image_paths', 'knit_asis'), dpi = dpi)
}

#' Download an image from the web and include it in a document
#'
#' 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 \code{xfun::\link[xfun]{download_file}()} and
#' \code{\link{include_graphics}()}.
#' @param url The URL of an image.
#' @param 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.
#' @param 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.
#' @param ... Other arguments to be passed to \code{\link{include_graphics}()}.
#' @export
#' @examplesIf interactive()
#' knitr::download_image('https://www.r-project.org/Rlogo.png')
download_image = function(url, path = xfun::url_filename(url), use_file = !pandoc_to('html'), ...) {
if (!file.exists(path) && use_file) xfun::download_file(url, path)
include_graphics(if (use_file) path else url, ...)
}

# calculate the width in inches for PNG/JPEG images given a DPI
raster_dpi_width = function(path, dpi) {
if (!file.exists(path) || is.na(dpi)) return()
Expand Down
37 changes: 37 additions & 0 deletions man/download_image.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 43dd3ae

Please sign in to comment.