diff --git a/R/shiny.R b/R/shiny.R new file mode 100644 index 0000000..f368030 --- /dev/null +++ b/R/shiny.R @@ -0,0 +1,24 @@ + +#' Shiny bindings for uPlot +#' +#' Output and render functions for using uPlot within Shiny +#' applications and interactive Rmd documents. +#' +#' @inheritParams htmlwidgets::shinyWidgetOutput +#' +#' @name uPlot-shiny +#' +#' @importFrom htmlwidgets shinyWidgetOutput shinyRenderWidget +#' +#' @export +uPlotOutput <- function(outputId, width = "100%", height = "400px") { + htmlwidgets::shinyWidgetOutput(outputId, "uPlot", width, height, package = "uPlot") +} + +#' @inheritParams htmlwidgets::shinyRenderWidget +#' @rdname uPlot-shiny +#' @export +renderUPlot <- function(expr, env = parent.frame(), quoted = FALSE) { + if (!quoted) { expr <- substitute(expr) } # force quoted + htmlwidgets::shinyRenderWidget(expr, uPlotOutput, env, quoted = TRUE) +} diff --git a/R/uBands.R b/R/uBands.R index 760ff6e..1628ca9 100644 --- a/R/uBands.R +++ b/R/uBands.R @@ -10,7 +10,7 @@ #' @return An `htmlwidget` object of class `"uPlot"`. #' @export #' -#' @examples +#' @example examples/ex-uBands.R uBands <- function(uplot, lower, upper, fill, ...) { check_uplot(uplot) stopifnot( diff --git a/R/uPlot.R b/R/uPlot.R index 0d5fe1b..9780a63 100644 --- a/R/uPlot.R +++ b/R/uPlot.R @@ -33,11 +33,11 @@ #' ) #' ) uPlot <- function(data, - options = list(), + options = list(), ..., use_gzipped_json = FALSE, - width = NULL, - height = NULL, + width = NULL, + height = NULL, elementId = NULL) { options <- as.list(options) @@ -48,7 +48,10 @@ uPlot <- function(data, } if (is.null(options$series)) { strokes <- rep_len(palette(), length(series_nms)) - options$series <- prepare_options_series(label = series_nms, stroke = strokes) + options$series <- prepare_options_series( + label = series_nms, + stroke = strokes + ) } if (is.null(options$bands)) { options$bands <- list() @@ -87,32 +90,3 @@ uPlot <- function(data, ) } -#' Shiny bindings for uPlot -#' -#' Output and render functions for using uPlot within Shiny -#' applications and interactive Rmd documents. -#' -#' @param outputId output variable to read from -#' @param width,height Must be a valid CSS unit (like \code{'100\%'}, -#' \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a -#' string and have \code{'px'} appended. -#' @param expr An expression that generates a uPlot -#' @param env The environment in which to evaluate \code{expr}. -#' @param quoted Is \code{expr} a quoted expression (with \code{quote()})? This -#' is useful if you want to save an expression in a variable. -#' -#' @name uPlot-shiny -#' -#' @importFrom htmlwidgets shinyWidgetOutput shinyRenderWidget -#' -#' @export -uPlotOutput <- function(outputId, width = '100%', height = '400px'){ - htmlwidgets::shinyWidgetOutput(outputId, 'uPlot', width, height, package = 'uPlot') -} - -#' @rdname uPlot-shiny -#' @export -renderUPlot <- function(expr, env = parent.frame(), quoted = FALSE) { - if (!quoted) { expr <- substitute(expr) } # force quoted - htmlwidgets::shinyRenderWidget(expr, uPlotOutput, env, quoted = TRUE) -} diff --git a/examples/scatter.R b/examples/scatter.R index 9fd7913..b3e53c2 100644 --- a/examples/scatter.R +++ b/examples/scatter.R @@ -44,8 +44,8 @@ uPlot::uPlot( ), series = list( list(), - list(stroke = "#DF010180", paths = htmlwidgets::JS("drawPoints")) , - list(stroke = "#0431B480", paths = htmlwidgets::JS("drawPoints")) + list(label = "A", stroke = "#DF010180", paths = htmlwidgets::JS("drawPoints")) , + list(label = "B", stroke = "#0431B480", paths = htmlwidgets::JS("drawPoints")) ) ) ) diff --git a/man/uBands.Rd b/man/uBands.Rd index 3da359c..9e7278d 100644 --- a/man/uBands.Rd +++ b/man/uBands.Rd @@ -23,3 +23,15 @@ An \code{htmlwidget} object of class \code{"uPlot"}. \description{ Draw bands between series } +\examples{ + +# Ribbon between 2 series (always high > low) +uPlot(temperatures[, c("date", "low", "high")]) \%>\% + uBands("low", "high", fill = "#4242424D") + +# Ribbon between 2 intersecting series +uPlot(temperatures[, c("date", "temperature", "average")]) \%>\% + uBands("temperature", "average", fill = "#F68180") \%>\% + uBands("average", "temperature", fill = "#2F64FF") + +} diff --git a/man/uPlot-shiny.Rd b/man/uPlot-shiny.Rd index 3b816b3..e63fdbd 100644 --- a/man/uPlot-shiny.Rd +++ b/man/uPlot-shiny.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/uPlot.R +% Please edit documentation in R/shiny.R \name{uPlot-shiny} \alias{uPlot-shiny} \alias{uPlotOutput} @@ -13,11 +13,12 @@ renderUPlot(expr, env = parent.frame(), quoted = FALSE) \arguments{ \item{outputId}{output variable to read from} -\item{width, height}{Must be a valid CSS unit (like \code{'100\%'}, -\code{'400px'}, \code{'auto'}) or a number, which will be coerced to a -string and have \code{'px'} appended.} +\item{width, height}{Must be a valid CSS unit (like \code{"100\%"}, +\code{"400px"}, \code{"auto"}) or a number, which will be coerced to a +string and have \code{"px"} appended.} -\item{expr}{An expression that generates a uPlot} +\item{expr}{An expression that generates an HTML widget (or a +\href{https://rstudio.github.io/promises/}{promise} of an HTML widget).} \item{env}{The environment in which to evaluate \code{expr}.}