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

查看:17
本文介绍了安装新版本 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:SetinfoRpackagelist.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:SetinfoRpackagelist.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天全站免登陆