轻松安装新版本 R 的方法?
Andrew Gelman 最近对缺乏简单的升级过程表示遗憾R(在 Windows 上可能比在 Linux 上更相关)。有谁有进行升级的好技巧,从安装软件到复制所有设置/包?
这个建议包含在评论中,也是我最近一直在使用的。首先安装新版本,然后在旧版本中运行以下命令:
#--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)
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")
Followed by this in the new version:
#--run in the new version
setwd("C:/Temp/")
load("Rpackages")
for (p in setdiff(packages, installed.packages()[,"Package"]))
install.packages(p)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
在linux下,现在就很简单了。只需制作:
如果您未指定
x='4.4.0'
或您想要的最新版本,ropenblas::rcompiler 将出现 404 文件未找到错误,因为在glue< 中创建的 url /code> 附加两个
tar
,...tar.xz.tar.gz
,这不是该文件的名称,因此未找到 404。In linux, Now it is very simple. Just make:
If you don't specify
x='4.4.0'
, or latest version you want, ropenblas::rcompiler will 404 file not found error as the url created inglue
appends twotar
,...tar.xz.tar.gz
, which is not what the file is called, hence the 404 not found.为了完整起见,有一些方法可以防止您遇到此问题。正如德克所说,将包保存在计算机上的另一个目录中。
您也可以使用函数
.libPaths
更改默认的.Library
值。这会将此路径作为
.Library
变量中的第一个值,并将其设为默认值。如果您想进一步自动化此操作,您可以在 Rprofile.site 文件中指定它,您可以在 R 版本的 /etc/ 目录中找到该文件。然后每次 R 加载时它都会自动加载,你不用再担心这个了。您只需从指定目录安装并加载软件包即可。
最后,我的 Rprofile.site 中包含一些小代码,允许我在安装新的 R 版本时重新安装所有软件包。您只需在更新到新的 R 版本之前列出它们即可。我使用包含所有包的更新列表的 .RData 文件来执行此操作。
我通过在 Rprofile.site 中指定
.Last()
来创建 packagelist.RData。如果我安装了一些软件包,这会更新软件包列表:当我安装新的 R 版本时,我只需将必要的元素添加到 Rprofile.site 文件中,然后所有软件包都会重新安装。无论如何,我必须调整 Rprofile.site(使用总和对比,添加 Tinn-R 的额外代码,这些东西),所以这并不是真正的额外工作。重新安装所有软件包只是需要额外的时间。
最后一点相当于原始问题中给出的解决方案。我只是不需要担心先获取“已安装”列表。
同样,如果您有不是从 CRAN 安装的软件包,这也不能完美地工作。但这段代码也可以轻松扩展以包含这些代码。
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.
You can change the default
.Library
value using the function.libPaths
tooThis will put this path as a first value in the
.Library
variable, and will make it the default.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.
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.
I make the packagelist.RData by specifying
.Last()
in my Rprofile.site. This updates the package list if I installed some :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.
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.
如果您使用的是 Windows,您可能需要使用 installr 软件包:
执行此操作的最佳方法是从 RGui 系统。您的所有包都将转移到新文件夹,旧的包将被删除或保存(您可以选择其中之一)。
然后,一旦您再次打开 RStudio,它会立即识别出您正在使用更新的版本。对我来说,这就像一种魅力。
更多有关安装程序的信息请参见此处。
If you are using Windows, you might want to use the installr package:
The best way of doing this is from the RGui system. All your packages will be transferred to the new folder and the old ones will be deleted or saved (you can pick either).
Then once you open RStudio again, it immediately recognizes that you are using an updated version. For me this worked like a charm.
More info on installr here.
两个快速建议:
使用 Gabor 的批处理文件,据说其中包含有助于解决此问题的工具批量图书馆搬迁。警告:我没有使用过它们。
不要在已安装的 R 版本的“文件树”中安装库。在 Windows 上,我可以将 R 放入 C:/opt/R/R-$version 中,但使用以下代码片段将所有库放入 C:/opt/R/library/ 中,因为它首先缓解了问题:
Two quick suggestions:
Use Gabor's batchfiles which are said to comprise tools helping with e.g. this bulk library relocations. Caveat: I have not used them.
Don't install libraries within the 'filetree' of the installed R version. On Windows, I may put R into C:/opt/R/R-$version but place all libraries into C:/opt/R/library/ using the following snippet as it alleviates the problem in the first place:
如果您有不是来自 CRAN 的软件包,上面建议的方法将不会完全起作用。例如,个人包或从非 CRAN 站点下载的包。
我在 Windows 上的首选方法(升级 2.10.1 到 2.11.0):
R-2.10.0/library/*
复制到R-2.11.0/library /
update.packages()
The method suggested above will not completely work if you have packages that are not from CRAN. For example, a personal package or a package downloaded from a non-CRAN site.
My preferred method on Windows (upgrading 2.10.1 to 2.11.0):
R-2.10.0/library/*
toR-2.11.0/library/
update.packages()
对于问题中给出的解决方案,如果您已经安装了新版本,则运行旧版本的 R 可能并不容易。在这种情况下,您仍然可以重新安装先前 R 版本中所有缺失的软件包,如下所示。
(请注意,第一行代码中
list.files()
的参数应该是之前 R 版本的库目录的路径,其中包含之前版本中包的所有文件夹。我当前的情况是"/Library/Frameworks/R.framework/Versions/3.2/Resources/library"
如果您以前的 R 版本不是 3.2,或者您是在 Windows 上。)if
语句确保如果某个软件包for
循环的前一次迭代中。With respect to the solution given in the question, it might not be easy to run your older version of R if you have already installed the new version. In this case, you can still reinstall all missing packages from the previous R version as follows.
(Note that the argument to
list.files()
in the first line of code should be the path to the library directory for your previous R version, where all folders of packages in the previous version are. In my current case, this is"/Library/Frameworks/R.framework/Versions/3.2/Resources/library"
. This will be different if your previous R version is not 3.2, or if you're on Windows.)The
if
statement makes sure that a package is not installed iffor
loop.按照 Dirk 的建议,这里有一些在 Windows 上执行此操作的 R 代码: 如何在 Windows XP 上轻松升级 R
更新 (15.04.11):我就此主题写了另一篇文章,解释了如何处理 的常见问题在 Windows 7 上升级 R
Following Dirk's suggestion, here is some R code to do it on windows: How to easily upgrade R on windows XP
Update (15.04.11): I wrote another post on the subject, explaining how to deal with common issues of upgrading R on windows 7
两个选项:
.libPaths("C:/R/library")
Two options:
.libPaths("C:/R/library")
我使用的是 Windows 8,由于某些奇怪的原因,我永远无法使用互联网连接安装软件包。
我通常使用 CRAN 的 .zip 文件安装它。
在我从 R 3.2.5 转到 R 3.3.1 之后。
我只是将包从
C:\Path\to\packa\R\win-library\3.2
复制到C:\Path\to\packa\R\win-library\3.3< /代码>。
然后我重新启动 R 会话。效果很好。
我还没有检查所有软件包是否都运行良好。
但是,我检查过的那些都运行得很好。
希望这个技巧对每个人都有效。
干杯。
I am on Windows 8 and for some weird reason, I can never install packages using my internet connections.
I generally install it using the .zip file from CRAN.
After I went from R 3.2.5 to R 3.3.1.
I simply copied the packages from
C:\Path\to\packa\R\win-library\3.2
toC:\Path\to\packa\R\win-library\3.3
.And then I restarted the R session. Worked perfectly.
I haven't checked if ALL the packages are functioning well.
But, the ones I checked are working perfectly well.
Hope this hack works for everybody.
Cheers.
如果您有远见,接受的答案可能会起作用,但我已经摆脱了旧版本,因此无法遵循这些指示。
下面描述的步骤适用于从 2.1 和 3.1 升级 OSX。
更新:要获取最新版本的目录(而不是输入 3.1 或 3.2),您可以使用以下命令。第二个直接转换为 R 变量,跳过
.
和..
以及.DS_Store
,使用:(Add
|pbcopy
到末尾,将其直接复制到 Mac 剪贴板)然后在 R 中,您可以粘贴生成的变量。一旦在新版本的 R 中定义了它,您就可以按照上面的说明循环浏览已安装的包...
The accepted answer might work if you have foresight, but I had already gotten rid of the old version so wasn't able to follow these directions.
The steps described below worked for OSX upgrading from 2.1 and 3.1.
UPDATED: To get the directory for your most recent version (instead of typing in 3.1 or 3.2) you can use the below commands. The second one converts directly to the R-variable, skipping
.
and..
and.DS_Store
, use:(Add
|pbcopy
to the end to copy it directly to your Mac clipboard)Then within R you can paste that variable that is generated. Once that is defined in the new version of R, you can loop through the installed packages from the instructions above...
linux
+bash
+debian
+apt
用户:如果您要安装/升级到最新版本
R
版本,那么我们可能会假设您拥有root
权限。 (不是必需的,只是使过程变得更加简单;为了保持一致性,下面的脚本对所有安装使用sudo
。)由于
R
软件包也是由root
安装的,因此允许将它们放置在/usr/local/
中。下面对
curl
的调用假设您已经对R
的sid
版本感兴趣,这是最新的不稳定版本strong> 版本(根据构建/检查R
包时的需要),即cat /etc/apt/sources.list | grep 'sid' ||出口1
尽管这可以很容易地被最近的稳定版本替换,例如
buster
。请注意,我没有使用密钥,因为通常推荐。这不是必需的,特别是如果(如下面的脚本中所示)我们在
R
本身内安装软件包(下面的Rscript -e
)。此外,此类钥匙往往每隔几年就会损坏/更换。因此,当然欢迎您将以下前言添加到文件R.sh
中,如下所示:sudo apt-key adv --keyserver keyserver.ubuntu.com \
--recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
R
包的array
显然并不详尽,但给出了一些我个人的例子发现有用。使用debian
软件包进行全新安装/升级r-recommished
,如下所示,应该提供所有标准“推荐”软件包的最新版本(例如survival
)。我相信 CRAN 版本和相关debian
软件包的更新之间可能存在轻微的滞后。因此,如果必须拥有最新版本的“推荐”R
软件包,您可能希望将其中一些添加到下面的array
中。在下面的过程中安装的
debian
软件包也不是必需的(对于使用r-base
),也不详尽,但提供了一个否。对于合理的否来说很重要的“附加组件”。R
软件包。无论如何...将以下内容放入
R.sh
中:然后执行它,例如假设已经在目录中:
source R.sh
。从 shell 循环中逐一安装软件包(无论是
debian
还是R
)效率有些低,但可以更简单地跟踪错误, 恕我直言。可能需要一些时间,具体取决于编号。R
包,所以也许最简单的方法是让运行过夜......linux
+bash
+debian
+apt
users:If you're installing/upgrading to the newest version of
R
, then we may assume you haveroot
permissions. (Not essential, just makes the process a lot simpler; for consistency the script below usessudo
for all installs.)As the
R
packages are also installed byroot
, it is thus permissible to place these in/usr/local/
.The call to
curl
below assumes you are already interested in thesid
release ofR
, the very latest unstable version (as required when building/checking anR
package) i.e.cat /etc/apt/sources.list | grep 'sid' || exit 1
although this could easily be replaced with a recent stable release e.g.
buster
.Note that I am not using a key as is typically recommended. This is not essential, particularly if (as in the script which follows) we install packages within
R
itself (Rscript -e
below). Also, such keys have a tendency to break/change every few years. Thus, you are of course welcome to add the following preface to the fileR.sh
which follows:sudo apt-key adv --keyserver keyserver.ubuntu.com \
--recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
The
array
ofR
packages is clearly not exhaustive but gives some examples which I personally find useful. A fresh install/upgrade with thedebian
packager-recommended
, as below, should give the latest version of all of the the standard 'recommended' packages (e.g.survival
). I believe there may be a slight lag between a CRAN release and an update to the relevantdebian
package. Thus, you may wish to add some of these to thearray
below if having the latest version of a 'recommended'R
package is essential.The
debian
packages installed in the process below are also neither essential (for usingr-base
) nor exhaustive but provide a no. of 'add-ons' which are important for a reasonable no. ofR
packages.Anyway... place the following in
R.sh
:Then execute it, e.g. assuming in directory already:
source R.sh
.Installing packages (whether
debian
orR
) one-by-one in a loop from shell is somewhat inefficient, but allows for simpler tracing of errors, IMHO. May take some time depending on the no. ofR
packages, so maybe simplest to let run overnight...对我来说这个页面很好
https ://www.r-statistics.com/2013/03/updating-r-from-r-on-windows-using-the-installr-package/
或者
另一个选项是安装新选项,最后放置,例如在我的电脑的 Windows 中
.libPaths(c(
“D:/文档/R/win-library/3.2”,
“C:/ Program Files / R / R-3.2.3 /库”,
“C:/ Program Files / R / R-3.2.0 /库”,
“D:/文档/R/win-library/2.15”
)
在我的情况下,最后一个版本的每个路径我总是将第一个路径是固定的“D:/Documents/R/win-library/3.2”
然后我放了另一个,因为你不需要复制或移动任何包,在我的建议中,只需调用它
for me this page is good
https://www.r-statistics.com/2013/03/updating-r-from-r-on-windows-using-the-installr-package/
or
another option is just install the new option and at final you put, for example in windows in my pc
.libPaths(c(
"D:/Documents/R/win-library/3.2",
"C:/Program Files/R/R-3.2.3/library",
"C:/Program Files/R/R-3.2.0/library",
"D:/Documents/R/win-library/2.15"
)
every path of last version in my case i always put the first path is "D:/Documents/R/win-library/3.2" that is fixed
and then i put the other because you do not need copy or move any packages, in my sugest just call it