轻松安装新版本 R 的方法?

发布于 2024-08-05 03:08:48 字数 610 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

岁月如刀 2024-08-12 03:08:49

在linux下,现在就很简单了。只需制作:

    install.packages("ropenblas")
    ropenblas::rcompiler(x='4.4.0')

如果您未指定 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:

    install.packages("ropenblas")
    ropenblas::rcompiler(x='4.4.0')

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 in glue appends two tar, ...tar.xz.tar.gz, which is not what the file is called, hence the 404 not found.

回忆那么伤 2024-08-12 03:08:48

为了完整起见,有一些方法可以防止您遇到此问题。正如德克所说,将包保存在计算机上的另一个目录中。

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

您也可以使用函数 .libPaths 更改默认的 .Library 值。

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

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

如果您想进一步自动化此操作,您可以在 Rprofile.site 文件中指定它,您可以在 R 版本的 /etc/ 目录中找到该文件。然后每次 R 加载时它都会自动加载,你不用再担心这个了。您只需从指定目录安装并加载软件包即可。

最后,我的 Rprofile.site 中包含一些小代码,允许我在安装新的 R 版本时重新安装所有软件包。您只需在更新到新的 R 版本之前列出它们即可。我使用包含所有包的更新列表的 .RData 文件来执行此操作。

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。如果我安装了一些软件包,这会更新软件包列表:

.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 的额外代码,这些东西),所以这并不是真正的额外工作。重新安装所有软件包只是需要额外的时间。

最后一点相当于原始问题中给出的解决方案。我只是不需要担心先获取“已安装”列表。

同样,如果您有不是从 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.

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

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

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

This 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.

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])
}

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

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.

烟柳画桥 2024-08-12 03:08:48

如果您使用的是 Windows,您可能需要使用 installr 软件包:

install.packages("installr")
require(installr)
updateR()

执行此操作的最佳方法是从 RGui 系统。您的所有包都将转移到新文件夹,旧的包将被删除或保存(您可以选择其中之一)。
然后,一旦您再次打开 RStudio,它会立即识别出您正在使用更新的版本。对我来说,这就像一种魅力。

更多有关安装程序的信息请参见此处

If you are using Windows, you might want to use the installr package:

install.packages("installr")
require(installr)
updateR()

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.

满地尘埃落定 2024-08-12 03:08:48

两个快速建议:

  1. 使用 Gabor 的批处理文件,据说其中包含有助于解决此问题的工具批量图书馆搬迁。警告:我没有使用过它们。

  2. 不要在已安装的 R 版本的“文件树”中安装库。在 Windows 上,我可以将 R 放入 C:/opt/R/R-$version 中,但使用以下代码片段将所有库放入 C:/opt/R/library/ 中,因为它首先缓解了问题:

$ cat .Renviron         # this is using MSys/MinGW which looks like Cygwin  
## Example .Renviron on Windows    
R_LIBS="C:/opt/R/library"

Two quick suggestions:

  1. Use Gabor's batchfiles which are said to comprise tools helping with e.g. this bulk library relocations. Caveat: I have not used them.

  2. 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:

$ cat .Renviron         # this is using MSys/MinGW which looks like Cygwin  
## Example .Renviron on Windows    
R_LIBS="C:/opt/R/library"
梦醒灬来后我 2024-08-12 03:08:48

如果您有不是来自 CRAN 的软件包,上面建议的方法将不会完全起作用。例如,个人包或从非 CRAN 站点下载的包。

我在 Windows 上的首选方法(升级 2.10.1 到 2.11.0):

  1. 安装 R-2.11.0
  2. R-2.10.0/library/* 复制到 R-2.11.0/library /
  3. 对于询问您是否可以覆盖的提示,回答“否”。
  4. 启动 R 2.11.0
  5. 运行 R 命令 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):

  1. Install R-2.11.0
  2. Copy R-2.10.0/library/* to R-2.11.0/library/
  3. Answer "no" to the prompts asking you if it is okay to overwrite.
  4. Start R 2.11.0
  5. Run the R command update.packages()
葮薆情 2024-08-12 03:08:48

对于问题中给出的解决方案,如果您已经安装了新版本,则运行旧版本的 R 可能并不容易。在这种情况下,您仍然可以重新安装先前 R 版本中所有缺失的软件包,如下所示。

# Get names of packages in previous R version
old.packages <- list.files("/Library/Frameworks/R.framework/Versions/3.2/Resources/library")

# Install packages in the previous version. 

# For each package p in previous version...
    for (p in old.packages) {
      # ... Only if p is not already installed
      if (!(p %in% installed.packages()[,"Package"])) {
        # Install p 
        install.packages(p) 
      }
    }

(请注意,第一行代码中 list.files() 的参数应该是之前 R 版本的库目录的路径,其中包含之前版本中包的所有文件夹。我当前的情况是 "/Library/Frameworks/R.framework/Versions/3.2/Resources/library" 如果您以前的 R 版本不是 3.2,或者您是在 Windows 上。)

if 语句确保如果某个软件包

  • 已安装在新的 R 版本中,或者
  • 已作为已安装软件包的依赖项进行安装, 则该软件包不会安装在 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.

# Get names of packages in previous R version
old.packages <- list.files("/Library/Frameworks/R.framework/Versions/3.2/Resources/library")

# Install packages in the previous version. 

# For each package p in previous version...
    for (p in old.packages) {
      # ... Only if p is not already installed
      if (!(p %in% installed.packages()[,"Package"])) {
        # Install p 
        install.packages(p) 
      }
    }

(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 if

  • It's already installed in the new R version, or
  • Has been installed as a dependency from a package installed in a previous iteration of the for loop.
窗影残 2024-08-12 03:08:48

按照 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

就像说晚安 2024-08-12 03:08:48

两个选项:

  1. 此处实施我的答案
  2. 如果您使用在带有 StatET 的 Eclipse 下的 R,打开运行配置,单击控制台选项卡,然后在名为启动后运行 R 代码段的框中添加此行选择目录:.libPaths("C:/R/library")

Two options:

  1. Implement my answer here
  2. If you use R under Eclipse with StatET, open Run Configurations, click on Console tab and in the box called R snippet run after startup add this line with your choice of directory: .libPaths("C:/R/library")
初心 2024-08-12 03:08:48

我使用的是 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 to C:\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.

甚是思念 2024-08-12 03:08:48

如果您有远见,接受的答案可能会起作用,但我已经摆脱了旧版本,因此无法遵循这些指示。
下面描述的步骤适用于从 2.1 和 3.1 升级 OSX。

更新:要获取最新版本的目录(而不是输入 3.1 或 3.2),您可以使用以下命令。第二个直接转换为 R 变量,跳过 ... 以及 .DS_Store,使用:

OLD=$(ls -d /Library/Frameworks/R.framework/Versions/*.* |tail -n 2 | head -n 1)Resources/library/
echo "packages = c(\"`ls $OLD | tail +4| paste -s -d ',' - | sed -E 's|,|\",\"|'g`\")" | tr -d "/" 

(Add |pbcopy 到末尾,将其直接复制到 Mac 剪贴板)

然后在 R 中,您可以粘贴生成的变量。一旦在新版本的 R 中定义了它,您就可以按照上面的说明循环浏览已安装的包...

for (p in setdiff(packages, installed.packages()[,"Package"]))
   install.packages(p, dependencies=TRUE, quiet=TRUE, ask=FALSE)

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:

OLD=$(ls -d /Library/Frameworks/R.framework/Versions/*.* |tail -n 2 | head -n 1)Resources/library/
echo "packages = c(\"`ls $OLD | tail +4| paste -s -d ',' - | sed -E 's|,|\",\"|'g`\")" | tr -d "/" 

(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...

for (p in setdiff(packages, installed.packages()[,"Package"]))
   install.packages(p, dependencies=TRUE, quiet=TRUE, ask=FALSE)
柠檬色的秋千 2024-08-12 03:08:48

linux + bash + debian + apt 用户:

  1. 如果您要安装/升级到最新版本R 版本,那么我们可能会假设您拥有 root 权限。 (不是必需的,只是使过程变得更加简单;为了保持一致性,下面的脚本对所有安装使用sudo。)
    由于 R 软件包也是由 root 安装的,因此允许将它们放置在 /usr/local/ 中。

  2. 下面对 curl 的调用假设您已经对 Rsid 版本感兴趣,这是最新的不稳定版本strong> 版本(根据构建/检查 R 包时的需要),即

    cat /etc/apt/sources.list | grep 'sid' ||出口1

    尽管这可以很容易地被最近的稳定版本替换,例如buster

  3. 请注意,我没有使用密钥,因为通常推荐。这不是必需的,特别是如果(如下面的脚本中所示)我们在 R 本身内安装软件包(下面的 Rscript -e)。此外,此类钥匙往往每隔几年就会损坏/更换。因此,当然欢迎您将以下前言添加到文件 R.sh 中,如下所示:

    sudo apt-key adv --keyserver keyserver.ubuntu.com \
    --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

  4. R 包的 array 显然并不详尽,但给出了一些我个人的例子发现有用。使用 debian 软件包进行全新安装/升级 r-recommished,如下所示,应该提供所有标准“推荐”软件包的最新版本(例如survival)。我相信 CRAN 版本和相关 debian 软件包的更新之间可能存在轻微的滞后。因此,如果必须拥有最新版本的“推荐”R 软件包,您可能希望将其中一些添加到下面的array 中。

  5. 在下面的过程中安装的 debian 软件包也不是必需的(对于使用 r-base),也不详尽,但提供了一个否。对于合理的否来说很重要的“附加组件”。 R 软件包。

无论如何...将以下内容放入R.sh中:

sudo apt update && sudo apt --yes full-upgrade
sudo apt install --yes libappstream4 curl
### ov1 = online version; lv1 = local version (i.e. currently installed)
ov1=$(curl --silent --url https://packages.debian.org/sid/r-base |
    grep 'meta name=\"Keywords\"' |
    grep --only-matching '[0-9].*[0-9]') ; echo $ov1
## command -v = print a description of COMMAND similar to the `type' builtin
## && = if prior command succeeds, then do; || = if prior fails, then do
command -v 'R --version' &&
    lv1=$(R --version |
              grep --only-matching '[0-9\.]*[0-9]' |
              ## || = otherwise
              head -1) ||
        lv1=0
## 'lt' = less than
if dpkg --compare-versions "$lv1" 'lt' "$ov1" 
then ## declare -a = indexed array
     declare -a deb1=('r-base' 'r-base-dev' 'r-recommended')
     for i in "${deb1[@]}"
     do sudo apt install --yes "$i"
     done
fi
### certain Debian packages are required by 'R' so best have these first
sudo apt install --yes ccache libcairo2-dev libxml2-dev libcurl4-openssl-dev \
     libssl-dev liblapack-dev libssl-dev
declare -a pkg1=('data.table' 'ggplot2' 'knitr' 'devtools' 'roxygen2')
## installing as 'root' so these are installed in
Rscript -e ".libPaths()[1]"
for i in "${pkg1[@]}"
do sudo Rscript -e "install.packages('$i', dependencies=TRUE)"
done
### other useful additions
sudo apt install --yes libblas-dev libboost-dev libarmadillo-dev \
     jags pandoc pandoc-citeproc 
sudo apt update && sudo apt full-upgrade

然后执行它,例如假设已经在目录中:source R.sh

从 shell 循环中逐一安装软件包(无论是 debian 还是 R)效率有些低,但可以更简单地跟踪错误, 恕我直言。可能需要一些时间,具体取决于编号。 R 包,所以也许最简单的方法是让运行过夜......

linux + bash + debian + apt users:

  1. If you're installing/upgrading to the newest version of R, then we may assume you have root permissions. (Not essential, just makes the process a lot simpler; for consistency the script below uses sudo for all installs.)
    As the R packages are also installed by root, it is thus permissible to place these in /usr/local/.

  2. The call to curl below assumes you are already interested in the sid release of R, the very latest unstable version (as required when building/checking an R 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.

  3. 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 file R.sh which follows:

    sudo apt-key adv --keyserver keyserver.ubuntu.com \
    --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

  4. The array of R packages is clearly not exhaustive but gives some examples which I personally find useful. A fresh install/upgrade with the debian package r-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 relevant debian package. Thus, you may wish to add some of these to the array below if having the latest version of a 'recommended' R package is essential.

  5. The debian packages installed in the process below are also neither essential (for using r-base) nor exhaustive but provide a no. of 'add-ons' which are important for a reasonable no. of R packages.

Anyway... place the following in R.sh:

sudo apt update && sudo apt --yes full-upgrade
sudo apt install --yes libappstream4 curl
### ov1 = online version; lv1 = local version (i.e. currently installed)
ov1=$(curl --silent --url https://packages.debian.org/sid/r-base |
    grep 'meta name=\"Keywords\"' |
    grep --only-matching '[0-9].*[0-9]') ; echo $ov1
## command -v = print a description of COMMAND similar to the `type' builtin
## && = if prior command succeeds, then do; || = if prior fails, then do
command -v 'R --version' &&
    lv1=$(R --version |
              grep --only-matching '[0-9\.]*[0-9]' |
              ## || = otherwise
              head -1) ||
        lv1=0
## 'lt' = less than
if dpkg --compare-versions "$lv1" 'lt' "$ov1" 
then ## declare -a = indexed array
     declare -a deb1=('r-base' 'r-base-dev' 'r-recommended')
     for i in "${deb1[@]}"
     do sudo apt install --yes "$i"
     done
fi
### certain Debian packages are required by 'R' so best have these first
sudo apt install --yes ccache libcairo2-dev libxml2-dev libcurl4-openssl-dev \
     libssl-dev liblapack-dev libssl-dev
declare -a pkg1=('data.table' 'ggplot2' 'knitr' 'devtools' 'roxygen2')
## installing as 'root' so these are installed in
Rscript -e ".libPaths()[1]"
for i in "${pkg1[@]}"
do sudo Rscript -e "install.packages('$i', dependencies=TRUE)"
done
### other useful additions
sudo apt install --yes libblas-dev libboost-dev libarmadillo-dev \
     jags pandoc pandoc-citeproc 
sudo apt update && sudo apt full-upgrade

Then execute it, e.g. assuming in directory already: source R.sh.

Installing packages (whether debian or R) 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. of R packages, so maybe simplest to let run overnight...

隐诗 2024-08-12 03:08:48

对我来说这个页面很好
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文