bash 从 Mac OSX 终端运行 R
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44336345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running R from Mac OSX terminal
提问by sjc725
I've searched the web, and I'm still unclear on how to run R from the Mac terminal. I have Rstudio and the standalone R app installed. I thought I could just type "R" from the command line as I do with "python", but that doesn't work. Is it necessary to edit the PATH in my bash profile? If so, how do I give the correct location of R?
我在网上搜索过,我仍然不清楚如何从 Mac 终端运行 R。我安装了 Rstudio 和独立的 R 应用程序。我以为我可以像使用“python”一样从命令行输入“R”,但这不起作用。是否有必要在我的 bash 配置文件中编辑 PATH?如果是这样,我该如何给出 R 的正确位置?
Thanks for any help
谢谢你的帮助
Edits after receiving comments
收到评论后修改
So, I'm running Sierra, and when I type "r" or "R" at the terminal, I get "-bash: R: command not found." If I type, "which R" in the terminal I do not get any output.
所以,我正在运行 Sierra,当我在终端输入“r”或“R”时,我得到“-bash: R: command not found”。如果我在终端中输入“which R”,我不会得到任何输出。
Here is the output from "echo $PATH": /usr/local/heroku/bin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Users/samuelcolon/anaconda/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Users/samuelcolon/.rvm/gems/ruby-2.1.0/bin:/Users/samuelcolon/.rvm/gems/ruby-2.1.0@global/bin:/Users/samuelcolon/.rvm/rubies/ruby-2.1.0/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/samuelcolon/.rvm/bin:/Users/samuelcolon/.rvm/bin
这是“echo $PATH”的输出:/usr/local/heroku/bin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/用户/samuelcolon/anaconda/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Users/samuelcolon/.rvm/gems/ruby-2.1.0/bin:/Users/samuelcolon/.rvm/gems /ruby-2.1.0@global/bin:/Users/samuelcolon/.rvm/rubies/ruby-2.1.0/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin :/usr/bin:/bin:/usr/sbin:/sbin:/Users/samuelcolon/.rvm/bin:/Users/samuelcolon/.rvm/bin
As for the installation, I believe I downloaded it directly from cran.r-project.org a while ago. I can locate the GUI in my applications and open it-- I have version 3.13. Is it possible, I only have R.app installed but not R? Perhaps that's the reason I'm getting the 'command not found' when typing "R" into the terminal?
至于安装,相信我前段时间直接从cran.r-project.org下载的。我可以在我的应用程序中找到 GUI 并打开它——我有 3.13 版。可能吗,我只安装了 R.app 而没有安装 R?也许这就是我在终端中输入“R”时收到“找不到命令”的原因?
Generally, I've been working in RStudio, but I'd still like to access R from the terminal and also to find where things are located. I'm fine with removing and re-installing R if it's easiest to start from square one. I hope the extra detail helps, and I appreciate the responses.
一般来说,我一直在 RStudio 工作,但我仍然想从终端访问 R 并找到东西所在的位置。如果从第一个开始最容易,我可以删除并重新安装 R。我希望额外的细节有帮助,我很感激你的回答。
回答by Cam_Aust
An answer for those not that familiar with Terminal and Bash.
对于那些不熟悉 Terminal 和 Bash 的人来说,这是一个答案。
I have done a fresh update install of R from the R.org cran site as part of seeking an answer to your question.
作为寻求问题答案的一部分,我从 R.org cran 站点完成了 R 的全新更新安装。
I found this latest install version 3.4.0 installs R for access in Terminal, and also installs R.app as part of the package.
我发现这个最新的安装版本 3.4.0 安装了 R 以便在终端中访问,并且还安装了 R.app 作为包的一部分。
To my understanding, reading support docs, if you have an older version of R it will update that. However it will not update an installation of R installed by the anaconda package.
据我了解,阅读支持文档,如果您有旧版本的 R,它会更新它。但是,它不会更新由 anaconda 包安装的 R 安装。
Where are the R files stored?
R文件存储在哪里?
I can only assume that with a fresh install of the latest R, R will work for you in Terminal.
我只能假设通过全新安装最新的 R,R 将在终端中为您工作。
To learn where the R files are that are being accessed - in Terminal after starting R, and in R.app, type:
要了解正在访问 R 文件的位置 - 启动 R 后在终端中,以及在 R.app 中,键入:
>R.home()
>R.home()
In my case as example:
以我为例:
In R.app - the R version 3.4.0 is accessed in the top directory (not my user folder):
在 R.app 中 - 在顶级目录(不是我的用户文件夹)中访问 R 版本 3.4.0:
R.home()
[1] "/Library/Frameworks/R.framework/Resources"
In Terminal - the R version 3.3.2 is accessed in the Anaconda package, again in the top level directory.
在终端中,R 版本 3.3.2 在 Anaconda 包中访问,再次在顶级目录中。
R.home()
[1] "/anaconda/lib/R"
So I have two different versions of R, and Terminal accesses a different version to R.app.
所以我有两个不同版本的 R,终端访问 R.app 的不同版本。
How can I ensure I access the same version in Terminal as I do in the R.app?
如何确保我在终端中访问的版本与在 R.app 中访问的版本相同?
For someone familiar with bash, and how the whole bash command system works I am sure there is a well constructed command. All the same here are some novice solutions.
对于熟悉 bash 以及整个 bash 命令系统如何工作的人,我确信有一个构造良好的命令。这里都是一些新手解决方案。
-
——
??First Solution:
??第一个解决方案:
I could update the anaconda version, however, I would prefer not to as as other elements of the anaconda package my depend on this older version of R. For those not yet familiar with Terminal and bash, not such a novice solution.
我可以更新 anaconda 版本,但是,我不想像 anaconda 包的其他元素一样依赖这个旧版本的 R。对于那些还不熟悉终端和 bash 的人,这不是一个新手解决方案。
-
——
? Second Solution:
? 第二种解决方案:
This solution came from mko. It provides a single use solution. From the result above, and checking the directory structure a little further to find this R file.
这个解决方案来自 mko。它提供了一次性使用的解决方案。从上面的结果,并进一步检查目录结构以找到这个 R 文件。
Finding the significant R file enables me to edit an extension of the above path shown in the R.app. So add /bin/R to enter
找到重要的 R 文件使我能够编辑 R.app 中显示的上述路径的扩展名。所以添加 /bin/R 进入
/Library/Frameworks/R.framework/Resources/bin/R
Entering and pressing return will start R from this version.
输入并按回车将从这个版本启动 R。
Alternatively, one can find this file and icon in the GUI Finder, lead by the above result, and just double click on it, and it will open Terminal and a session with R running for you. Easy!
或者,你可以在 GUI Finder 中找到这个文件和图标,以上述结果为导向,双击它,它会打开终端和一个运行 R 的会话。简单!
One could also make an alias of it and put it on your desktop for easy future starts.
还可以为它取一个别名并将其放在您的桌面上,以便将来轻松启动。
-
——
? Third Solution:
? 第三个解决方案:
My last solution I think may be best, adding to mko's solution. Make an alias.
我认为我的最后一个解决方案可能是最好的,添加到 mko 的解决方案中。做一个别名。
Being in my home directory in Terminal I open .bash_profile using the nano text editor. (If you do not already know how to do this, then best not use this solution.)
在终端的主目录中,我使用 nano 文本编辑器打开 .bash_profile。(如果您还不知道如何执行此操作,则最好不要使用此解决方案。)
I then add the line in this env file.
然后我在这个 env 文件中添加这一行。
alias Rv340='/Library/Frameworks/R.framework/Resources/bin/R'
I then save the changes and exit this terminal session. I then open a new Terminal window. (This is so the changes to the env above are incorporated in the new terminal session).
然后我保存更改并退出此终端会话。然后我打开一个新的终端窗口。(这样对上面 env 的更改就合并到了新的终端会话中)。
Then when I enter the alias:
然后当我输入别名时:
Rv340
The version of R I want opens.
RI 想要的版本打开。
You can choose a different alias name to "Rv340".
您可以为“Rv340”选择不同的别名。
-
——
??Fourth Solution:
??第四种解决方案:
A second more permanent solution for opening the same version of R in Terminal is as follows.
在终端中打开相同版本的 R 的第二个更持久的解决方案如下。
Copy the path as showing in R.app in response to the R.home() command above, and add that path to PATH in your .bash_profile. (If you do not know already how to do this, then ignore this solution.) Do so as follows.
复制 R.app 中显示的路径以响应上面的 R.home() 命令,然后将该路径添加到 .bash_profile 中的 PATH。(如果您还不知道如何执行此操作,请忽略此解决方案。)按以下步骤操作。
export PATH="/Library/Frameworks/R.framework/Resources:$PATH"
To my understanding, this ensures that bash looks here for R (and anything else), then moves on to the other paths in PATH. Since this adds this path to the beginning of $PATH, an env variable, bash looks here first where it finds the newer version first, and stops looking.
据我了解,这可确保 bash 在此处查找 R(以及其他任何内容),然后移至 PATH 中的其他路径。由于这会将此路径添加到 $PATH(一个环境变量)的开头,因此 bash 首先在此处查找较新版本的位置,然后停止查找。
When it comes to understanding PATH in the env set up in .bash_profile the following two links were helpful.
当谈到在 .bash_profile 中设置的环境中理解 PATH 时,以下两个链接很有帮助。
How to correctly add a path to PATH.
This solution may muck with anaconda's invocation of R. I have yet to check this.
这个解决方案可能与 anaconda 对 R 的调用有关。我还没有检查过。
回答by Oo.oO
First of all, you have to start terminal application. You can use either built in Terminal.app, or you can use replacement. My favorite one is iTerm2
首先,您必须启动终端应用程序。您可以使用内置的 Terminal.app,也可以使用替换。我最喜欢的是 iTerm2
Then, you simply open terminal window and run R. Just like shown below:
然后,您只需打开终端窗口并运行 R。 如下所示:
Have fun with R!
玩得开心 R!
回答by Sagar Singh
An easy way to open RStudio with admin privilege on macOS:
在 macOS 上使用管理员权限打开 RStudio 的简单方法:
- Go to Applications, then right click on RStudio
- Select "Show Package Contents"
- Go to Contents/MacOS
- Now open terminal(in bash mode). Type sudo and drag the RStudio.exec into terminal and press on ENTER
- 转到应用程序,然后右键单击 RStudio
- 选择“显示包裹内容”
- 转到目录/MacOS
- 现在打开终端(在 bash 模式下)。输入 sudo 并将 RStudio.exec 拖入终端并按 ENTER
Now RStudio will have admin access!
现在 RStudio 将拥有管理员权限!