Home > Software design >  Use older version of ggplot in R script file
Use older version of ggplot in R script file

Time:11-30

I have ggplot2 3.4.0 installed on Ubuntu 22.04, but for some reasons I would like to use older version ggplot2 3.3.6.

library(ggplot2, lib.loc="~/R/ggplot336/") 

I worked fine when I started with a clean script file. However, when I use an existing script file like:

library(ggplot2, lib.loc="~/R/ggplot336/") 
ggimage::geom_image()

I got the following error massage:

Error in value[3L] : Package ‘ggplot2’ version 3.4.0 cannot be unloaded: Error in unloadNamespace(package) : namespace ‘ggplot2’ is imported by ‘ggfun’, ‘ggplotify’, ‘ggimage’ so cannot be unloaded

Edit

  1. Fresh start Rstudio
  2. Open my R script file with following 3 lines:
sessionInfo()
library(ggplot2, lib.loc="~/R/ggplot336/") 
ggimage::geom_image()

When I run the first line, sessionInfo() before doing anything else. We can see ggplot2_3.4.0 there. Could this be a Rstudio feature or an issue?

R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.1 LTS

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

locale:
 [1] LC_CTYPE=en_AU.UTF-8       LC_NUMERIC=C               LC_TIME=en_AU.UTF-8       
 [4] LC_COLLATE=en_AU.UTF-8     LC_MONETARY=en_AU.UTF-8    LC_MESSAGES=en_AU.UTF-8   
 [7] LC_PAPER=en_AU.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C       

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

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9         highr_0.9          pillar_1.8.1       compiler_4.2.2    
 [5] R.utils_2.12.2     R.methodsS3_1.8.2  yulab.utils_0.0.5  tools_4.2.2       
 [9] digest_0.6.30      evaluate_0.18      jsonlite_1.8.3     lifecycle_1.0.3   
[13] tibble_3.1.8       gtable_0.3.1       ggimage_0.3.1      R.cache_0.16.0    
[17] pkgconfig_2.0.3    rlang_1.0.6        reprex_2.0.2       DBI_1.1.3         
[21] cli_3.4.1          ggplotify_0.1.0    rstudioapi_0.14    magick_2.7.3      
[25] yaml_2.3.6         xfun_0.35          fastmap_1.1.0      knitr_1.41        
[29] withr_2.5.0        dplyr_1.0.10       styler_1.8.1       generics_0.1.3    
[33] vctrs_0.5.1        fs_1.5.2           gridGraphics_0.5-1 grid_4.2.2        
[37] tidyselect_1.2.0   glue_1.6.2         R6_2.5.1           processx_3.8.0    
[41] fansi_1.0.3        rmarkdown_2.18     clipr_0.8.0        callr_3.7.3       
[45] ggplot2_3.4.0      purrr_0.3.5        magrittr_2.0.3     ps_1.7.2          
[49] htmltools_0.5.3    scales_1.2.1       assertthat_0.2.1   colorspace_2.0-3  
[53] utf8_1.2.2         munsell_0.5.0      ggfun_0.0.9        R.oo_1.25.0       

CodePudding user response:

It looks like R studio is loading in all of the previous libraries from a previous session. Maybe something leftover in .RData or .Rhistory. A fresh start of R should have far few loaded libraries.

Here is my fresh start:

sessionInfo()
R version 4.2.2 (2022-10-31)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.6.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

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

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

loaded via a namespace (and not attached):
[1] compiler_4.2.2 tools_4.2.2  

In Rstudio's preference you should see this screen:

enter image description here

I would try unchecking all of the boxes and then quit and restart Rstudio to see if that clears up the problem.

CodePudding user response:

After some experiments, I kind of worked out the problem. Since this has consumed so much time of my time, I thought this could be helpful to others.

The problem is :: (double colon) with packages using ggplot2, such as ggimage in my example. It appears that RStudio automatically load some dependent packages when a script file is open. One way to fix this problem for my work now is to remove ::, but use library() then function (geom_image) instead.

This fixed my problem but I still don't understand why RStudio behaves this way.

If :: (double colon) is necessary, comment lines containing ::, then after loading older version of ggplot2, uncomment those lines. A little awkward, but it works for me.

  • Related