Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add $addPlots() method to PlotGridConfiguration #269

Merged
merged 5 commits into from
May 12, 2022
Merged
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
48 changes: 47 additions & 1 deletion R/plot-grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ plotGrid <- function(plotGridConfiguration) {
PlotGridConfiguration <- R6::R6Class(
"PlotGridConfiguration",
public = list(
plotList = NULL,
plotList = list(),
title = NULL,
subtitle = NULL,
caption = NULL,
Expand All @@ -164,6 +164,52 @@ PlotGridConfiguration <- R6::R6Class(
#' @return A `PlotGridConfiguration` object.
initialize = function(plotList = NULL) {
self$plotList <- plotList
},

#' @description Add a plot object.
#'
#' @param plots A single or a list containing `ggplot` object(s).
#'
#' @examples
#'
#' library(ggplot2)
#'
#' myPlotGrid <- PlotGridConfiguration$new()
#'
#' # You can add a single ggplot object
#' p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
#' myPlotGrid$addPlots(p)
#'
#' # Or you can also pass a list
#' myPlotGrid$addPlots(list("p1" = ggplot(), "p2" = ggplot()))
#'
#' # Since we added three plots, the `plotList` field should
#' # now be a list of length `3`
#' length(myPlotGrid$plotList)
#'
#' @return `PlotGridConfiguration` object with `$plotList` field updated to
#' store entered plots.
addPlots = function(plots = NULL) {
if (!is.null(plots)) {
validateIsOfType(plots, "ggplot")

# When only single instance of `ggplot` object is entered:
# `addPlots = ggplot()`
if (objectCount(plots) == 1L) {
# This single object needs to be converted to a list *only if* it
# already hasn't been entered in a list: `addPlots = list(ggplot())`.
#
# `ggplot` objects themselves are lists, but they will always have a
# length greater than 1. That's how we can distinguish between
# `ggplot` object itself as a list and the same object stored in a
# list.
if (is.list(plots) && length(plots) > 1L) {
plots <- list(plots)
}
}

self$plotList <- append(self$plotList, plots)
}
}
)
)
68 changes: 68 additions & 0 deletions man/PlotGridConfiguration.Rd

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

21 changes: 21 additions & 0 deletions tests/testthat/test-plot-grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,24 @@ test_that("plots grid is rendered correctly", {
# fig = plotGrid(plotGridObj)
# )
})

test_that("adding plots works with plots grid configuration", {
myPlotGrid <- PlotGridConfiguration$new()

# adding single plot works
myPlotGrid$addPlots(ggplot())

expect_equal(length(myPlotGrid$plotList), 1L)

# adding a list of plot works
myPlotGrid$addPlots(list("p1" = ggplot(), "p2" = ggplot()))

expect_equal(length(myPlotGrid$plotList), 3L)

# adding a list of a single plot also works
myPlotGrid$addPlots(list("p3" = ggplot()))

expect_equal(length(myPlotGrid$plotList), 4L)

expect_equal(names(myPlotGrid$plotList), c("", "p1", "p2", "p3"))
})