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

before CRAN #17

Merged
merged 29 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0751d7d
before cran
Polkas Nov 18, 2024
70b593f
title case title
Polkas Nov 19, 2024
00bc425
add quote for ggplot2 in DESCRIPTION
Polkas Nov 20, 2024
984510b
cran comments and standalone
Polkas Nov 21, 2024
de6472d
cran comments and standalone
Polkas Nov 21, 2024
b7713ed
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 21, 2024
215a7ad
prefix standalone
Polkas Nov 21, 2024
9e8fdfd
depends
Polkas Nov 21, 2024
b90cf6f
update
Polkas Nov 21, 2024
83e31d0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 21, 2024
e135345
Update README.md
Polkas Nov 21, 2024
b3f0088
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 21, 2024
9f083ff
add import
Polkas Nov 21, 2024
c5a4145
edit docs
Polkas Nov 21, 2024
c34c728
improve
Polkas Nov 21, 2024
2fd4a2f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 21, 2024
9f23561
improve
Polkas Nov 21, 2024
b9207ff
update
Polkas Nov 21, 2024
e6aaf52
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 21, 2024
7ecb7c0
update
Polkas Nov 21, 2024
50fb7a2
dont run styler
Polkas Nov 21, 2024
0037c16
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 21, 2024
5e0eec6
ggcall_example
Polkas Nov 21, 2024
aeb41f2
clean check
Polkas Nov 21, 2024
1a990d4
clean check
Polkas Nov 21, 2024
8444f50
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 21, 2024
8261d71
edit NEWS
Polkas Nov 21, 2024
178193f
edit NEWS
Polkas Nov 21, 2024
54726c4
edit NEWS
Polkas Nov 21, 2024
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
20 changes: 10 additions & 10 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
Package: ggcall
Type: Package
Title: Extract ggplot2 Layers Created within a Function
Version: 0.3.0
Title: Extract 'ggplot2' Layers Created Within a Function
Version: 0.3.3
Authors@R:
person(given = "Maciej",
family = "Nasinski",
role = c("aut", "cre"),
email = "[email protected]")
Maintainer: Maciej Nasinski <[email protected]>
Description:
Enhance ggplot2 with the ability to extract the code used to create a ggplot object, even when it is generated within a function.
This feature aids in understanding, replicating, and modifying complex ggplot2 visualizations produced in functional workflows.
Enhance 'ggplot2' with the ability to extract the code used to create a 'ggplot2' object, even when it is generated within a function.
This feature aids in understanding, replicating, and modifying complex 'ggplot2' visualizations produced in functional workflows.
URL: https://github.com/Polkas/ggcall, https://polkas.github.io/ggcall/
BugReports: https://github.com/Polkas/ggcall/issues
License: Apache License (>= 2)
Encoding: UTF-8
LazyData: true
Depends:
Depends:
ggplot2
Imports:
patchwork
Suggests:
backports,
knitr,
patchwork,
rmarkdown,
usethis,
styler,
testthat (>= 3.0.0)
RoxygenNote: 7.3.2
Config/testthat/edition: 3
VignetteBuilder: knitr
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export(ggcall)
export(ggcall_add_assignments)
export(ggcall_env)
export(ggplot)
importFrom(ggplot2,ggplot)
import(ggplot2, except = c(ggplot))
import(patchwork)
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# ggcall v0.3.3

* Added support for usethis standalone, `usethis::use_standalone` can be used with ggcall.
* Added a reference to `ggcall.example` package, an example implementation of ggcall.

# ggcall v0.3.0

* Added support for ggplot related patchwork operators.
Expand Down
40 changes: 0 additions & 40 deletions R/patchwork.R

This file was deleted.

57 changes: 47 additions & 10 deletions R/ggcall.R → R/standalone-ggcall.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# ---
# repo: polkas/ggcall
# file: ggcall.R
# last-updated: 2024-11-21
# license: https://unlicense.org
# imports: ggplot2
# ---
#
# This file provides a minimal shim to provide a ggcall functionality on top of
# ggplot2.
#
# ## Changelog
#

# nocov start

#' Enhanced `ggplot` Function with History Tracking
#'
#' Overrides the default `ggplot` function from the ggplot2 package, adding the
Expand All @@ -10,8 +26,9 @@
#' attribute 'ggcall' that stores the history of plot construction.
#'
#' @seealso \code{\link[ggplot2]{ggplot}}
#' @importFrom ggplot2 ggplot
#' @rawNamespace import(ggplot2, except = c(ggplot))
#' @examples
#' library(ggplot2, exclude = "ggplot")
#' p <- ggplot(mtcars, aes(x = wt, y = mpg))
#' # the + function has to come from ggcall package
#' attr(p + geom_point(), "ggcall")
Expand All @@ -38,10 +55,12 @@ ggplot <- function(...) {
#' conjunction with the enhanced ggplot function provided by this package.
#'
#' @param e1 A ggplot object of class 'ggcall'.
#' @param e2 A layer or theme to add to the ggplot object.
#' @param e2 A layer, theme or ggcall to add.
#'
#' @return A modified ggplot object with updated plot history.
#' @rdname ggcall-add-operator
#' @examples
#' library(ggplot2, exclude = "ggplot")
#' p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
#' geom_point()
#' attr(p, "ggcall") # View the plot call
Expand Down Expand Up @@ -92,6 +111,7 @@ ggplot <- function(...) {
#' a list representing the history of the ggplot object.
#'
#' @examples
#' library(ggplot2, exclude = "ggplot")
#' # Example: Create a function which combines a few ggplot layers
#' # Typically, it will be a function from your R package where you implemented ggcall
#' func <- function(data, x, y, bool = TRUE) {
Expand All @@ -112,8 +132,12 @@ ggplot <- function(...) {
#' }
#' plot_call <- ggcall(func(mtcars, "wt", "mpg"))
#' # Optionally: Style the code with styler
#' styler::style_text(backports:::deparse1(plot_call))
#'
#' # deparse1 is recommended and available in R>=4.0.0
#' \dontrun{
#' styler::style_text(
#' paste(deparse(plot_call), collapse = "\n")
#' )
#' }
#' @export
#'
ggcall <- function(plot) {
Expand Down Expand Up @@ -143,6 +167,7 @@ ggcall <- function(plot) {
#' More complex variables are referenced to ggcall environment.
#'
#' @examples
#' library(ggplot2, exclude = "ggplot")
#' # Example: Create a function which combines a few ggplot layers
#' # Typically, it will be a function from your R package where you implemented ggcall
#' func <- function(data, x, y, bool = TRUE) {
Expand All @@ -164,14 +189,17 @@ ggcall <- function(plot) {
#' plot_call <- ggcall(func(mtcars, "wt", "mpg"))
#' # Optionally: Add assignments
#' plot_call_with_assignments <- ggcall_add_assignments(plot_call)
#' \dontrun{
#' styler::style_text(
#' paste(deparse(plot_call_with_assignments), collapse = "\n")
#' )
#'
#' }
#' eval_ggcall(plot_call_with_assignments)
#'
#' # Will Fail as data is needed and skipped
#' # eval_ggcall(ggcall_add_assignments(plot_call, vars = c("x", "y")))
#' \dontrun{
#' eval_ggcall(ggcall_add_assignments(plot_call, vars = c("x", "y")))
#' }
#' @export
ggcall_add_assignments <- function(call, vars = extract_names(call)) {
stopifnot(inherits(call, "ggcall_code"))
Expand Down Expand Up @@ -232,9 +260,14 @@ ggcall_add_assignments <- function(call, vars = extract_names(call)) {
#' @return The resulting ggplot object produced by evaluating the expression `x`.
#'
#' @examples
#' p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
#' geom_point()
#' plot_call <- ggcall(p)
#' library(ggplot2, exclude = "ggplot")
#'
#' func <- function() {
#' ggplot(mtcars, aes(x = wt, y = mpg)) +
#' geom_point()
#' }
#' gplot <- func()
#' plot_call <- ggcall(gplot)
#' reconstructed_plot <- eval_ggcall(plot_call)
#' print(reconstructed_plot)
#'
Expand All @@ -259,11 +292,13 @@ eval_ggcall <- function(call, ...) {
#' @param call An expression representing the ggplot construction code.
#' @return The environment in which the ggplot construction code was created.
#' @examples
#' library(ggplot2, exclude = "ggplot")
#' fun <- function(data, x, y) {
#' ggplot(data, aes(x = !!as.name(x), y = !!as.name(y))) +
#' geom_point()
#' }
#' plot_call <- ggcall(fun(mtcars, "wt", "mpg"))
#' gplot <- fun(mtcars, "wt", "mpg")
#' plot_call <- ggcall(gplot)
#' env <- ggcall_env(plot_call)
#' ls(env)
#' env[["data"]]
Expand Down Expand Up @@ -309,3 +344,5 @@ extract_names <- function(expr) {

return(character())
}

# nocov end
103 changes: 103 additions & 0 deletions R/standalone-patchwork.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# ---
# repo: polkas/ggcall
# file: patchwork
# last-updated: 2024-11-21
# license: https://unlicense.org
# dependencies: ggcall.R
# imports: [ggplot2, patchwork]
# ---
#
# This file provides a minimal shim to provide a ggcall functionality on top of
# ggplot2. Additionally patchwork operators are supported.
#
# ## Changelog
#

# nocov start

#' @title Base Function for Patchwork Operators
#' @description A helper function that applies patchwork operators to `ggcall` objects.
#' @param e1 The left-hand side `ggcall` object.
#' @param e2 The right-hand side `ggcall` object.
#' @param operator The operator as a string (e.g., "-", "/", "|", "*", "&").
#' @param class The class to which the operator is applied ("ggplot" or "gg").
#' @return A combined `ggcall` object resulting from the operation.
#' @import patchwork
#' @keywords internal
patch_operator_base <- function(e1, e2, operator, class) {
if (!requireNamespace("patchwork", quietly = TRUE)) {
stop("patchwork package has to be installed.")
}
plot <- utils::getFromNamespace(sprintf("%s.%s", operator, class), "patchwork")(e1, e2)
if (inherits(e1, "ggcall") && inherits(e2, "ggcall")) {
lhs <- ggcall(e1)
rhs <- ggcall(e2)
attr(plot, "ggcall") <- as.call(list(as.name(operator), lhs, rhs))
attr(plot, "ggcall_env") <- merge_env(attr(e1, "ggcall_env"), attr(e2, "ggcall_env"))
class(plot) <- unique(c("ggcall", class(plot)))
}
plot
}

#' @title Minus Operator for ggcall Objects
#' @description Applies the minus operator to `ggcall` objects, utilizing the corresponding operator from the `patchwork` package.
#' @details This function allows for the subtraction of `ggcall` objects using the `patchwork` syntax.
#' @inheritParams patch_operator_base
#' @return A combined `ggcall` object after applying the minus operation.
#' @keywords internal
#' @rdname ggcall-operators
#' @export
"-.ggcall" <- function(e1, e2) {
patch_operator_base(e1, e2, "-", "ggplot")
}

#' @title Division Operator for ggcall Objects
#' @description Applies the division operator to `ggcall` objects, utilizing the corresponding operator from the `patchwork` package.
#' @details This function allows for the division of `ggcall` objects using the `patchwork` syntax.
#' @inheritParams patch_operator_base
#' @return A combined `ggcall` object after applying the division operation.
#' @keywords internal
#' @rdname ggcall-operators
#' @export
"/.ggcall" <- function(e1, e2) {
patch_operator_base(e1, e2, "/", "ggplot")
}

#' @title Or Operator for ggcall Objects
#' @description Applies the or operator to `ggcall` objects, utilizing the corresponding operator from the `patchwork` package.
#' @details This function allows for the combination of `ggcall` objects using the `patchwork` syntax.
#' @inheritParams patch_operator_base
#' @return A combined `ggcall` object after applying the or operation.
#' @keywords internal
#' @rdname ggcall-operators
#' @export
"|.ggcall" <- function(e1, e2) {
patch_operator_base(e1, e2, "|", "ggplot")
}

#' @title Multiplication Operator for ggcall Objects
#' @description Applies the multiplication operator to `ggcall` objects, utilizing the corresponding operator from the `patchwork` package.
#' @details This function allows for the multiplication of `ggcall` objects using the `patchwork` syntax.
#' @inheritParams patch_operator_base
#' @return A combined `ggcall` object after applying the multiplication operation.
#' @keywords internal
#' @rdname ggcall-operators
#' @export
"*.ggcall" <- function(e1, e2) {
patch_operator_base(e1, e2, "*", "gg")
}

#' @title And Operator for ggcall Objects
#' @description Applies the and operator to `ggcall` objects, utilizing the corresponding operator from the `patchwork` package.
#' @details This function allows for the combination of `ggcall` objects using the `patchwork` syntax.
#' @inheritParams patch_operator_base
#' @return A combined `ggcall` object after applying the and operation.
#' @keywords internal
#' @rdname ggcall-operators
#' @export
"&.ggcall" <- function(e1, e2) {
patch_operator_base(e1, e2, "&", "gg")
}


# nocov end
Loading
Loading