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.

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"))

3 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")

4 Fit

The argument fit is used to control css property object-fit. It is set to cover by default.

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

Resize the width of the page to see the effect of fit="cover". The image scales within it’s frame rather than shrink. Explore css property object-fit for more details, for example, on w3schools.

5 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.

6 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")

7 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.

7.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")

7.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/base/envs/r-4.4/lib/R/library/pixture/extdata/images/pexels-photo-3126574.jpg"
[2] "/opt/homebrew/Caskroom/miniforge/base/envs/r-4.4/lib/R/library/pixture/extdata/images/pexels-photo-4210900.jpg"
[3] "/opt/homebrew/Caskroom/miniforge/base/envs/r-4.4/lib/R/library/pixture/extdata/images/pexels-photo-4666748.jpg"
[4] "/opt/homebrew/Caskroom/miniforge/base/envs/r-4.4/lib/R/library/pixture/extdata/images/pexels-photo-4932184.jpg"
[5] "/opt/homebrew/Caskroom/miniforge/base/envs/r-4.4/lib/R/library/pixture/extdata/images/pexels-photo-572897.jpg"
[6] "/opt/homebrew/Caskroom/miniforge/base/envs/r-4.4/lib/R/library/pixture/extdata/images/pexels-photo-7604425.jpg"
# pixfigure(paths[1])

8 Session

R version 4.4.1 (2024-06-14)
Platform: x86_64-apple-darwin13.4.0
Running under: macOS Big Sur ... 10.16

Matrix products: default
BLAS/LAPACK: /opt/homebrew/Caskroom/miniforge/base/envs/r-4.4/lib/libopenblasp-r0.3.21.dylib;  LAPACK version 3.9.0

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

time zone: Europe/Stockholm
tzcode source: internal

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

other attached packages:
[1] pixture_0.1.0

loaded via a namespace (and not attached):
 [1] digest_0.6.36     later_1.3.2       R6_2.5.1          httpuv_1.6.15
 [5] fastmap_1.2.0     xfun_0.45         magrittr_2.0.3    shiny_1.8.1.1
 [9] knitr_1.48        htmltools_0.5.8.1 rmarkdown_2.27    lifecycle_1.0.4
[13] promises_1.3.0    cli_3.6.3         xtable_1.8-4      compiler_4.4.1
[17] rstudioapi_0.16.0 tools_4.4.1       mime_0.12         evaluate_0.24.0
[21] Rcpp_1.0.12       yaml_2.3.9        rlang_1.1.4       jsonlite_1.8.8
[25] htmlwidgets_1.6.4