Skip to content

Commit

Permalink
args for coordinate fun
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Apr 16, 2024
1 parent eb37faf commit e4fe755
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 18 deletions.
3 changes: 2 additions & 1 deletion R/esquisse-server.R
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ esquisse_server <- function(id,
labs = controls_rv$labs,
theme = controls_rv$theme$theme,
theme_args = controls_rv$theme$args,
coord = controls_rv$coord,
coord = controls_rv$coord$fun,
coord_args = controls_rv$coord$args,
facet = aes_r()$facet,
facet_row = aes_r()$facet_row,
facet_col = aes_r()$facet_col,
Expand Down
22 changes: 12 additions & 10 deletions R/ggcall.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@

#' Generate code to create a \code{ggplot2}
#' Generate code to create a `ggplot2`
#'
#' @param data Character. Name of the \code{data.frame}.
#' @param data Character. Name of the `data.frame`.
#' @param mapping List. Named list of aesthetics.
#' @param geom Character. Name of the geom to use (with or without "geom_").
#' @param geom_args List. Arguments to use in the geom.
#' @param scales Character vector. Scale(s) to use (with or without "scale_").
#' @param scales_args List. Arguments to use in scale(s),
#' if \code{scales} is length > 1, must be a named list with \code{scales} names.
#' if `scales` is length > 1, must be a named list with `scales` names.
#' @param coord Character. Coordinates to use (with or without "coord_").
#' @param coord_args Arguments for coordinates function.
#' @param labs List. Named list of labels to use for title, subtitle, x & y axis, legends.
#' @param theme Character. Name of the theme to use (with or without "theme_").
#' @param theme_args Named list. Arguments for \code{\link[ggplot2:theme]{theme}}.
#' @param facet Character vector. Names of variables to use in \code{\link[ggplot2]{facet_wrap}}.
#' @param facet_row Character vector. Names of row variables to use in \code{\link[ggplot2]{facet_grid}}.
#' @param facet_col Character vector. Names of col variables to use in \code{\link[ggplot2]{facet_grid}}.
#' @param facet_args Named list. Arguments for \code{\link[ggplot2:facet_wrap]{facet_wrap}}.
#' @param theme_args Named list. Arguments for [ggplot2::theme()].
#' @param facet Character vector. Names of variables to use in [ggplot2::facet_wrap].
#' @param facet_row Character vector. Names of row variables to use in [ggplot2::facet_grid()].
#' @param facet_col Character vector. Names of col variables to use in [ggplot2::facet_grid()].
#' @param facet_args Named list. Arguments for [ggplot2::facet_wrap()].
#' @param xlim A vector of length 2 representing limits on x-axis.
#' @param ylim A vector of length 2 representing limits on y-axis.
#'
#' @return a \code{call} that can be evaluated with \code{eval}.
#' @return a `call` that can be evaluated with `eval`.
#' @export
#'
#' @importFrom stats setNames
Expand All @@ -34,6 +35,7 @@ ggcall <- function(data = NULL,
scales = NULL,
scales_args = list(),
coord = NULL,
coord_args = list(),
labs = list(),
theme = NULL,
theme_args = list(),
Expand Down Expand Up @@ -103,7 +105,7 @@ ggcall <- function(data = NULL,
if (!is.null(coord)) {
if (!grepl("^coord_", coord))
coord <- paste0("coord_", coord)
coord <- call2(coord)
coord <- call2(coord, !!!coord_args)
ggcall <- expr(!!ggcall + !!coord)
}
if (!is.null(theme)) {
Expand Down
31 changes: 29 additions & 2 deletions R/module-controls-axes.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ controls_axes_ui <- function(id) {
)
),
tags$hr(),
tags$b("Coordinates system:"),
tags$b("Coordinate system:"),
prettyRadioButtons(
inputId = ns("coordinates"),
label = "Coordinates:",
Expand All @@ -72,6 +72,27 @@ controls_axes_ui <- function(id) {
status = "primary",
outline = TRUE,
inline = TRUE
),
conditionalPanel(
condition = "input.coordinates == 'fixed'",
ns = ns,
numericInput(
inputId = ns("fixed_ratio"),
label = "Aspect ratio:",
value = 1,
width = "100%"
)
),
conditionalPanel(
condition = "input.coordinates == 'polar'",
ns = ns,
prettyRadioButtons(
inputId = ns("polar_theta"),
label = "Variable to map angle to:",
choices = c("x", "y"),
inline = TRUE,
width = "100%"
)
)
)
}
Expand Down Expand Up @@ -114,7 +135,13 @@ controls_axes_server <- function(id,
})

coord_r <- reactive(
if (identical(input$coordinates, "cartesian")) NULL else input$coordinates
list(
fun = if (!identical(input$coordinates, "cartesian")) input$coordinates,
args = dropNulls(list(
ratio = if (identical(input$coordinates, "fixed")) input$fixed_ratio,
theta = if (identical(input$coordinates, "polar")) input$polar_theta
))
)
)

limits_r <- reactive({
Expand Down
16 changes: 16 additions & 0 deletions examples/ex-ggcall.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,19 @@ ggcall(
size_continuous = list(range = c(1, 20))
)
)


# Coordinates
ggcall(
data = "mtcars",
mapping = list(x = "mpg", y = "wt"),
geom = "point",
coord = "fixed"
)
ggcall(
data = "mtcars",
mapping = list(x = "mpg", y = "wt"),
geom = "point",
coord = "fixed",
coord_args = list(ratio = 5)
)
29 changes: 24 additions & 5 deletions man/ggcall.Rd

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

0 comments on commit e4fe755

Please sign in to comment.