Skip to content

feat: random_image_ext() — Lorem Picsum images for Shiny prototyping (closes #8)#17

Open
VincentGuyader wants to merge 2 commits into
masterfrom
feat/random-image-ext
Open

feat: random_image_ext() — Lorem Picsum images for Shiny prototyping (closes #8)#17
VincentGuyader wants to merge 2 commits into
masterfrom
feat/random-image-ext

Conversation

@VincentGuyader
Copy link
Copy Markdown
Member

Reworks @feddelegrand7's PR #8 into a mergeable state (the original branch is 5 years stale and had no tests).

What it does

random_image_ext(width = 400, height = 400, seed = NULL) returns an <img> htmltools tag whose src points at the Lorem Picsum API:

random_image_ext()                          # <img src="https://picsum.photos/400/400">
random_image_ext(width = 200, height = 300) # <img src="https://picsum.photos/200/300">
random_image_ext(seed = "caramba")          # <img src="https://picsum.photos/seed/caramba/400/400">

No network request is performed by the function itself — only the URL is built; the browser fetches the image. Drop it straight into a Shiny UI.

Changes vs PR #8

  • Dropped the glue dependency — plain sprintf().
  • Input validation: width / height must be single positive integers; seed must be a single non-missing scalar (coerced to character).
  • seed is URL-encoded (utils::URLencode(reserved = TRUE)), so spaces / slashes don't break the URL.
  • @examples no longer hit the network in a way that bothers R CMD check (the call only builds a tag).
  • Full test suite: tests/testthat/test-image-ext.R (return type, dimensions in URL, seeded URL + encoding, validation of every argument). Written first (red), then the implementation.
  • htmltools added to Imports (used by htmltools::img); the previously-unused utils import is now actually used (URLencode).

Test plan

Note for the merge

Touches DESCRIPTION (adds htmltools to Imports) like #15 / #16 — trivial conflict if those land first. Independent of everything else otherwise.

Closes #8

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new random_image_ext() helper to generate an htmltools <img> tag whose src points at the Lorem Picsum API, intended for quick Shiny UI prototyping.

Changes:

  • Introduces random_image_ext(width, height, seed) with input validation and URL-encoding for seed.
  • Adds test coverage for URL construction and argument validation.
  • Wires the feature into the package exports/docs and adds htmltools to Imports.

Reviewed changes

Copilot reviewed 5 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
R/random_image_ext.R New implementation of random_image_ext() building a Picsum URL and returning an htmltools::img() tag.
tests/testthat/test-image-ext.R New tests validating return type, URL formatting, seed encoding, and argument validation.
NAMESPACE Exports random_image_ext() and imports htmltools::img, attempt::stop_if_not, utils::URLencode.
DESCRIPTION Adds htmltools to Imports and updates RoxygenNote.
man/random_image_ext.Rd Generated documentation for the new function.
NEWS.md Changelog entry for random_image_ext().
man/shinipsum-package.Rd Minor package documentation formatting adjustment.
Files not reviewed (2)
  • man/random_image_ext.Rd: Language not supported
  • man/shinipsum-package.Rd: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread R/random_image_ext.R Outdated
Comment on lines +26 to +27
is.numeric(.x) && length(.x) == 1L && !is.na(.x) &&
.x >= 1L && .x == as.integer(.x)
Comment thread R/random_image_ext.R Outdated
} else {
stop_if_not(
seed,
~ length(.x) == 1L && !is.na(.x),
Comment on lines +28 to +43
test_that("width / height are validated", {
expect_error(random_image_ext(width = 0), "width")
expect_error(random_image_ext(width = -10), "width")
expect_error(random_image_ext(width = 1.5), "width")
expect_error(random_image_ext(width = c(100, 200)), "width")
expect_error(random_image_ext(width = NA), "width")
expect_error(random_image_ext(width = "100"), "width")
expect_error(random_image_ext(height = 0), "height")
expect_error(random_image_ext(height = "x"), "height")
})

test_that("seed is validated", {
expect_error(random_image_ext(seed = c("a", "b")), "seed")
expect_error(random_image_ext(seed = NA), "seed")
expect_error(random_image_ext(seed = character(0)), "seed")
# numeric / other atomic scalars are coerced to character
Reworked from PR #8: drop the glue dependency (use sprintf), validate
width/height/seed, URL-encode the seed, document that no network request
is made, add a full test suite. htmltools added to Imports (used by
htmltools::img); utils import is now actually used (URLencode).

TDD: tests/testthat/test-image-ext.R written first (red), then implementation.

Co-authored-by: feddelegrand7 <ihaddaden.fodeil@gmail.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 7 changed files in this pull request and generated no new comments.

Files not reviewed (2)
  • man/random_image_ext.Rd: Language not supported
  • man/shinipsum-package.Rd: Language not supported

- valid_dim() predicate is now total (is.finite + round, never NA/error):
  Inf / NaN / 1e400 / complex / logical fail with the friendly message
- seed predicate gains an is.atomic() guard so list()/non-atomic inputs
  fail cleanly instead of throwing
- format dimensions with %.0f (no as.integer overflow to NA)
- add tests for the tricky inputs above

Co-authored-by: feddelegrand7 <ihaddaden.fodeil@gmail.com>
@VincentGuyader
Copy link
Copy Markdown
Member Author

Thanks @copilot — all 3 points addressed in fa86cdd:

  • valid_dim() could return NA / throw: the predicate is now total — is.numeric(.x) && length(.x) == 1L && is.finite(.x) && .x >= 1 && .x == round(.x). Inf / NaN / 1e400 / complex / logical all fail with the friendly `width` must be a single positive integer message instead of an NA predicate or a low-level error. Dimensions are now formatted with %.0f so there's no as.integer() overflow-to-NA either.
  • seed predicate could throw on non-atomic input: added an is.atomic(.x) guard, so seed = list("a") (and friends) fail with the custom message.
  • Tests: added explicit cases for width = Inf / NaN / 1e400 / 1+0i / TRUE and seed = list("a") / seed = TRUE.

R CMD check: Status OK (0/0/0). R/random_image_ext.R at 100% coverage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants