Skip to contents

This is a demo of the function pixfigure(). This is useful for individual images or a set of images but not in a gallery format. The mandatory input required is one or more paths/URLs to image(s).

library(pixture)
paths <- c(
  "https://images.pexels.com/photos/572897/pexels-photo-572897.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
  "https://images.pexels.com/photos/7604425/pexels-photo-7604425.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
  "https://images.pexels.com/photos/4666748/pexels-photo-4666748.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"
)

Clicking on the thumbnail opens up the full image in a lightbox. To customize the lightbox, see the lightbox vignette.

pixfigure(paths[1])

1 Caption

Optionally, captions can be added to the images which are displayed below the images as well as in the lightbox.

pixfigure(paths[1], caption = "Night sky over the mountains")

The default is TRUE, which means that clicking on the thumbnail opens the image in a lightbox. Setting this to FALSE turns off links.

pixfigure(paths[1:2], link = FALSE)
pixfigure(
  paths[1:2],
  caption = c("Night sky over the mountains", "Though the dense rainforest"),
  link = FALSE
)

By setting this to a character vector, you can add custom links.

pixfigure(
  paths[1:2],
  link = c(
    "https://en.wikipedia.org/wiki/Milky_Way",
    "https://en.wikipedia.org/wiki/Rainforest"
  )
)
pixfigure(
  paths[1:2],
  caption = c("Night sky over the mountains", "Through the dense rainforest"),
  link = c(
    "https://en.wikipedia.org/wiki/Milky_Way",
    "https://en.wikipedia.org/wiki/Rainforest"
  )
)

By default, the caption shown in the lightbox is the same as the caption shown below the image (caption_lightbox=TRUE). Setting this to FALSE will remove captions from the lightbox.

pixfigure(
  paths[1],
  caption = "Night sky over the mountains",
  caption_lightbox = FALSE
)

By providing a character vector, you can set custom captions for the lightbox.

pixfigure(
  paths[1:2],
  caption = c("Night sky over the mountains", "Through the dense rainforest"),
  caption_lightbox = c("Milky Way galaxy as seen from Earth", "A walk in the jungle")
)

You can also only set captions for the lightbox by providing a character vector equal in length to the number of images in paths.

pixfigure(
  paths[1:2],
  caption_lightbox = c("Milky Way galaxy as seen from Earth", "A walk in the jungle")
)

4 Dimensions

The argument h and w is used to adjust the dimension of the image. This value is a string in any valid css unit.

pixfigure(paths[1], w = "400px")
pixfigure(paths[3], h = "400px", caption = "shark")

5 Fit

The argument fit is used to control css property object-fit. The differences between these options are easily visible when handling images with mixed dimensions and aspect ratios. It is set to cover by default.

pixfigure(paths[1], h = "400px")

The image scales within it’s frame rather than shrink. Now, let’s try fit="contain".

pixfigure(paths[1], h = "400px", fit = "contain")

Resize the width of the page to see the difference. Explore css property object-fit for more details, for example, on w3schools.

6 Position

The argument position determines how the image is positioned within the frame. This controls css property object-position.

pixfigure(paths[1], h = "400px", w = "400px", position = "left")
pixfigure(paths[1], h = "400px", w = "400px", position = "right")

Here is a youtube video that discusses fit and position.

7 Multiple images

Input can also be a vector images. Images are displayed one after the other.

pixfigure(
  paths,
  caption = c(
    "Night sky over the mountains",
    "Walking through dense woodland",
    "Out in the open seas"
  ),
  h = "200px",
  w = "250px",
  position = "left"
)

8 Local images

Local images (not on the internet) can be provided as relative or absolute paths. The images do not show up in the RStudio notebook preview, but they should work in a rendered HTML document.

  Pixture does not move, copy or embed images. The images must be in their final intended destination. It is left to the user to handle images as assets. Images are not embedded into self contained documents.

8.1 Relative path

This example assumes your images are available next to the Rmd/qmd/HTML file in a directory named images and that you copy these assets if the Rmd/qmd/HTML is relocated.

pixfigure("images/pexels-photo-3126574.jpg", h = "300px")

8.2 Absolute path

Here is an example using sample images from the package. These should work locally, but may not work if the rendered HTML is relocated.

paths <- list.files(
  system.file("extdata/images", package = "pixture"),
  full = TRUE
)
paths
[1] "/opt/homebrew/Caskroom/miniforge/envs/r-4.5-arm/lib/R/library/pixture/extdata/images/pexels-photo-3126574.jpg"
[2] "/opt/homebrew/Caskroom/miniforge/envs/r-4.5-arm/lib/R/library/pixture/extdata/images/pexels-photo-4210900.jpg"
[3] "/opt/homebrew/Caskroom/miniforge/envs/r-4.5-arm/lib/R/library/pixture/extdata/images/pexels-photo-4666748.jpg"
[4] "/opt/homebrew/Caskroom/miniforge/envs/r-4.5-arm/lib/R/library/pixture/extdata/images/pexels-photo-4932184.jpg"
[5] "/opt/homebrew/Caskroom/miniforge/envs/r-4.5-arm/lib/R/library/pixture/extdata/images/pexels-photo-572897.jpg"
[6] "/opt/homebrew/Caskroom/miniforge/envs/r-4.5-arm/lib/R/library/pixture/extdata/images/pexels-photo-7604425.jpg"
# pixfigure(paths[1])

9 Session

R version 4.5.1 (2025-06-13)
Platform: aarch64-apple-darwin20.0.0
Running under: macOS Sequoia 15.7.1

Matrix products: default
BLAS/LAPACK: /opt/homebrew/Caskroom/miniforge/envs/r-4.5-arm/lib/libopenblas.0.dylib;  LAPACK version 3.12.0

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

time zone: Europe/Stockholm
tzcode source: system (macOS)

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

other attached packages:
[1] pixture_0.2.1

loaded via a namespace (and not attached):
 [1] digest_0.6.37     later_1.4.4       R6_2.6.1          httpuv_1.6.16
 [5] fastmap_1.2.0     xfun_0.54         magrittr_2.0.4    shiny_1.11.1
 [9] knitr_1.50        htmltools_0.5.8.1 rmarkdown_2.30    lifecycle_1.0.4
[13] promises_1.5.0    cli_3.6.5         xtable_1.8-4      compiler_4.5.1
[17] tools_4.5.1       mime_0.13         evaluate_1.0.5    Rcpp_1.1.0
[21] yaml_2.3.10       otel_0.2.0        rlang_1.1.6       jsonlite_2.0.0
[25] htmlwidgets_1.6.4