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

POC: Alternative way of determining parameters #6101

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Sep 12, 2024

This PR explores to fix #1516.

It follows the approach proposed in #1516 (comment).
I've numbered the comments in the code below so it'd be easier to follow.

This PR is currently only implemented for 1 stat and 2 geoms, complying with 'a minimal version' of the approach.
It is for this reason still a POC, and we still have to decide if we like this approach.

Comment on lines +66 to +67
default_params = NULL,

Copy link
Collaborator Author

@teunbrand teunbrand Sep 12, 2024

Choose a reason for hiding this comment

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

  1. The Geom and Stat classes have a new field that can hold a named list of default parameters.

Comment on lines +214 to +216
if (!is.null(self$default_params)) {
return(names(self$default_params))
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. We simply return the name of the default parameter list for checking layer() input. For backward compatibility, if this list isn't present, we resort to the old way of determining input.

Comment on lines +433 to +434
params <- defaults(c(self$geom_params, self$aes_params), self$geom$default_params)
self$computed_geom_params <- self$geom$setup_params(data, params)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. Before we add computed components to the parameters, we initialise the parameters from the new field

@@ -79,7 +81,7 @@ Geom <- ggproto("Geom",
}

# Trim off extra parameters
params <- params[intersect(names(params), self$parameters())]
params <- filter_args(params, self$draw_panel)
Copy link
Collaborator Author

@teunbrand teunbrand Sep 12, 2024

Choose a reason for hiding this comment

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

  1. We then filter the parameters on the go in the compute/draw_layer() and compute/draw_panel() methods. Implementation of filter_args() in the utils file.

Comment on lines +76 to +77
default_params = NULL,

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. We take the same approach with Stats

Comment on lines +184 to +188
default_params = list(
na.rm = FALSE, orientation = NA,
fun.data = NULL, fun = NULL, fun.max = NULL, fun.min = NULL,
fun.args = list()
),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. As an example stat, here is how we could implement the default parameters for StatSummary

Comment on lines +181 to +187
default_params = list(
na.rm = FALSE, width = NULL, orientation = NA, outliers = TRUE,
lineend = "butt", linejoin = "mitre", fatten = 2, outlier.colour = NULL,
outlier.fill = NULL, outlier.shape = NULL, outlier.size = NULL,
outlier.stroke = 0.5, outlier.alpha = NULL, notch = FALSE, notchwidth = 0.5,
staplewidth = 0, varwidth = FALSE, flipped_aes = FALSE
),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. As a geom example, here is how we could implement the default parameter list for GeomBoxplot

}

ggname("geom_violin", grobTree(
GeomPolygon$draw_panel(newdata, ...),
inject(GeomPolygon$draw_panel(newdata, !!!params)),
Copy link
Collaborator Author

@teunbrand teunbrand Sep 12, 2024

Choose a reason for hiding this comment

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

  1. Here is a downside of the method: we cannot simply pass dots here as GeomViolin recieves more parameters than GeomPolygon needs. This was the only Geom where this was a problem though.

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.

Better way of handling parameters
1 participant