Skip to content

Commit 43dd3ae

Browse files
committed
close #2274: add a function download_image() to include an image via a URL for non-HTML output formats
1 parent 7bc8b39 commit 43dd3ae

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: knitr
22
Type: Package
33
Title: A General-Purpose Package for Dynamic Report Generation in R
4-
Version: 1.43.15
4+
Version: 1.43.16
55
Authors@R: c(
66
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
77
person("Abhraneel", "Sarma", role = "ctb"),

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export(convert_chunk_header)
3535
export(current_input)
3636
export(dep_auto)
3737
export(dep_prev)
38+
export(download_image)
3839
export(engine_output)
3940
export(extract_raw_output)
4041
export(fig_chunk)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
- 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).
1212

13+
- 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).
14+
1315
## BUG FIXES
1416

1517
- Make the internal function `add_html_caption()` work with Quarto <= v1.3.353 (thanks, @giabaio, #2261).

R/plot.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,29 @@ include_graphics = function(
490490
structure(path, class = c('knit_image_paths', 'knit_asis'), dpi = dpi)
491491
}
492492

493+
#' Download an image from the web and include it in a document
494+
#'
495+
#' When including images in non-HTML output formats such as LaTeX/PDF, URLs will
496+
#' not work as image paths. In this case, we have to download the images. This
497+
#' function is a wrapper of \code{xfun::\link[xfun]{download_file}()} and
498+
#' \code{\link{include_graphics}()}.
499+
#' @param url The URL of an image.
500+
#' @param path The download path (inferred from the URL by default). If the file
501+
#' exists, it will not be downloaded (downloading can take time and requires
502+
#' Internet connection). If you are sure the file needs to be downloaded
503+
#' again, delete it beforehand.
504+
#' @param use_file Whether to use the URL or the download path to include the
505+
#' image. By default, the URL is used for HTML output formats, and the file
506+
#' path is used for other output formats.
507+
#' @param ... Other arguments to be passed to \code{\link{include_graphics}()}.
508+
#' @export
509+
#' @examplesIf interactive()
510+
#' knitr::download_image('https://www.r-project.org/Rlogo.png')
511+
download_image = function(url, path = xfun::url_filename(url), use_file = !pandoc_to('html'), ...) {
512+
if (!file.exists(path) && use_file) xfun::download_file(url, path)
513+
include_graphics(if (use_file) path else url, ...)
514+
}
515+
493516
# calculate the width in inches for PNG/JPEG images given a DPI
494517
raster_dpi_width = function(path, dpi) {
495518
if (!file.exists(path) || is.na(dpi)) return()

man/download_image.Rd

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)