安装新版本R的无痛方法? [英] Painless way to install a new version of R?

查看:220
本文介绍了安装新版本R的无痛方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,安德鲁·盖尔曼(Andrew Gelman)感叹缺乏简易性R 的升级过程(在Windows上可能比Linux更相关).从安装软件到复制所有设置/程序包,有没有人有做升级的好窍门?

Andrew Gelman recently lamented the lack of an easy upgrade process for R (probably more relevant on Windows than Linux). Does anyone have a good trick for doing the upgrade, from installing the software to copying all the settings/packages over?

此建议包含在注释中,这是我最近一直在使用的建议.首先,您安装新版本,然后在旧版本中运行它:

This suggestion was contained in the comments and is what I've been using recently. First you install the new version, then run this in the old verion:

#--run in the old version of R
setwd("C:/Temp/")
packages <- installed.packages()[,"Package"]
save(packages, file="Rpackages")

在新版本中紧随其后:

#--run in the new version
setwd("C:/Temp/")
load("Rpackages")
for (p in setdiff(packages, installed.packages()[,"Package"]))
install.packages(p)

推荐答案

出于完整性考虑,有一些方法可以防止出现此问题.正如Dirk所说,将软件包保存在计算机上的另一个目录中.

Just for completeness, there are some ways to prevent you from having this problem. As Dirk said, save your packages in another directory on your computer.

install.packages("thepackage",lib="/path/to/directory/with/libraries")

您也可以使用功能.libPaths更改默认的.Library

You can change the default .Library value using the function .libPaths too

.libPaths("/path/to/directory/with/libraries")

这会将此路径作为第一个值放在.Library变量中,并将其设置为默认值.

This will put this path as a first value in the .Library variable, and will make it the default.

如果要进一步自动化,可以在Rprofile.site文件中进行指定,该文件位于R构建的/etc/目录中.然后,它将在每次加载R时自动加载,而您不必再为此担心.您可以只从指定目录安装和加载软件包.

If you want to automate this further, you can specify this in the Rprofile.site file, which you find in the /etc/ directory of your R build. Then it will load automatically every time R loads, and you don't have to worry about that any more. You can just install and load packages from the specified directory.

最后,我在Rprofile.site中包含一些小代码,允许我在安装新的R版本时重新安装所有软件包.您只需将其列出,然后再更新到新的R版本.我使用包含所有包的更新列表的.RData文件来做到这一点.

Finally, I have some small code included in my Rprofile.site allowing me to reinstall all packages when I install a new R version. You just have to list them up before you update to the new R version. I do that using an .RData file containing an updated list with all packages.

library(utils)

## Check necessary packages
load("G:\Setinfo\R\packagelist.RData") # includes a vector "pkgs"
installed <- pkgs %in% installed.packages()[, 'Package']
if (length(pkgs[!installed]) >=1){
  install.packages(pkgs[!installed])
}

我通过在Rprofile.site中指定.Last()来制作packagelist.RData.如果我安装了一些软件包,则会更新软件包列表:

I make the packagelist.RData by specifying .Last() in my Rprofile.site. This updates the package list if I installed some :

.Last <- function(){
  pkgs <- installed.packages()[,1]
  if (length(pkgs) > length(installed)){
    save(pkgs,file="G:\Setinfo\R\packagelist.RData")
  }
}

当我安装新的R版本时,只需将必要的元素添加到Rprofile.site文件中,然后重新安装所有软件包.无论如何,我都必须调整Rprofile.site(使用总和,为Tinn-R添加额外的代码,这些事情),所以这并不是真正的额外工作.重新安装所有软件包只需要花费额外的时间.

When I install a new R version, I just add the necessary elements to the Rprofile.site file and all packages are reinstalled. I have to adjust the Rprofile.site anyway (using sum contrasts, adding the extra code for Tinn-R, these things), so it's not really extra work. It just takes extra time installing all packages anew.

最后一点等同于原始问题中给出的解决方案.我只是不必担心首先获取已安装"列表.

This last bit is equivalent to what is given in the original question as a solution. I just don't need to worry about getting the "installed" list first.

同样,如果您没有从CRAN安装的软件包,则无法正常工作.但是此代码也很容易扩展,以包括这些代码.

Again, this doesn't work flawless if you have packages that are not installed from CRAN. But this code is easily extendible to include those ones too.

这篇关于安装新版本R的无痛方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆