使用RStudio更新R

316

我该如何通过RStudio更新R?


3
答案不一致,似乎自问答以来有很多事情发生了变化... - Arthur Yip
10个回答

302
为了完整起见,答案是:您无法在RStudio内部执行此操作。@agstudy说得对-您需要安装更新版本的R,然后重新启动RStudio,它将自动使用新版本,如@Brandon所指出的那样。
如果有类似于install.packages()函数或update.packages(function)的update.R()函数,那就太好了。
因此,为了安装R,
  1. 访问http://www.r-project.org
  2. 单击“CRAN”,
  3. 然后选择您喜欢的CRAN站点。我喜欢Kansas:http://rweb.quant.ku.edu/cran/
  4. 单击“下载XXX的R”[其中XXX是您的操作系统]
  5. 按照您的操作系统的安装过程
  6. 重新启动RStudio
  7. 欢呼
--等等,我的宝贵包呢?--

好的,我使用Mac电脑,因此我只能提供Mac电脑的准确细节 - 也许其他人可以提供Windows / Linux的准确路径; 我相信过程是相同的。

为了确保您的软件包与最新版本的R兼容,请执行以下操作:

  1. move the packages from the old R installation into the new version; on Mac OSX, this means moving all folders from here:

    /Library/Frameworks/R.framework/Versions/2.15/Resources/library
    

    to here:

    /Library/Frameworks/R.framework/Versions/3.0/Resources/library
    

    [where you'll replace "2.15" and "3.0" with whatever versions you're upgrading from and to. And only copy whatever packages aren't already in the destination directory. i.e. don't overwrite your new 'base' package with your old one - if you did, don't worry, we'll fix it in the next step anyway. If those paths don't work for you, try using installed.packages() to find the proper pathnames.]

  2. now you can update your packages by typing update.packages() in your RStudio console, and answering 'y' to all of the prompts.

    > update.packages(checkBuilt=TRUE)
    class :
     Version 7.3-7 installed in /Library/Frameworks/R.framework/Versions/3.0/Resources/library 
     Version 7.3-8 available at http://cran.rstudio.com
    Update (y/N/c)?  y
    ---etc---
    
  3. finally, to reassure yourself that you have done everything, type these two commands in the RStudio console to see what you have got:

    > version
    > packageStatus()
    

4
每当有人说用 R 无法做某事时,我就想去尝试一下。看起来是时候使用 RCurl 和一些 system 调用来完成一些事情了... - Dason
我迫不及待地期待着你实现它 :) - RyanStochastic
18
这不是我的作品,但已经有人为Windows用户完成了这项工作:https://github.com/talgalili/installr - Dason
4
因此,后来的答案表明,无论是“正确”的答案还是得票最高的答案都没有给出非常完整的答案,似乎这在这里是如此显而易见,应该采取一些措施来更新这个问题。 - Stenemo
2
看起来终于有一个R包可以使用Mac电脑从RStudio更新R了:https://github.com/AndreaCirilloAC/updateR - jroberayalas
显示剩余3条评论

154
你可以从官方网站下载并安装最新版本的R。 重新启动RStudio后,它应该会自动使用新版本。 如果需要手动更改,可以在RStudio中进入:工具 ->选项 ->常规。 详细步骤请查看@micstr的答案

48
RStudio会自动检测到这一点... 只有在您想要使用不同的(以前的、x32、x64)位版本时,您才需要更新选项。 - Brandon Bertelsen
7
如何更新R版本?我的R版本从未更新过。帮助菜单中的“更新”只是更新RStudio本身。 - lovetl2002
3
安装新版本的R后,您必须重新启动RStudio。为了让RStudio自动检测到新版本,请关闭并重新打开RStudio。 - warship
1
@warship,真的不明白你的评论,对我来说,“关闭并重新打开RStudio”等同于“重启RStudio”。 - agstudy
我在Windows 10上使用RStudio。我安装了最新版本的R(3.4.0)和早期版本(3.3.2,作为另一个程序的一部分安装)。RStudio无法加载最新版本,即3.4.0,但我可以手动设置为最新版本,并且一切似乎都很好。 - Reza Dodge
显示剩余4条评论

121
如果您使用的是Windows系统,您可以使用installr。使用示例可在此处找到

8
请注意,你可以从RStudio内运行更新过程。 - peter2108
6
这也是我的首选方式。移动所有软件包的代码特别简单。`# 安装/加载软件包: if(!require(installr)) { install.packages("installr"); require(installr)} #加载/安装+加载installrupdateR(F, T, T, F, T, F, T) #安装、移动,更新软件包,退出 R。` - Tom
@peter2108 不行。至少我必须使用installr而不是Rstudio。 - lovetl2002
@Zhubarb,@kungfujam,@Tom:运行 updateR() 后需要多长时间?我的 Rstudio 会在运行此命令时卡住。不确定这是否正常。谢谢! - Ryan Chase
1
我遇到了一个错误:Error in file(con, "r") : cannot open the connection. 有什么想法吗? - derelict
3
如果你遇到"Error in file(con, "r")"的问题,尝试使用setInternet2(TRUE)命令。详见Troubleshooting section - ToJo

73

我建议使用Windows软件包installr来完成此操作。该软件包不仅会更新您的R版本,还将复制并更新所有的包。有一篇关于这个话题的博客在这里。只需在R Studio中运行以下命令,并按照提示进行操作:

# installing/loading the package:
if(!require(installr)) {
install.packages("installr"); require(installr)} #load / install+load installr

# using the package:
updateR() # this will start the updating process of your R installation.  It will check for newer versions, and if one is available, will guide you through the decisions you'd need to make.

11
"installr" 包似乎是一个很好的解决方案,但不幸的是它只适用于 Windows。 - Michael MacAskill
1
@clemlaflemme:运行 updateR() 后需要多长时间?我的 Rstudio 会在运行此命令时卡住,不确定这是否正常。谢谢! - Ryan Chase
非常合理;就目前而言,我甚至都记不起来了,这意味着这不是一个问题。 - ClementWalter

47

如果您使用的是Mac计算机,您可以使用新的updateR包从RStudio更新R版本:http://www.andreacirillo.com/2018/02/10/updater-package-update-r-version-with-a-function-on-mac-osx/

简而言之,您需要执行以下步骤:

使用updateR在Rstudio中更新R版本,您只需运行以下五行代码

install.packages('devtools') #assuming it is not already installed
library(devtools)
install_github('andreacirilloac/updateR')
library(updateR)
updateR(admin_password = 'Admin user password')

在安装过程的最后,会弹出一条消息来确认安装已经完成:

everything went smoothly
open a Terminal session and run 'R' to assert that latest version was installed

1
成功安装新版本的R(如上所述),系统要求更新软件包并询问是否要重新启动R。无论选择是或否,我都会收到以下消息:install.packages(as.vector(needed_packages)) Error in install.packages : object 'needed_packages' not found - petzi
@petzi 这个问题应该在后续版本中得到修复。请查看官方仓库以获取更多更新:https://github.com/AndreaCirilloAC/updateR - Andrea Cirillo
@andrea-cirillo 是的,谢谢。问题不再出现了。 - petzi
2
这似乎是一个问题。尝试从3.5.2更新到3.6.2,但收到了相同的消息。 - Adam_G
2021年5月 - UpdateR 工作正常,但没有对我的软件包进行任何更改。我使用的是 R.app 控制台,而不是 Rstudio。 - DryLabRebel
显示剩余3条评论

16
将以下内容粘贴到控制台并运行命令:
## How to update R in RStudio using installr package (for Windows)
## paste this into the console and run the commands
## "The updateR() command performs the following: finding the latest R version, downloading it, running the installer, deleting the installation file, copy and updating old packages to the new R installation."
## more info here: https://cran.r-project.org/web/packages/installr/index.html

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

## Watch for small pop up windows. There will be many questions and they don't always pop to the front. 
## Note: It warns that it might work better in Rgui but I did it in Rstudio and it worked just fine. 

它说“包‘installr’不可用(适用于R版本3.2.0)”... - munmunbb
FYI,这仍然有效。每当我需要更新R时,我会在这里搜索我的答案并粘贴代码。 :) - Cara Wogsland

8
有一个名为installr的新软件包,可以在Windows平台上使用R内部更新您的R版本。该软件包是基于版本3.2.3构建的。
从R Studio中,单击工具并选择安装包...,然后键入名称“installr”并单击安装。或者,在控制台中键入install.packages("installr")
一旦R Studio完成安装软件包,通过在控制台中输入require(installr)来加载它。
要开始更新R安装过程,请键入updateR()。此函数将检查是否有更新版本的R,如果有,它将指导您需要做出的决策。如果您的R安装已经是最新的,则会返回FALSE。
如果您选择下载和安装更新版本,则有一个选项可将所有软件包从当前R安装复制/移动到更新的R安装中,这非常方便。
更新过程结束后,请退出并重新启动R Studio。 R Studio将加载更新的R版本。

如果您希望了解如何使用installr包,请跟随此链接


4
不要使用Rstudio来更新R。Rstudio并不是R,它只是一个集成开发环境。这个答案总结了不同操作系统的先前答案。对于所有操作系统,在更新前提前查看已安装的软件包此处是很方便的。
WINDOWS ->> 以管理员身份打开CMD/Powershell并输入“R”进入交互模式。如果无法运行,请搜索并运行RGui.exe而不是在控制台中写入R ...然后:
lib_path <- gsub( "/", "\\\\" , Sys.getenv("R_LIBS_USER"))
install.packages("installr", lib = lib_path)
install.packages("stringr", lib_path)
library(stringr, lib.loc = lib_path)
library(installr, lib.loc = lib_path)
installr::updateR()

苹果电脑操作系统 ->> 您可以使用updateR软件包。该软件包不在CRAN上,因此您需要在Rgui中运行以下代码:

install.packages("devtools")
devtools::install_github("AndreaCirilloAC/updateR")
updateR(admin_password = "PASSWORD") # Where "PASSWORD" stands for your system password

请注意,计划在不久的将来合并updateR和installR以适用于Mac和Windows。
Linux ->> 目前installr不可用于Linux/MacOS(请参阅当前版本0.20的文档)。由于已安装了R,因此您可以按照这些说明进行操作(在Ubuntu中,尽管其他发行版的思路相同:添加源��更新和升级并安装)。

更新R时是否有一种方法可以告诉它管理员用户名? - Alex

4

在安装完新版本的R之后,只需重新启动R Studio即可。要确认您正在使用新版本,请在控制台中输入 >version ,您应该会看到新的细节。


1
我发现在Linux下保持最新的永久解决方案是安装R-patched项目。这将使您的R安装保持最新状态,您甚至不需要在安装之间移动软件包(这在RyanStochastic的答案中有描述)。
对于openSUSE,请参见此处的说明

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接