davidgohel / ggiraph

make 'ggplot' graphics interactive

Home Page:https://davidgohel.github.io/ggiraph

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

girafe and piping

neuwirthe opened this issue · comments

The following code has issues when output from ggplot expression is piped into girafe call within an function


library(tidyverse)
library(ggiraph)

## function takes a ggobj and adds girafe enhancements
make_chart_html <- function(chart_in, height_factor = 1) {
  height_svg <- 3.6 * height_factor
  width_svg <- 6
  sizing_width <- 0.9
  print(class(chart_in))
  girafe(
    ggobj = chart_in, 
    width_svg = width_svg, 
    height_svg = height_svg * height_factor
  ) -> ggobj_out
  ggobj_out
}

# data for chart
chart_df <-
  tibble(x=seq(0,1,by=0.1),
         y=x^2)

# this works 
# object assigned to a global variable, glibal variable use as explicit argument
chart_df |>
ggplot(aes(x,y)) +
  geom_line() ->
  chart
  make_chart_html(chart)
  
# this works
# object assigned to a global variable, global object piped into function
chart_df |>
ggplot(aes(x,y)) +
  geom_line() ->
  chart
chart |>
  make_chart_html()


# this does not work
# object within make_chart_html function does not have class ggobj
# which is needed by girafe
chart_df |>
ggplot(aes(x,y)) +
  geom_line() |>
  make_chart_html()

output from last expression

[1] "LayerInstance" "Layer"         "ggproto"       "gg"           
Error:
! `ggobj` must be a ggplot2 plot
Backtrace:
 1. global make_chart_html(geom_line())
 2. ggiraph::girafe(...)

sessionInfo()

R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.2.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Vienna
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ggiraph_0.8.8   lubridate_1.9.3 forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4     purrr_1.0.2    
 [7] readr_2.1.4     tidyr_1.3.0     tibble_3.2.1    ggplot2_3.4.4   tidyverse_2.0.0

loaded via a namespace (and not attached):
 [1] utf8_1.2.4        generics_0.1.3    blogdown_1.18     stringi_1.8.3     hms_1.1.3        
 [6] digest_0.6.33     magrittr_2.0.3    evaluate_0.23     grid_4.3.2        timechange_0.2.0 
[11] fastmap_1.1.1     jsonlite_1.8.8    fansi_1.0.6       scales_1.3.0      cli_3.6.2        
[16] rlang_1.1.2       ellipsis_0.3.2    munsell_0.5.0     withr_2.5.2       yaml_2.3.8       
[21] tools_4.3.2       tzdb_0.4.0        uuid_1.1-1        colorspace_2.1-0  vctrs_0.6.5      
[26] R6_2.5.1          lifecycle_1.0.4   htmlwidgets_1.6.4 pkgconfig_2.0.3   pillar_1.9.0     
[31] gtable_0.3.4      glue_1.6.2        Rcpp_1.0.11       systemfonts_1.0.5 xfun_0.41        
[36] tidyselect_1.2.0  rstudioapi_0.15.0 knitr_1.45        farver_2.1.1      htmltools_0.5.7  
[41] rmarkdown_2.25    labeling_0.4.3    compiler_4.3.2   

It's not related to 'ggiraph' but to piping, take a look at the modified function make_chart_html() where we don't use girafe() but a simple instruction to save the plot, error is the same kind.

make_chart_html <- function(chart_in, height_factor = 1) {
  ggsave(tempfile(fileext = ".png"), plot = chart_in)
}

Another possible solution is:

(chart_df |> ggplot(aes(x,y)) + geom_line()) |> 
  make_chart_html()