Skip to content

Instantly share code, notes, and snippets.

@tomsing1
Created January 8, 2024 19:09
Show Gist options
  • Save tomsing1/86914347f938c72bcd2c84b73975091f to your computer and use it in GitHub Desktop.
Save tomsing1/86914347f938c72bcd2c84b73975091f to your computer and use it in GitHub Desktop.
Create an image tag pointing to a random picture from Lorem Picsum
#' Create an image tag with an example image
#'
#' @param width Scalar integer, the width of the image
#' @param height Scalar integer, the height of the image
#' @param title Scalar character, the title of the image
#' @return A `shiny.tag` with the URL to a random image from
#' [Lorem Picsum](https://picsum.photos/)
#' @export
#' @importFrom htmltool tags
#' @importFrom checkmate assert_count assert_character
#' @examples
#' image_placeholder(100, 200)
image_placeholder <- function(width = 200, height = 300,
title = "Example Image") {
checkmate::assert_count(width)
checkmate::assert_count(height, null.ok = TRUE)
checkmate::assert_character(title)
url <- ifelse(is.null(height),
sprintf("https://picsum.photos/%s", width),
sprintf("https://picsum.photos/%s/%s", width, height))
htmltools::tags$img(src=url, title=title, width=width,
height=ifelse(is.null(height), width, height))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment