wilkox / treemapify

🌳 Draw treemaps in ggplot2
http://wilkox.org/treemapify
213 stars 18 forks source link

Treemap text piled up in center in shinyproxy, same code works fine locally on win10 #41

Closed D3SL closed 3 years ago

D3SL commented 4 years ago

I use treemapify for a small webapp I put together as an internal tool. It works just fine when I run it locally (windows 10 pro) in vscode, rstudio, a browser, or as an internal window to the IDE. When I deploy the exact same code to shinyproxy however I get the below output where all the text just piles up in the center.

Could this be caused by a dependency or system library I've missed in the dockerfile?

this glitch

wilkox commented 4 years ago

Hey @D3SL, probably the first thing to check would be that the R and package versions are the same in the ShinyProxy environment and your development environment. Are you able to run 'sessionInfo()' in both and compare?

D3SL commented 4 years ago

Hi @wilkox . I'm using 2.5.3 installed off CRAN in both cases.

wilkox commented 4 years ago

Can you try a couple of things to help me debug this:

  1. Are you able to run sessionInfo() in both environments and post the full output? There could be some differences in the package dependencies too.
  2. Add grid::current.viewport() to your code just above the ggplot() call and see if this changes the appearance of the plot.

Thanks!

D3SL commented 4 years ago

Attempting to put grid::current.viewport() into my renderPlot function before the pipeline that feeds into ggplot() breaks the app with an unexpected symbol error.

Session info from the docker container:

R version 4.0.0 (2020-04-24)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

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

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

other attached packages:
 [1] sf_0.9-4                 readwritesqlite_0.1.1    here_0.1
 [4] dbplyr_1.4.4             pool_0.1.4.3             odbc_1.2.3
 [7] DBI_1.1.0                RSQLite_2.2.0            formattable_0.2.0.1
[10] XLConnect_1.0.1          shinyWidgets_0.5.3       DT_0.14
[13] shinydashboardPlus_0.7.0 shinydashboard_0.7.1     ggfittext_0.9.0
[16] treemapify_2.5.3         magrittr_1.5             lubridate_1.7.9
[19] forcats_0.5.0            stringr_1.4.0            dplyr_1.0.0
[22] purrr_0.3.4              readr_1.3.1              tidyr_1.1.0
[25] tibble_3.0.1             ggplot2_3.3.2            tidyverse_1.3.0
[28] shiny_1.5.0

loaded via a namespace (and not attached):
 [1] nlme_3.1-147       fs_1.4.2           bit64_0.9-7        httr_1.4.1
 [5] rprojroot_1.3-2    tools_4.0.0        backports_1.1.8    R6_2.4.1
 [9] KernSmooth_2.23-17 colorspace_1.4-1   withr_2.2.0        tidyselect_1.1.0
[13] bit_1.1-15.2       compiler_4.0.0     cli_2.0.2          rvest_0.3.5
[17] xml2_1.3.2         scales_1.1.1       classInt_0.4-3     digest_0.6.25
[21] rmarkdown_2.3      pkgconfig_2.0.3    htmltools_0.5.0    fastmap_1.0.1
[25] htmlwidgets_1.5.1  rlang_0.4.6        readxl_1.3.1       rstudioapi_0.11
[29] generics_0.0.2     jsonlite_1.7.0     crosstalk_1.1.0.1  Rcpp_1.0.4.6
[33] munsell_0.5.0      fansi_0.4.1        lifecycle_0.2.0    yaml_2.2.1
[37] stringi_1.4.6      chk_0.5.0          grid_4.0.0         blob_1.2.1
[41] promises_1.1.1     crayon_1.3.4       lattice_0.20-41    haven_2.3.1
[45] hms_0.5.3          knitr_1.29         pillar_1.4.4       reprex_0.3.0
[49] glue_1.4.1         evaluate_0.14      modelr_0.1.8       vctrs_0.3.1
[53] httpuv_1.5.4       cellranger_1.1.0   gtable_0.3.0       assertthat_0.2.1
[57] xfun_0.15          mime_0.9           xtable_1.8-4       broom_0.5.6
[61] e1071_1.7-3        later_1.1.0.1      class_7.3-17       rJava_0.9-12
[65] memoise_1.1.0      units_0.6-7        ellipsis_0.3.1

And from my local environment:

R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_World.1252  LC_CTYPE=English_World.1252   
[3] LC_MONETARY=English_World.1252 LC_NUMERIC=C                  
[5] LC_TIME=English_World.1252    

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

other attached packages:
 [1] glue_1.4.1               clipr_0.7.0              sf_0.9-3                
 [4] readwritesqlite_0.1.1    here_0.1                 dbplyr_1.4.3            
 [7] pool_0.1.4.3             odbc_1.2.2               DBI_1.1.0               
[10] RSQLite_2.2.0            formattable_0.2.0.1      XLConnect_1.0.1         
[13] shinyWidgets_0.5.2       DT_0.13                  shinydashboardPlus_0.7.0
[16] shinydashboard_0.7.1     ggfittext_0.8.1          treemapify_2.5.3        
[19] magrittr_1.5             lubridate_1.7.8          forcats_0.5.0           
[22] stringr_1.4.0            dplyr_0.8.5              purrr_0.3.4             
[25] readr_1.3.1              tidyr_1.0.3              tibble_3.0.1            
[28] ggplot2_3.3.0            tidyverse_1.3.0          shiny_1.4.0.2           

loaded via a namespace (and not attached):
 [1] nlme_3.1-147       fs_1.4.1           bit64_0.9-7        httr_1.4.1        
 [5] rprojroot_1.3-2    tools_4.0.0        backports_1.1.7    R6_2.4.1          
 [9] KernSmooth_2.23-16 colorspace_1.4-1   withr_2.2.0        tidyselect_1.1.0  
[13] bit_1.1-15.2       compiler_4.0.0     cli_2.0.2          rvest_0.3.5       
[17] xml2_1.3.2         scales_1.1.1       classInt_0.4-3     digest_0.6.25     
[21] rmarkdown_2.1      pkgconfig_2.0.3    htmltools_0.4.0    fastmap_1.0.1     
[25] htmlwidgets_1.5.1  rlang_0.4.6        readxl_1.3.1       rstudioapi_0.11   
[29] farver_2.0.3       generics_0.0.2     jsonlite_1.6.1     crosstalk_1.1.0.1 
[33] Rcpp_1.0.4.6       munsell_0.5.0      fansi_0.4.1        lifecycle_0.2.0   
[37] yaml_2.2.1         stringi_1.4.6      chk_0.5.0          grid_4.0.0        
[41] blob_1.2.1         promises_1.1.0     crayon_1.3.4       lattice_0.20-41   
[45] haven_2.2.0        hms_0.5.3          knitr_1.28         pillar_1.4.4      
[49] reprex_0.3.0       evaluate_0.14      modelr_0.1.7       vctrs_0.3.0       
[53] httpuv_1.5.2       cellranger_1.1.0   gtable_0.3.0       assertthat_0.2.1  
[57] xfun_0.13          mime_0.9           xtable_1.8-4       broom_0.5.6       
[61] e1071_1.7-3        later_1.0.0        class_7.3-16       rJava_0.9-12      
[65] memoise_1.1.0      units_0.6-6        ellipsis_0.3.1 
wilkox commented 4 years ago

I'm a bit stumped by this one. Are you able to provide a reprex that reproduces this problem so I can do some testing in ShinyProxy? Thanks.

D3SL commented 4 years ago

I can do you one better than that. I'm doing all of this inside docker, you can reproduce everything down to the OS and libraries I have with this dockerfile:

FROM rocker/r-ubuntu:20.04

# system and R libraries 
RUN apt-get update && apt-get install -y  --no-install-recommends \
gdal-bin \
git-core \
libcairo2-dev \
libcurl4-openssl-dev \
libgdal-dev \
libgeos-dev \
libgeos++-dev \
libgit2-dev \
libssh2-1-dev \
libssl-dev \
libudunits2-dev \
libxml2-dev \
make \
pandoc \
pandoc-citeproc \
unixodbc-dev \
zlib1g-dev \
openjdk-8-jre-headless \
r-cran-rjava \
r-cran-shiny \
r-cran-glue \
r-cran-httr \
r-cran-jsonlite \
r-cran-data.table \
r-cran-lubridate \
r-cran-magrittr \
r-cran-shinydashboard \
r-cran-shinywidgets \
r-cran-shinyfiles \
r-cran-shinyjs \
r-cran-rsqlite \
r-cran-tidyverse \
r-cran-future \
r-cran-furrr \
r-cran-dt \
r-cran-ggfittext \
r-cran-formattable \
r-cran-odbc \
r-cran-pool \
r-cran-clipr \
r-cran-leaflet \
r-cran-styler \
r-cran-devtools \
r-cran-sf \
r-cran-geojsonsf \
r-cran-leafpop \
r-cran-lwgeom \
r-cran-satellite \
r-cran-leafsync \
r-cran-spatstat \
r-cran-spatialwidget \
&& rm -rf /var/lib/apt/lists/*

RUN R CMD javareconf

# R packages
RUN R -e "install.packages(c('treemapify','shinydashboardPlus','here','readwritesqlite','XLConnect'), repos = c('CRAN' = 'https://cran.rstudio.com/'), pkgtype='both', Ncpus=4, dependencies=TRUE)"

ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk-amd64

# copy the app to the image
RUN mkdir /root/my_app
COPY my_app /root/my_app
COPY Rprofile.site /usr/lib/R/etc/

EXPOSE 3838

CMD ["R", "-e", "shiny::runApp('/root/my_app',host='0.0.0.0', port=3838)"]
wilkox commented 4 years ago

I'm going to try to get this up and running – in the meantime, can I ask if you use the place argument in any of the treemapify text geoms? If so, what happens if you remove this argument?

D3SL commented 4 years ago

I'm sorry I just disappeared for over a month, I'm still learning how github works and just simply lost track of this. After updating R to 4.0.2 (ggfittext_0.9.0, treemapify_2.5.3) I get the same piled-in-the-middle effect on my local development copy of the app as well. Deleting place = "center" fixes it.

Originally on my local copy place = "center" moved labels from the top left of their respective box to the middle of the box, which left me confused as to what was going on with the shinyproxy copy of the app. I had assumed that the remote and shinyproxy copy of the app were using the same versions of the app but it's possible they weren't and that accounted for different behavior.

Either way it looks like place = "center" is using the entire treemap diagram as a reference instead of the appropriate sub-layer. Please let me know if there's any kind of logs or dev output I can offer to help narrow this down.

wilkox commented 3 years ago

Can you post the code you're running in your local development copy that produces the error? I'm afraid I still don't have any good ideas about what's causing this.

D3SL commented 3 years ago

Sure thing, I probably should've posted this right up front. This is the current code I'm using which works fine and has primary labels in the top left of each box with subgroup labels at the bottom center.

Originally I had place = "center" as an argument for geom_treemap_text which put the primary labels in the middle of their respective boxes when run locally on my win10 machine and piled up in the overall middle of the entire treemap when deployed to shinyproxy. After updating to R 4.0.2 my win10 machine would also render all the labels piled up in the center.

It's pretty clear geom_treemap_text is somehow losing track of which "center" it's supposed to be aiming for. Instead of placing labels in the center of the treemap's various boxes it places all text in the overall center of the treemap. Let me know if there's any sessioninfo, debug logs, anything I can provide that'll help. Just do me a favor and tag me by name so github pings me and I don't lose track of how to get here, I'm still learning this site/system.

    output$treemap<-renderPlot(

      reactivecontainer$data %>%
        count(status,type) %>%
        filter_all(all_vars(!grepl('Closed',.))) %>%
        replace(is.na(.),"NEW") %>%
        ggplot(.,
        aes(area = n, fill=status, subgroup=type, label = as.character(status))) +
        geom_treemap(show.legend = FALSE) +
        geom_treemap_subgroup_border(show.legend = FALSE) +
        geom_treemap_subgroup_text( grow = FALSE, alpha = 0.5, colour = "black",
          fontface = "italic", reflow = TRUE) +
        geom_treemap_text(fontface = "italic", colour = "white",
          reflow = TRUE ,grow = FALSE)
    )
wilkox commented 3 years ago

@D3SL I've tried to reproduce this (using the G20 dataset instead of yours, which I don't have access to), but still can't. See the reprex below – could you run the exact code shown below and post the result as a reprex?

library(ggplot2)
#> Warning: replacing previous import 'vctrs::data_frame' by 'tibble::data_frame'
#> when loading 'dplyr'
library(treemapify)
sessionInfo()
#> R version 4.0.2 (2020-06-22)
#> Platform: x86_64-apple-darwin19.5.0 (64-bit)
#> Running under: macOS Catalina 10.15.6
#> 
#> Matrix products: default
#> BLAS/LAPACK: /usr/local/Cellar/openblas/0.3.10_1/lib/libopenblasp-r0.3.10.dylib
#> 
#> locale:
#> [1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] treemapify_2.5.3.9003 ggplot2_3.3.2         nvimcom_0.9-102      
#> 
#> loaded via a namespace (and not attached):
#>  [1] knitr_1.29           magrittr_1.5         tidyselect_1.1.0    
#>  [4] munsell_0.5.0        colorspace_1.4-1     R6_2.4.1            
#>  [7] rlang_0.4.7          dplyr_1.0.1          stringr_1.4.0       
#> [10] highr_0.8            tools_4.0.2          grid_4.0.2          
#> [13] gtable_0.3.0         xfun_0.17            withr_2.2.0         
#> [16] htmltools_0.5.0      ellipsis_0.3.1       yaml_2.2.1          
#> [19] digest_0.6.25        tibble_3.0.3         lifecycle_0.2.0     
#> [22] crayon_1.3.4         purrr_0.3.4          vctrs_0.3.4         
#> [25] ggfittext_0.9.0.9001 glue_1.4.2           evaluate_0.14       
#> [28] rmarkdown_2.3        stringi_1.5.3        compiler_4.0.2      
#> [31] pillar_1.4.6         generics_0.0.2       scales_1.1.1        
#> [34] pkgconfig_2.0.3

ggplot(G20, aes(area = gdp_mil_usd, fill = econ_classification, subgroup = region, 
                label = country)) +
  geom_treemap(show.legend = FALSE) +
  geom_treemap_subgroup_border(show.legend = FALSE) +
  geom_treemap_subgroup_text(grow = FALSE, alpha = 0.5, colour = "black",
                             fontface = "italic", reflow = TRUE) +
  geom_treemap_text(fontface = "italic", colour = "white",
                    reflow = TRUE ,grow = FALSE)

Created on 2020-09-20 by the reprex package (v0.3.0)

D3SL commented 3 years ago

Running your exact code I get the same result as you:

library(ggplot2)
library(treemapify)

ggplot(G20, aes(area = gdp_mil_usd, fill = econ_classification, subgroup = region, 
                label = country)) +
  geom_treemap(show.legend = FALSE) +
  geom_treemap_subgroup_border(show.legend = FALSE) +
  geom_treemap_subgroup_text(grow = FALSE, alpha = 0.5, colour = "black",
                             fontface = "italic", reflow = TRUE) +
  geom_treemap_text(fontface = "italic", colour = "white",
                    reflow = TRUE ,grow = FALSE)

Created on 2020-09-21 by the reprex package (v0.3.0)

And by doing nothing but adding place = "center" to geom_treemap_text I get this:

library(ggplot2)
library(treemapify)

ggplot(G20, aes(area = gdp_mil_usd, fill = econ_classification, subgroup = region, 
                label = country)) +
  geom_treemap(show.legend = FALSE) +
  geom_treemap_subgroup_border(show.legend = FALSE) +
  geom_treemap_subgroup_text(grow = FALSE, alpha = 0.5, colour = "black",
                             fontface = "italic", reflow = TRUE) +
  geom_treemap_text(place = "center", fontface = "italic", colour = "white",
                    reflow = TRUE ,grow = FALSE)

Created on 2020-09-21 by the reprex package (v0.3.0)

wilkox commented 3 years ago

Can you try upgrading to the latest development version of treemapify (2.5.3.9003)? This looks a lot like a bug I fixed recently where the US spelling ‘center’ was mishandled. Alternatively, can you try replacing place = "center" with place = "centre" and see if that works?

jspickering commented 3 years ago

I was having the same issue and changing place = "center" to place = "centre" worked for me. Thanks!

wilkox commented 3 years ago

@D3SL I've just released version 2.5.4 to CRAN which includes a fix for this issue – please let me know if you run into any more problems.