Can we create a list of downloaded packages in R?

Is there a function where we can create a list of required packages in R? Something like "freezing" so we can quickly copy the environment?

+2


source to share


2 answers


Thank you for not being vague. Since you mentioned duplicate environments, there is information about the availability and namespaces of these available packages here.

In addition to the functions specified by @smci, will .Packages

list all packages available in the path to the library folder lib.loc

. And find.package

will show you the path to the package. Be aware of what find.packages

can be problematic when determining the availability of a package. require

- recommended method (see ?find.package

for explanation).

> x <- .packages(TRUE)
> head(x)
# [1] "assertthat"      "BH"              "car"             "data.table"     
# [5] "digest"          "dplyr"
> f <- find.package(x)
> sample(f, 5)
# [1] "/home/richard/R/x86_64-pc-linux-gnu-library/3.1/latticeExtra"  
# [2] "/home/richard/R/x86_64-pc-linux-gnu-library/3.1/Lahman"        
# [3] "/home/richard/R/x86_64-pc-linux-gnu-library/3.1/microbenchmark"
# [4] "/usr/lib/R/library/tools"                                      
# [5] "/home/richard/R/x86_64-pc-linux-gnu-library/3.1/knitr" 

      



For a list of environments with namespaces for these packages in, x

you can use (among others)getNamespace

> sapply(x, getNamespace)[1:3]
# $assertthat
# <environment: namespace:assertthat>

# $BH
# <environment: namespace:BH>

# $car
# <environment: namespace:car>

      

+2


source


If you meant " after running the appropriate code ":

  • loadedNamespaces()

    (package names only or)
  • search()

    as @Richard Scriven said


but if you meant to statically parse the given code without running it , there is no tool, but outputting the results from egrep -R -w '(require|include|source)' *.r

should give you what you want (obviously, but not used or commented out)

+2


source







All Articles