Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggiraph plot not appearing in shiny app, but works in RStudio

Tags:

r

shiny

ggiraph

I really want to use ggiraph to add tooltips in my shiny app. However, when I try to do so the plot does not appear. In the example below I used a plotOutput and ggiraphOutput but only my.plot appears. I can produce the interactive plots in RStudio, just not within the app. Is there something wrong with my implementation? Or could there be a compatibility issue (sessionInfo() below)? Thanks!

library(ggiraph)
library(shiny)
library(ggplot2)
ui <- fluidPage(
  fluidRow(
    column(6,plotOutput(outputId ="my.plot")),
    column(6,ggiraphOutput(outputId = "interactive.plot"))
  )
)

server <- function(input, output){
  output$my.plot <- renderPlot({
    data = data.frame(x = 1:10, y= rnorm(10), z = 11:20)
    ggplot(data, aes(x = x, y = y)) + geom_col()

    #girafe(ggobj = xx)
  })
  output$interactive.plot <- renderggiraph({
    data = data.frame(x = 1:10, y= rnorm(10), z = 11:20)
    gg <- ggplot(data, aes(x = 1:10, y = rnorm(10), z = 11:20)) + geom_col_interactive(aes(tooltip = z))
    girafe(ggobj = gg)
  })
}

shinyApp(ui, server)

Here is my sessionInfo()

R version 3.5.3 (2019-03-11)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.5 LTS

Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] gdtools_0.2.1 ggplot2_3.2.1 shiny_1.4.0   dplyr_0.8.4   ggiraph_0.7.0

loaded via a namespace (and not attached):
 [1] tidyselect_1.0.0  remotes_2.1.0     purrr_0.3.3       colorspace_1.4-1  testthat_2.3.1    htmltools_0.4.0   usethis_1.5.1    
 [8] yaml_2.2.1        rlang_0.4.4       pkgbuild_1.0.6    pillar_1.4.3      later_1.0.0       glue_1.3.1        withr_2.1.2      
[15] sessioninfo_1.1.1 uuid_0.1-4        lifecycle_0.1.0   munsell_0.5.0     gtable_0.3.0      devtools_2.2.1    htmlwidgets_1.5.1
[22] memoise_1.1.0     labeling_0.3      callr_3.4.1       fastmap_1.0.1     Cairo_1.5-11      httpuv_1.5.2      ps_1.3.0         
[29] fansi_0.4.1       Rcpp_1.0.3        xtable_1.8-4      scales_1.1.0      backports_1.1.5   promises_1.1.0    desc_1.2.0       
[36] pkgload_1.0.2     jsonlite_1.6.1    farver_2.0.3      mime_0.9          systemfonts_0.1.1 fs_1.3.1          digest_0.6.23    
[43] processx_3.4.2    cowplot_1.0.0     grid_3.5.3        rprojroot_1.3-2   cli_2.0.1         tools_3.5.3       magrittr_1.5     
[50] lazyeval_0.2.2    tibble_2.1.3      crayon_1.3.4      pkgconfig_2.0.3   ellipsis_0.3.0    xml2_1.2.2        prettyunits_1.1.1
[57] assertthat_0.2.1  rstudioapi_0.11   R6_2.4.1          compiler_3.5.3   
like image 893
Sarah W Avatar asked Mar 30 '20 20:03

Sarah W


1 Answers

I'm not 100% sure why, but the problem seems to be with the "." (period) in the outputID name. If you change it to

ggiraphOutput(outputId = "interactivePlot")

and

output$interactivePlot <- renderggiraph({ ... ])

it seems to work just fine. And this does seem to be documented in the ?girafeOutput help page, though not the ?ggiraphOutput help page.

output variable to read the girafe from. Do not use special JavaScript characters such as a period . in the id, this would create a JavaScript error.

It seems they are trying to use girafeOutput and renderGirafe going forward.

like image 136
MrFlick Avatar answered Sep 23 '22 15:09

MrFlick