Skip to content

Commit

Permalink
work around aes_string() deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnMount committed Apr 14, 2023
1 parent aaf0307 commit 8e18212
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Imports:
cdata (>= 1.2.0),
rqdatatable (>= 1.3.1),
rquery (>= 1.4.9),
rlang,
utils,
grid,
gridExtra,
Expand All @@ -39,5 +40,5 @@ Suggests:
plotly,
hexbin,
tinytest
RoxygenNote: 7.1.1
RoxygenNote: 7.2.3
ByteCompile: true
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export(ThresholdPlot)
export(plot_Keras_fit_trajectory)
export(plot_fit_trajectory)
export(plotlyROC)
export(simulate_aes_string)
import(rqdatatable)
import(rquery)
import(wrapr)
Expand Down
4 changes: 2 additions & 2 deletions R/ScatterBoxPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#' WVPlots::ScatterBoxPlot(frm2, "label", "meas", pt_alpha=0.2, title="Example Scatter/Box plot")
#'
#' @export
ScatterBoxPlot = function(frm, xvar, yvar, title, ...,
ScatterBoxPlot <- function(frm, xvar, yvar, title, ...,
pt_alpha=0.3,
pt_color='black',
box_color='black',
Expand All @@ -45,7 +45,7 @@ ScatterBoxPlot = function(frm, xvar, yvar, title, ...,
stop(paste(xvar, "should be discrete (factor, character, integer, or logical)"))
}

ggplot2::ggplot(frm, ggplot2::aes_string(x=xvar, y=yvar, group=xvar)) +
ggplot2::ggplot(data=frm, mapping=ggplot2::aes(!!!simulate_aes_string(x=xvar, y=yvar, group=xvar))) +
ggplot2::geom_boxplot(outlier.size=0, color=box_color, fill=box_fill) +
ggplot2::geom_point(color=pt_color, alpha=pt_alpha, position=ggplot2::position_jitter(width=0.1,height=0)) +
ggplot2::ggtitle(title)
Expand Down
37 changes: 37 additions & 0 deletions R/SimulateAESString.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@


#' Simulate the needlessly deprecated \code{ggplot2::aes_string()}.
#'
#' Use to allow (needlessly) replacing code of the form \code{ggplot2::aes_string(...)}
#' with code of the form \code{ggplot2::aes(!!!simulate_aes_string(...))}.
#' Purpose is to get out of the way of the deprecation and possible future removal of \code{ggplot2::aes_string()}.
#' This will itself break when \code{rlang} again chantes its API and deprecates the affordances used.
#' Inspired by the research of \url{https://stackoverflow.com/a/74424353/6901725}.
#'
#'
#' @param ... named string arguments to turn into symbols using `rlang::data_sym()`.
#' @return some rlang NSE that simulates values at great complexity (but needed for newer ggplot2()).
#'
#' @examples
#'
#' d <- data.frame(x = c(1, 2, 3), y = c(4, 5, 6))
#' xvar <- 'x' # the idea is, this is passed in and not known at coding time
#' yvar <- 'y'
#' # what we want:
#' # ggplot2::ggplot(data = d, mapping = ggplot2::aes_string(x = xvar, y = yvar)) + ggplot2::geom_point()
#' # The required "tidy evaluation ideoms[sic] with `aes()`".
#' ggplot2::ggplot(data = d, mapping = ggplot2::aes(!!!simulate_aes_string(x = xvar, y = yvar))) + ggplot2::geom_point()
#'
#' @export
simulate_aes_string <- function(...) {
# replace the needlessly deprecated aes_string
# from:
# https://stackoverflow.com/a/74424353/6901725
args <- list(...)
for (v in args) {
if (is.character(v)) {
stopifnot(length(v) == 1)
}
}
lapply(args, function(x) {if (is.character(x)) rlang::data_sym(x) else x})
}
32 changes: 32 additions & 0 deletions man/simulate_aes_string.Rd

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

0 comments on commit 8e18212

Please sign in to comment.