Skip to content

Commit

Permalink
change: girafeOutput() argument height now defaults to NULL
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgohel committed May 16, 2024
1 parent 83dbf8b commit 0462f3e
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 89 deletions.
89 changes: 47 additions & 42 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"),
person("Panagiotis", "Skintzos", role = c("aut"),
email = "[email protected]"),
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", , "[email protected]", role = c("aut", "cre")),
person("Panagiotis", "Skintzos", , "[email protected]", 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'
Expand Down
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# 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.

## 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)`.

Expand Down
84 changes: 52 additions & 32 deletions R/girafe.R
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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).
#'
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -144,8 +145,9 @@ girafe <- function(
tryCatch({
if (!is.null(ggobj)) {
print(ggobj)
} else
} else {
code
}
}, finally = {
if (length(dev.list()) > devlength) {
dev.off()
Expand All @@ -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
)

}


Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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")
}
}

24 changes: 13 additions & 11 deletions man/girafe.Rd

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

23 changes: 20 additions & 3 deletions man/girafeOutput.Rd

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

0 comments on commit 0462f3e

Please sign in to comment.