diff --git a/DESCRIPTION b/DESCRIPTION index 4fdc262..30bef21 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,58 +1,63 @@ -Package: ggiraph Type: Package +Package: ggiraph Title: Make 'ggplot2' Graphics Interactive -Description: Create interactive 'ggplot2' graphics using 'htmlwidgets'. -Version: 0.8.10.004 +Version: 0.8.10.005 Authors@R: c( - person("David", "Gohel", role = c("aut", "cre"), - email = "david.gohel@ardata.fr"), - person("Panagiotis", "Skintzos", role = c("aut"), - email = "sigmapi@posteo.net"), - person("Mike", "Bostock", role = c("cph"), - comment = "d3.js"), - person("Speros", "Kokenes", role = c("cph"), - comment = "d3-lasso"), - person("Eric", "Shull", role = c("cph"), - comment = "saveSvgAsPng js library"), - person("Lee", "Thomason", role = c("cph"), - comment = "TinyXML2"), - person("Vladimir", "Agafonkin", role = c("cph"), - comment = "Flatbush"), - person("Eric", "Book", role = c("ctb"), - comment = "hline and vline geoms") - ) + person("David", "Gohel", , "david.gohel@ardata.fr", role = c("aut", "cre")), + person("Panagiotis", "Skintzos", , "sigmapi@posteo.net", role = "aut"), + person("Mike", "Bostock", role = "cph", + comment = "d3.js"), + person("Speros", "Kokenes", role = "cph", + comment = "d3-lasso"), + person("Eric", "Shull", role = "cph", + comment = "saveSvgAsPng js library"), + person("Lee", "Thomason", role = "cph", + comment = "TinyXML2"), + person("Vladimir", "Agafonkin", role = "cph", + comment = "Flatbush"), + person("Eric", "Book", role = "ctb", + comment = "hline and vline geoms") + ) +Description: Create interactive 'ggplot2' graphics using 'htmlwidgets'. License: GPL-3 -Copyright: See file COPYRIGHTS. -Encoding: UTF-8 -SystemRequirements: libpng +URL: https://davidgohel.github.io/ggiraph/ +BugReports: https://github.com/davidgohel/ggiraph/issues Imports: - grid, ggplot2 (>= 3.5.0), - htmlwidgets (>= 1.5), - stats, + cli, + ggplot2 (>= 3.5.0), + grid, htmltools, + htmlwidgets (>= 1.5), + purrr, Rcpp (>= 1.0), + rlang, + stats, systemfonts, - purrr, rlang, uuid, - vctrs, cli -LinkingTo: Rcpp, systemfonts + uuid, + vctrs Suggests: + dplyr, + gdtools (>= 0.3.0), + ggrepel (>= 0.9.1), + hexbin, knitr, - tinytest, - rmarkdown, maps, - hexbin, - shiny, - sf (>= 1.0), - ggrepel (>= 0.9.1), quantreg, - xml2 (>= 1.0), - dplyr, - gdtools (>= 0.3.0) -VignetteBuilder: knitr -URL: https://davidgohel.github.io/ggiraph/ -BugReports: https://github.com/davidgohel/ggiraph/issues -RoxygenNote: 7.3.1 + rmarkdown, + sf (>= 1.0), + shiny, + tinytest, + xml2 (>= 1.0) +LinkingTo: + Rcpp, + systemfonts +VignetteBuilder: + knitr +Copyright: See file COPYRIGHTS. +Encoding: UTF-8 Roxygen: list(markdown = TRUE) +RoxygenNote: 7.3.1 +SystemRequirements: libpng Collate: 'RcppExports.R' 'ipar.R' diff --git a/NEWS.md b/NEWS.md index 6e9ce8c..190d81b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,10 @@ # ggiraph 0.8.10 +## Changes + +- `girafeOutput()` argument `height` now defaults to NULL. Set `height` to +"500px" to recover previous disposition. + ## Issues - fixed size is now defined within style in inches, the size is now as expected. @@ -7,7 +12,6 @@ ## Feature - support trailing commas everywhere -- Update to tinyxml2 10.0.0 - girafe toolbar can now be fixed instead of floating. This feature can be defined with `opts_toolbar(fixed = TRUE)`. diff --git a/R/girafe.R b/R/girafe.R index 317d28f..e17461e 100644 --- a/R/girafe.R +++ b/R/girafe.R @@ -45,16 +45,21 @@ #' library(ggplot2) #' #' dataset <- mtcars -#' dataset$carname = row.names(mtcars) +#' dataset$carname <- row.names(mtcars) #' -#' gg_point = ggplot( data = dataset, -#' mapping = aes(x = wt, y = qsec, color = disp, -#' tooltip = carname, data_id = carname) ) + -#' geom_point_interactive() + theme_minimal() +#' gg_point <- ggplot( +#' data = dataset, +#' mapping = aes( +#' x = wt, y = qsec, color = disp, +#' tooltip = carname, data_id = carname +#' ) +#' ) + +#' geom_point_interactive() + +#' theme_minimal() #' #' x <- girafe(ggobj = gg_point) #' -#' if(interactive()){ +#' if (interactive()) { #' print(x) #' } #' @section Widget options: @@ -76,7 +81,7 @@ #' When a girafe graphic is in a Shiny application, #' graphic will be resized according to the arguments `width` and #' `height` of the function `girafeOutput`. Default -#' values are '100\%' and '500px'. These arguments determine the +#' values are '100\%' and NULL. These arguments determine the #' outer bounding box of the graphic (the HTML element that will #' contain the graphic with an aspect ratio). #' @@ -85,18 +90,14 @@ #' function `girafe`. Its value is beeing used to define a relative #' width of the graphic within its HTML container. Its height is automatically #' adjusted regarding to the argument `width` and the aspect ratio. -#' -#' If this behavior does not fit with your need, I recommend you to use -#' package widgetframe that wraps htmlwidgets inside a responsive iframe. #' @seealso [girafe_options()], [validated_fonts()], [dsvg()] #' @export #' @importFrom uuid UUIDgenerate girafe <- function( - code, ggobj = NULL, pointsize = 12, - width_svg = NULL, height_svg = NULL, - options = list(), dependencies = NULL, ...) { - - path = tempfile() + code, ggobj = NULL, pointsize = 12, + width_svg = NULL, height_svg = NULL, + options = list(), dependencies = NULL, ...) { + path <- tempfile() if (is.null(width_svg)) { width_svg <- default_width(default = 6) @@ -107,7 +108,7 @@ girafe <- function( args <- list(...) - args$canvas_id <- args$canvas_id %||% paste("svg", gsub('-', '_', UUIDgenerate()), sep = "_") + args$canvas_id <- args$canvas_id %||% paste("svg", gsub("-", "_", UUIDgenerate()), sep = "_") args$file <- path args$width <- width_svg args$height <- height_svg @@ -144,8 +145,9 @@ girafe <- function( tryCatch({ if (!is.null(ggobj)) { print(ggobj) - } else + } else { code + } }, finally = { if (length(dev.list()) > devlength) { dev.off() @@ -165,11 +167,10 @@ girafe <- function( unlink(path) createWidget( - name = 'girafe', x = x, package = 'ggiraph', + name = "girafe", x = x, package = "ggiraph", sizingPolicy = sizing_policy, dependencies = dependencies ) - } @@ -180,19 +181,33 @@ girafe <- function( #' @importFrom grDevices dev.off #' @title Create a girafe output element #' @description Render a girafe within an application page. +#' @section Size control: +#' If you want to control a fixed size, use +#' `opts_sizing(rescale = FALSE)` and set the chart size +#' with `girafe(width_svg=..., height_svg=...)`. +#' +#' If you want +#' the graphic to fit the available width, +#' use `opts_sizing(rescale = TRUE)` and set the size of the +#' graphic with `girafe(width_svg=..., height_svg=...)`, this +#' size will define the aspect ratio. #' #' @param outputId output variable to read the girafe from. Do not use special JavaScript #' characters such as a period \code{.} in the id, this would create a JavaScript error. -#' @param width widget width -#' @param height widget height +#' @param width widget width, its default value is set so that the graphic can +#' cover the entire available horizontal space. +#' @param height widget height, its default value is NULL so that width +#' adaptation is not restricted. The height will then be defined according +#' to the width used and the aspect ratio. Only use a value for the height +#' if you have a specific reason and want to strictly control the size. #' @export -girafeOutput <- function(outputId, width = "100%", height = "500px"){ +girafeOutput <- function(outputId, width = "100%", height = NULL) { # if( "auto" %in% height ) # stop("'height:auto' is not supported", call. = FALSE) # if( "auto" %in% width ) # stop("'width:auto' is not supported", call. = FALSE) - shinyWidgetOutput(outputId, 'girafe', package = 'ggiraph', width = width, height = height) + shinyWidgetOutput(outputId, "girafe", package = "ggiraph", width = width, height = height) } #' @title Reactive version of girafe @@ -207,7 +222,9 @@ girafeOutput <- function(outputId, width = "100%", height = "500px"){ #' when `renderGirafe` is used in an interactive R Markdown document. #' @export renderGirafe <- function(expr, env = parent.frame(), quoted = FALSE, outputArgs = list()) { - if (!quoted) { expr <- substitute(expr) } # force quoted + if (!quoted) { + expr <- substitute(expr) + } # force quoted f <- shinyRenderWidget(expr, girafeOutput, env, quoted = TRUE) # shinyRenderWidget is missing outputArgs argument # set outputArgs to the result function instead @@ -217,7 +234,7 @@ renderGirafe <- function(expr, env = parent.frame(), quoted = FALSE, outputArgs f } -girafe_app_paths <- function(){ +girafe_app_paths <- function() { example_dir <- system.file(package = "ggiraph", "examples", "shiny") list.files(example_dir, full.names = TRUE) } @@ -231,18 +248,21 @@ girafe_app_paths <- function(){ #' iris, maps and modal. #' #' @export -run_girafe_example <- function(name = "crimes"){ +run_girafe_example <- function(name = "crimes") { example_dir <- system.file(package = "ggiraph", "examples", "shiny") apps <- girafe_app_paths() - if( !name %in% basename(apps) ){ + if (!name %in% basename(apps)) { abort("could not find app named ", shQuote(name), " in the following list: ", - paste0(shQuote(basename(apps)), collapse = ", "), call = NULL + paste0(shQuote(basename(apps)), collapse = ", "), + call = NULL ) } - if(requireNamespace("shiny")) + if (requireNamespace("shiny")) { shiny::runApp( appDir = file.path(example_dir, name), - display.mode = "showcase") - else warning("package shiny is required to be able to use the function") + display.mode = "showcase" + ) + } else { + warning("package shiny is required to be able to use the function") + } } - diff --git a/man/girafe.Rd b/man/girafe.Rd index 96f7a6f..e94a61f 100644 --- a/man/girafe.Rd +++ b/man/girafe.Rd @@ -87,7 +87,7 @@ always respected when the graph is displayed. When a girafe graphic is in a Shiny application, graphic will be resized according to the arguments \code{width} and \code{height} of the function \code{girafeOutput}. Default -values are '100\\%' and '500px'. These arguments determine the +values are '100\\%' and NULL. These arguments determine the outer bounding box of the graphic (the HTML element that will contain the graphic with an aspect ratio). @@ -96,25 +96,27 @@ document), the graphic will be resized according to the argument \code{width} of function \code{girafe}. Its value is beeing used to define a relative width of the graphic within its HTML container. Its height is automatically adjusted regarding to the argument \code{width} and the aspect ratio. - -If this behavior does not fit with your need, I recommend you to use -package widgetframe that wraps htmlwidgets inside a responsive iframe. } \examples{ library(ggplot2) dataset <- mtcars -dataset$carname = row.names(mtcars) - -gg_point = ggplot( data = dataset, - mapping = aes(x = wt, y = qsec, color = disp, - tooltip = carname, data_id = carname) ) + - geom_point_interactive() + theme_minimal() +dataset$carname <- row.names(mtcars) + +gg_point <- ggplot( + data = dataset, + mapping = aes( + x = wt, y = qsec, color = disp, + tooltip = carname, data_id = carname + ) +) + + geom_point_interactive() + + theme_minimal() x <- girafe(ggobj = gg_point) -if(interactive()){ +if (interactive()) { print(x) } } diff --git a/man/girafeOutput.Rd b/man/girafeOutput.Rd index de6dbf1..4331de1 100644 --- a/man/girafeOutput.Rd +++ b/man/girafeOutput.Rd @@ -4,16 +4,33 @@ \alias{girafeOutput} \title{Create a girafe output element} \usage{ -girafeOutput(outputId, width = "100\%", height = "500px") +girafeOutput(outputId, width = "100\%", height = NULL) } \arguments{ \item{outputId}{output variable to read the girafe from. Do not use special JavaScript characters such as a period \code{.} in the id, this would create a JavaScript error.} -\item{width}{widget width} +\item{width}{widget width, its default value is set so that the graphic can +cover the entire available horizontal space.} -\item{height}{widget height} +\item{height}{widget height, its default value is NULL so that width +adaptation is not restricted. The height will then be defined according +to the width used and the aspect ratio. Only use a value for the height +if you have a specific reason and want to strictly control the size.} } \description{ Render a girafe within an application page. } +\section{Size control}{ + +If you want to control a fixed size, use +\code{opts_sizing(rescale = FALSE)} and set the chart size +with \code{girafe(width_svg=..., height_svg=...)}. + +If you want +the graphic to fit the available width, +use \code{opts_sizing(rescale = TRUE)} and set the size of the +graphic with \code{girafe(width_svg=..., height_svg=...)}, this +size will define the aspect ratio. +} +