# small function to display plots only if it's interactive
p_ <- GGally::print_if_interactive
# example function that prints 'val'
fn <- function(data, mapping, val = 2) {
print(val)
}
fn(data = NULL, mapping = NULL) # 2
# wrap function to change default value 'val' to 5 instead of 2
wrapped_fn1 <- wrap(fn, val = 5)
wrapped_fn1(data = NULL, mapping = NULL) # 5
# you may still supply regular values
wrapped_fn1(data = NULL, mapping = NULL, val = 3) # 3
# wrap function to change 'val' to 5 using the arg list
wrapped_fn2 <- wrap_fn_with_param_arg(fn, params = list(val = 5))
wrapped_fn2(data = NULL, mapping = NULL) # 5
# change parameter settings in ggpairs for a particular function
## Goal output:
regularPlot <- ggally_points(
iris,
ggplot2::aes(Sepal.Length, Sepal.Width),
size = 5, color = "red"
)
p_(regularPlot)
# Wrap ggally_points to have parameter values size = 5 and color = 'red'
w_ggally_points <- wrap(ggally_points, size = 5, color = "red")
wrappedPlot <- w_ggally_points(
iris,
ggplot2::aes(Sepal.Length, Sepal.Width)
)
p_(wrappedPlot)
# Double check the aes parameters are the same for the geom_point layer
identical(regularPlot$layers[[1]]$aes_params, wrappedPlot$layers[[1]]$aes_params)
# Use a wrapped function in ggpairs
pm <- ggpairs(iris, 1:3, lower = list(continuous = wrap(ggally_points, size = 5, color = "red")))
p_(pm)
pm <- ggpairs(iris, 1:3, lower = list(continuous = w_ggally_points))
p_(pm)
Run the code above in your browser using DataLab