Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v5

- uses: r-lib/actions/setup-pandoc@v2

Expand Down
54 changes: 24 additions & 30 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -1,47 +1,41 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
on:
push:
branches: [main, master]
workflow_dispatch:

name: pkgdown

jobs:
pkgdown:
runs-on: macOS-latest
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v2
permissions:
contents: write

- uses: r-lib/actions/setup-r@v1
steps:
- uses: actions/checkout@v5

- uses: r-lib/actions/setup-pandoc@v1
- uses: r-lib/actions/setup-pandoc@v2

- name: Query dependencies
run: |
install.packages('remotes')
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}
- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- name: Cache R packages
uses: actions/cache@v2
- uses: r-lib/actions/setup-r-dependencies@v2
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-

- name: Install dependencies
run: |
remotes::install_deps(dependencies = TRUE)
remotes::install_github("ThinkR-open/thinkrtemplate")
install.packages("pkgdown", type = "binary")
shell: Rscript {0}
extra-packages: any::pkgdown, ThinkR-open/thinkrtemplate
needs: website

- name: Install package
run: R CMD INSTALL .
- name: Build site
run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
shell: Rscript {0}

- name: Deploy package
run: |
git config --local user.email "actions@github.com"
git config --local user.name "GitHub Actions"
Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE)'
- name: Deploy to GitHub pages
if: github.event_name != 'pull_request'
uses: JamesIves/github-pages-deploy-action@v4.5.0
with:
clean: false
branch: gh-pages
folder: docs
42 changes: 13 additions & 29 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
@@ -1,46 +1,30 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
on:
push:
branches:
- master
branches: [main, master]
pull_request:
branches:
- master
branches: [main, master]

name: test-coverage

jobs:
test-coverage:
runs-on: macOS-latest
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@v1

- uses: r-lib/actions/setup-pandoc@v1

- name: Query dependencies
run: |
install.packages('remotes')
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}
steps:
- uses: actions/checkout@v5

- name: Cache R packages
uses: actions/cache@v2
- uses: r-lib/actions/setup-r@v2
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-
use-public-rspm: true

- name: Install dependencies
run: |
install.packages(c("remotes"))
remotes::install_deps(dependencies = TRUE)
remotes::install_cran("covr")
shell: Rscript {0}
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr
needs: coverage

- name: Test coverage
run: covr::codecov()
run: covr::codecov(quiet = FALSE)
shell: Rscript {0}
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ Suggests:
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
RoxygenNote: 7.3.3
17 changes: 14 additions & 3 deletions R/Image.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@
#'
#' This function returns a random image that can be passed into `renderImage` and `plotOutput`.
#'
#' @return an image
#' @param width image width passed through to the rendered `<img>` tag.
#' `NULL` (default) leaves the attribute unset (#9).
#' @param height image height, same semantics as `width` (#9).
#' @param alt `alt` attribute for accessibility. `NULL` (default) leaves
#' the attribute unset (#9).
#'
#' @return a list compatible with `shiny::renderImage()`: `src`, plus the
#' `width` / `height` / `alt` attributes when provided.
#'
#' @export

random_image <- function(){
random_image <- function(width = NULL, height = NULL, alt = NULL){
l <- list.files(system.file("img", package = "shinipsum"), full.names = TRUE)
img <- normalizePath(sample(l, 1))
tmpimg <- paste(tempfile(), basename(img), sep = "-")
file.copy(img, tmpimg)
list(src = tmpimg)
out <- list(src = tmpimg)
if (!is.null(width)) out$width <- width
if (!is.null(height)) out$height <- height
if (!is.null(alt)) out$alt <- alt
out
}
39 changes: 37 additions & 2 deletions R/Plot.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
#' Build the categorical labels used by random_ggplot('bar', n_bars=).
#' @noRd
make_bar_labels <- function(n) {
if (n <= 26L) {
LETTERS[seq_len(n)]
} else {
sprintf("Cat%02d", seq_len(n))
}
}

#' A Random ggplot
#'
#' This function returns a ggplot object, which can be passed to `renderPlot` and `plotOutput`
#'
#' @param type type of the geom. Can be any of "random", "point", "bar", "boxplot","col", "tile", "line", "bin2d", "contour", "density", "density_2d", "dotplot", "hex", "freqpoly", "histogram", "ribbon", "raster", "tile", "violin" and defines the geom of the ggplot. Default is "random", and chooses a random geom for you.
#' @param n_bars integer, number of bars to draw when `type == "bar"`. When
#' `NULL` (default), one of the built-in datasets is sampled; otherwise
#' a synthetic data frame with `n_bars` categories is used (#5).
#'
#' @importFrom ggplot2 ggplot aes geom_point geom_bar scale_color_viridis_d theme_minimal geom_boxplot labs coord_flip geom_tile geom_line facet_grid geom_col scale_fill_viridis_c
#' @importFrom ggplot2 xlim ylim geom_bin2d geom_contour geom_density geom_density_2d geom_dotplot
Expand All @@ -18,7 +31,8 @@ random_ggplot <- function(type = c("random", "point", "bar",
"density", "density_2d", "dotplot",
"hex", "freqpoly", "histogram",
"ribbon", "raster", "tile",
"violin")) {
"violin"),
n_bars = NULL) {
type_matched <- match.arg(type)

if (type_matched == "random") {
Expand All @@ -28,6 +42,27 @@ random_ggplot <- function(type = c("random", "point", "bar",
type_matched <- sample( form, 1 )
}

# User asked for a specific number of bars -> short-circuit the
# builtin-dataset switch with synthetic data so we hit exactly n_bars (#5).
if (type_matched == "bar" && !is.null(n_bars)) {
stopifnot(is.numeric(n_bars), length(n_bars) == 1L,
!is.na(n_bars), n_bars >= 1L)
Comment on lines +48 to +49
n_bars <- as.integer(n_bars)
df <- data.frame(
category = factor(make_bar_labels(n_bars),
levels = make_bar_labels(n_bars)),
value = sample.int(100L, n_bars, replace = TRUE)
Comment on lines +47 to +54
)
return(
ggplot2::ggplot(df) +
ggplot2::aes(category, value, fill = category) +
ggplot2::geom_col() +
ggplot2::scale_fill_viridis_d() +
ggplot2::theme_minimal() +
ggplot2::theme(legend.position = "none")
)
}

r <- switch(as.character(type_matched),
"point" = sample(0:5, 1),
"bar" = sample(10:11, 1),
Expand Down Expand Up @@ -144,7 +179,7 @@ random_ggplot <- function(type = c("random", "point", "bar",
"50" = list(
ggplot(datasets::women) +
aes(height, weight) +
geom_line(size = 2) +
geom_line(linewidth = 2) +
theme_minimal()
),
"51" = list(
Expand Down
4 changes: 3 additions & 1 deletion R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ utils::globalVariables(unique(c(
"x", "y",
"depth", "cut", "carat", "price", "color",
"year", "level",
"z", "w"
"z", "w",
# random_ggplot('bar', n_bars=)
"category", "value"

# random dygraphs
# @importFrom datasets mdeaths fdeaths ldeaths nhtemp AirPassengers discoveries presidents austres
Expand Down
17 changes: 17 additions & 0 deletions dev/SUIVI_ISSUES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Suivi — passe `fix/multiple-issues`

| # | Type | Résumé | Fix | Test |
|---|---|---|---|---|
| #13 | bug | `random_ggplot("line")` triggered ggplot2's `size→linewidth` deprecation warning | `geom_line(size = 2)` -> `geom_line(linewidth = 2)` | `tests/testthat/test-no_deprecated_args.R` (build no warning + static sweep) |
| #9 | feat | `random_image()` couldn't propagate `width` / `height` / `alt` | new params `width = NULL, height = NULL, alt = NULL` (kept default `list(src = ...)` shape) | `tests/testthat/test-image_args.R` |
| #5 | feat | no way to control the number of bars in `random_ggplot("bar")` | new `n_bars = NULL` parameter — when set, generates a synthetic categorical data frame so the rendered plot has *exactly* that many bars | `tests/testthat/test-bar_n.R` (1 / 3 / 7 / 27 bars + invalid input) |

## Issues envisagées mais non traitées

| # | Pourquoi pas |
|---|---|
| #4 | "time series option" — design choice (date axis, density, faceting) à arbitrer avec mainteneur. |
| #3 | "other lorem ipsum options" — choix de corpora alternatifs (cat ipsum, hipster ipsum…), demande d'inclure de nouveaux assets. |
| #2 | "Release 0.1.0" — meta. |
| #1 | `mock_frame()` — feature, demande design. |
| #10 | `_R_CHECK_USE_CODETOOLS_=false` — dépend du contexte de test downstream. |
7 changes: 6 additions & 1 deletion man/random_ggplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions man/random_image.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/shinipsum-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions tests/testthat/test-bar_n.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
test_that("random_ggplot('bar', n_bars=) produces exactly n bars (#5)", {
for (n in c(1L, 3L, 7L, 27L)) {
p <- random_ggplot("bar", n_bars = n)
expect_s3_class(p, "ggplot")
# Build the plot and count distinct bars in the rendered data layer.
built <- ggplot2::ggplot_build(p)
bars <- nrow(built$data[[1]])
expect_equal(bars, n,
info = paste0("requested ", n, " bars, got ", bars))
}
})

test_that("random_ggplot('bar') without n_bars keeps the legacy datasets behaviour", {
old_seed <- if (exists(".Random.seed", envir = .GlobalEnv, inherits = FALSE)) {
get(".Random.seed", envir = .GlobalEnv)
} else NULL
on.exit(
if (!is.null(old_seed)) assign(".Random.seed", old_seed, envir = .GlobalEnv)
else if (exists(".Random.seed", envir = .GlobalEnv, inherits = FALSE)) {
rm(list = ".Random.seed", envir = .GlobalEnv)
},
add = TRUE
)
set.seed(1)
p <- random_ggplot("bar")
expect_s3_class(p, "ggplot")
})

test_that("random_ggplot rejects nonsense n_bars (#5)", {
expect_error(random_ggplot("bar", n_bars = -1))
expect_error(random_ggplot("bar", n_bars = "five"))
expect_error(random_ggplot("bar", n_bars = c(3, 4)))
})
20 changes: 20 additions & 0 deletions tests/testthat/test-image_args.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
test_that("random_image() returns just `src` by default (regression)", {
res <- random_image()
expect_type(res, "list")
expect_named(res, "src")
expect_true(file.exists(res$src))
})

test_that("random_image(width=, height=, alt=) propagates the arguments (#9)", {
res <- random_image(width = "100px", height = "80px", alt = "Yo")
expect_named(res, c("src", "width", "height", "alt"))
expect_equal(res$width, "100px")
expect_equal(res$height, "80px")
expect_equal(res$alt, "Yo")
})

test_that("random_image() drops NULL extras (#9)", {
res <- random_image(width = "200px")
expect_named(res, c("src", "width"))
expect_equal(res$width, "200px")
})
Loading
Loading