5

Is there a way to know what is the size of each package installed in R? I mean, the size that each packages have on my computer.

Any comment will be appreciated.

Thank you

geomicrobio
  • 321
  • 1
  • 3
  • 15

2 Answers2

3

Or the same without needing fs here (in case you are on a proper OS):

> system(paste("du -sh", system.file(package="dplyr"), "| awk '{print $1}'"), intern=TRUE)
[1] "2.1M"
> 

because

$ du -sh /usr/local/lib/R/site-library/dplyr
2.1M    /usr/local/lib/R/site-library/dplyr
$ 

Easy to generalize to look over all entries in .libPaths()[1], say.

Edit: But as the question was about the whole directory, let's try that too. Looking at du --help makes me learn about -d 1 which is what we want there. Then:

 > res <- read.table(pipe("du -d 1 /usr/local/lib/R/site-library/"), col.names=c("size", "name"))                                                                                                                  
!> head(res)                                                                                                                                                                                                       
    size                                       name                                                                                                                                                                
 1 15376  /usr/local/lib/R/site-library/StanHeaders                                                                                                                                                                
 2   540     /usr/local/lib/R/site-library/restfulr                                                                                                                                                                
 3  1028 /usr/local/lib/R/site-library/mlr3learners                                                                                                                                                                
 4  1360       /usr/local/lib/R/site-library/ndjson                                                                                                                                                                
 5  3920      /usr/local/lib/R/site-library/viridis                                                                                                                                                                
 6   300  /usr/local/lib/R/site-library/sessioninfo                                                                                                                                                                
 >  

Obviously this can be dressed up to take the path from .libPaths()[1] and to also edit down column names by gsub()-ing out the fixed path. But the gits is there and by not specifying 'human-readable' pretty size we can sort and compute easily too:

> head(res[order(-res$size), ])
        size                                              name
1030 2964632                    /usr/local/lib/R/site-library/
137   322800   /usr/local/lib/R/site-library/covid19.model.sa2
187   276324 /usr/local/lib/R/site-library/stxBrain.SeuratData
971   153532                  /usr/local/lib/R/site-library/BH
691    95368               /usr/local/lib/R/site-library/rstan
940    85972               /usr/local/lib/R/site-library/GO.db
> 

Edit 2 For kicks, here is a loop over .libPaths() aggregating all directories as desired.

res <- do.call(rbind, lapply(.libPaths(), \
       function(d) read.table(pipe(paste("du -d 1", d)),  \
                              col.names=c("size", "name"))))
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
1

To apply the answer in @GordonShumway, basically you need to find the directory it's installed in and then you can sum the file info. Since all packages have a description file, I use that:

system.file("DESCRIPTION", package = "dplyr") |> # replace dplyr with target pkg
  dirname() |>
  fs::dir_info(all = TRUE, recurse = TRUE) |>
  getElement("size") |>
  sum()

Marcus
  • 3,478
  • 1
  • 7
  • 16