Check for installed packages in R

11,687

It appears from reading ?Startup that:

Next, if a function .First is found on the search path, it is executed as .First(). Finally, function .First.sys() in the base package is run. This calls require to attach the default packages specified by options("defaultPackages").

Now, installed.packages is in the utils package, which is typically one of the default packages. So it's not available at the time .First is called.

Perhaps try replacing installed.packages with utils::installed.packages?

As Josh notes below my eyes skimmed over the piece that addresses this issue directly, namely:

Note that when the site and user profile files are sourced only the base package is loaded, so objects in other packages need to be referred to by e.g. utils::dump.frames or after explicitly loading the package concerned.

Share:
11,687
sinclairjesse
Author by

sinclairjesse

Updated on June 14, 2022

Comments

  • sinclairjesse
    sinclairjesse about 2 years

    Based on the answer to this question: Elegant way to check for missing packages and install them?

    I'm using the following code to make sure that all packages are installed when I upgrade R, or set up other users:

    list.of.packages <- c("RODBC", "reshape2", "plyr")
    new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
    if(length(new.packages)) install.packages(new.packages)
    

    I've placed it in my .First function in my .Rprofile, but when I start up R it gives the following error and continues starting up:

    Error in match(x, table, nomatch = 0L) : 
      could not find function "installed.packages"
    

    If I run the code after I get a prompt it works fine. Any ideas why?

    Thanks!