Check for installed packages in R

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

I use the following code to make sure all packages are installed when I upgrade R or configure 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 put it in my .First function in my .Rprofile, but when I run R, it gives the following error and continues to run:

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

If I run the code after receiving the prompt, it works fine. Any ideas why?

Thanks!

+6
source share
1 answer

From reading ?Startup it is clear that:

Further, if a function is found in the search path. First, it runs as .First (). Finally, the function .First.sys () in the base package is run. For this call you need to attach the default packages specified in the option ("defaultPackages").

Now installed.packages is in the utils package, which is usually one of the default packages. Therefore, it is not available at the instant. .First .

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

As Josh’s notes under my eyes looked through a part that directly addresses this issue, namely:

Please note that when the site and profile file get only the basic package is loaded, so objects in other packages should be mentioned, for example, utils :: dump.frames or after the package is explicitly downloaded.

+11
source

Source: https://habr.com/ru/post/919271/


All Articles