Search code examples
rreproducible-research

Packages listed in Imports of loaded package not listed in loaded pacakges in sessionInfo


How do I get packages that are imported by another package and their versions?

I was under the impression that if I load package A and package A lists package B under Imports: in it's DESCRIPTION, then sessionInfo() should list package B under "loaded via a namespace (and not attached):".

This does not seem to be the case. Is there a simple way of getting a list of those packages?

My use case would be to document what packages in what versions are used in a simulation study. But this is definitely also helpful for some reproducible examples, etc.

It seems saving the output of sessionInfo() is not enough to thoroughly document what packages are used in a session.


Solution

  • After some digging I found out more about the behaviour of sessionInfo and a possible solution to my problem.

    After calling the function that imports the imported package via :: the imported package is listed in the output of sessionInfo(). So to get a really reproducible example or to completely document the used packages run sessionInfo() after your code not before.

    A solution to get all potentially imported packages from the dependencies, is the following script using the pkgdepends package:

    library(pkgdepends)
    
    # load packages to be attached here
    
    sessionInfo() |> 
      _$otherPkgs |> 
      names()
    
    deps <- sessionInfo() |> 
      _$otherPkgs |> 
      names() |>
      new_pkg_deps()
    
    deps$solve()
    
    deps$get_solution()$data |> 
      subset(select=c("ref", "old_version"))
    

    Edit: the behaviour that a package is only loaded on use and not attached is also described here in the r-pkgs book.