检查R中已安装的软件包

时间:2021-02-27 21:37:54

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:

我正在使用以下代码来确保在升级R或安装其他用户时安装所有软件包:

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:

我已将它放在我的.Rprofile中的myFirst函数中,但是当我启动R时它会出现以下错误并继续启动:

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!

1 个解决方案

#1


11  

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").

接下来,如果在搜索路径上找到函数.First,则执行.First()。最后,运行基础包中的functionFirst.sys()。此调用需要附加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.

现在,installed.packages位于utils包中,它通常是默认包之一。所以它当时不可用。首先被称为。

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

也许尝试用utils :: installed.packages替换installed.packages?

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

正如Josh所说,我的眼睛掠过直接解决这个问题的那一块,即:

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.

请注意,当源站点和用户配置文件来源时,仅加载基本包,因此需要通过例如其他包来引用其他包中的对象。 utils :: dump.frames或在显式加载相关包之后。

#1


11  

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").

接下来,如果在搜索路径上找到函数.First,则执行.First()。最后,运行基础包中的functionFirst.sys()。此调用需要附加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.

现在,installed.packages位于utils包中,它通常是默认包之一。所以它当时不可用。首先被称为。

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

也许尝试用utils :: installed.packages替换installed.packages?

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

正如Josh所说,我的眼睛掠过直接解决这个问题的那一块,即:

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.

请注意,当源站点和用户配置文件来源时,仅加载基本包,因此需要通过例如其他包来引用其他包中的对象。 utils :: dump.frames或在显式加载相关包之后。