From e041f0f8ca93d7ff0559f756e4c1ab2c2ed48492 Mon Sep 17 00:00:00 2001 From: pvictor Date: Mon, 23 Sep 2024 07:18:27 +0200 Subject: [PATCH] fixed double legend in scale_gradient + example --- R/scales.R | 16 +++++----------- R/shiny.R | 2 ++ examples/scale_gradient.R | 15 +++++++++++++++ man/scale-gradient.Rd | 17 +++++++++++++++++ man/vchart-shiny.Rd | 3 +++ 5 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 examples/scale_gradient.R diff --git a/R/scales.R b/R/scales.R index 0dc82f3..38ac3fc 100644 --- a/R/scales.R +++ b/R/scales.R @@ -873,7 +873,7 @@ v_scale_fill_discrete <- v_scale_color_discrete #' #' @name scale-gradient #' -# @examples +#' @example examples/scale_gradient.R v_scale_colour_gradient <- function(vc, name = NULL, low = "#132B43", @@ -929,11 +929,7 @@ v_scale_gradient <- function(vc, "'vc' must be a 'vchart' htmlwidget object" = inherits(vc, "vchart") ) - if (!is.null(vc$x$mapdata[[aesthetic]])) { - x <- vc$x$mapdata[[aesthetic]] - } else { - x <- unlist(lapply(vc$x$mapdata, `[[`, aesthetic)) - } + x <- get_aes_data(extract_data(vc), c(aesthetic)) if (is.null(limits)) limits <- range(pretty(range(x, na.rm = TRUE))) @@ -955,6 +951,8 @@ v_scale_gradient <- function(vc, range = c(low, high) ) ) + i <- vapply(vc$x$specs$legends, function(x) identical(x$type, "color"), logical(1)) + vc$x$specs$legends[i] <- NULL vc <- .vchart_specs( vc, "legends", list(dropNulls(list( @@ -1001,11 +999,7 @@ v_scale_size <- function(vc, "'vc' must be a 'vchart' htmlwidget object" = inherits(vc, "vchart") ) - if (!is.null(vc$x$mapdata$size)) { - x <- vc$x$mapdata$size - } else { - x <- unlist(lapply(vc$x$mapdata, `[[`, "size")) - } + x <- get_aes_data(extract_data(vc), "size") title <- if (is.character(name) & length(name) == 1) { diff --git a/R/shiny.R b/R/shiny.R index 9f7f1e5..f3e0948 100644 --- a/R/shiny.R +++ b/R/shiny.R @@ -12,6 +12,8 @@ #' @export #' #' @importFrom htmlwidgets shinyWidgetOutput +#' +#' @return An output or render function that enables the use of the widget within Shiny applications. #' #' @example examples/shiny.R vchartOutput <- function(outputId, width = "100%", height = "400px"){ diff --git a/examples/scale_gradient.R b/examples/scale_gradient.R new file mode 100644 index 0000000..982879c --- /dev/null +++ b/examples/scale_gradient.R @@ -0,0 +1,15 @@ + +library(vchartr) +data("penguins", package = "palmerpenguins") + +vchart(penguins) %>% + v_scatter(aes( + x = bill_length_mm, + y = bill_depth_mm, + color = body_mass_g + )) %>% + v_scale_colour_gradient( + name = "Body mass", + low = "yellow", + high = "red" + ) diff --git a/man/scale-gradient.Rd b/man/scale-gradient.Rd index 16badc3..7aebe8d 100644 --- a/man/scale-gradient.Rd +++ b/man/scale-gradient.Rd @@ -45,3 +45,20 @@ A \code{\link[=vchart]{vchart()}} \code{htmlwidget} object. \description{ Color scale for continuous data } +\examples{ + +library(vchartr) +data("penguins", package = "palmerpenguins") + +vchart(penguins) \%>\% + v_scatter(aes( + x = bill_length_mm, + y = bill_depth_mm, + color = body_mass_g + )) \%>\% + v_scale_colour_gradient( + name = "Body mass", + low = "yellow", + high = "red" + ) +} diff --git a/man/vchart-shiny.Rd b/man/vchart-shiny.Rd index 4e99d0b..022572d 100644 --- a/man/vchart-shiny.Rd +++ b/man/vchart-shiny.Rd @@ -25,6 +25,9 @@ string and have \code{"px"} appended.} \item{quoted}{Is \code{expr} a quoted expression (with \code{quote()})? This is useful if you want to save an expression in a variable.} } +\value{ +An output or render function that enables the use of the widget within Shiny applications. +} \description{ Output and render functions for using \code{\link[=vchart]{vchart()}} within Shiny applications and interactive Rmd documents.