从Mac OSX终端运行R [英] Running R from Mac OSX terminal

查看:324
本文介绍了从Mac OSX终端运行R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索过网络,但仍不清楚如何从Mac终端运行R.我安装了Rstudio和独立的R应用程序.我以为我可以只输入"R"就像我对"python"所做的那样,在命令行中进行操作,但这是行不通的.是否有必要在我的bash个人资料中编辑PATH?如果是这样,我该如何确定R的正确位置?

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?

感谢您的帮助

因此,我正在运行Sierra,当我键入"r"时,或"R"在终端,我得到"-bash:R:未找到命令".如果输入,则哪个R"在终端中我没有任何输出.

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.

这是"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

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

对于安装,我相信前一段时间我是直接从cran.r-project.org下载的.我可以在应用程序中找到该GUI并打开它- 我有版本3.13.是否可以只安装R.app但不能安装R?也许这就是我在键入"R"时收到找不到命令"的原因.进入终端?

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?

通常,我一直在RStudio中工作,但我仍然想从终端访问R并查找对象的位置.如果最简单的从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.

推荐答案

针对那些不熟悉Terminal和Bash的人的答案.

我已经从R.org部落网站上完成了R的全新安装,这是为您的问题寻求答案的一部分.

I have done a fresh update install of R from the R.org cran site as part of seeking an answer to your question.

我发现此最新的安装版本3.4.0安装R以在Terminal中进行访问,并且还将R.app作为软件包的一部分进行安装.

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.

据我了解,请阅读支持文档,如果您有较旧的R版本,它将对其进行更新.但是,它将不会更新anaconda软件包安装的R的安装.

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文件存储在哪里?

Where are the R files stored?

我只能假设全新安装了最新的R后,R将在Terminal中为您工作.

I can only assume that with a fresh install of the latest R, R will work for you in Terminal.

要了解正在访问的R文件的位置-在启动R后在终端中以及在R.app中键入:

To learn where the R files are that are being accessed - in Terminal after starting R, and in R.app, type:

>R.home()

以我的情况为例:

In my case as example:

在R.app中-在顶部目录(不是我的用户文件夹)中访问R版本3.4.0:

In R.app - the R version 3.4.0 is accessed in the top directory (not my user folder):

R.home()
[1] "/Library/Frameworks/R.framework/Resources"

在终端中-R版本3.3.2可在Anaconda包中访问,再次在顶层目录中访问.

In Terminal - the R version 3.3.2 is accessed in the Anaconda package, again in the top level directory.

R.home()
[1] "/anaconda/lib/R"

所以我有两个不同的R版本,并且Terminal访问R.app的另一个版本.

So I have two different versions of R, and Terminal accesses a different version to R.app.

如何确保在终端机中访问的版本与在R.app中访问的版本相同?

How can I ensure I access the same version in Terminal as I do in the R.app?

对于熟悉bash以及整个bash命令系统如何工作的人,我相信这里有一个结构合理的命令.这都是一些新手解决方案.

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.

-

••第一个解决方案:

• First Solution:

我可以更新anaconda版本,但是,我不希望将anaconda包中的其他元素作为我较早版本的R的依赖.对于那些还不熟悉Terminal和bash的人,还不是这样的新手解决方案.

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.

-

•第二种解决方案:

• Second Solution:

此解决方案来自mko.它提供了一次性使用的解决方案.根据上面的结果,并进一步检查目录结构以找到此R文件.

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.

找到重要的R文件,使我可以编辑R.app中显示的上述路径的扩展名.因此,添加/bin/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

/Library/Frameworks/R.framework/Resources/bin/R

输入并按回车键将从该版本开始R.

Entering and pressing return will start R from this version.

或者,您可以在GUI Finder中找到该文件和图标,并以上述结果为指导,然后双击该文件和图标,它将打开Terminal并为您运行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!

还可以为其别名,并将其放在桌面上,以方便将来使用.

One could also make an alias of it and put it on your desktop for easy future starts.

-

•第三种解决方案:

• Third Solution:

我认为我的最后一个解决方案可能是最好的,增加了mko的解决方案.创建别名.

My last solution I think may be best, adding to mko's solution. Make an alias.

在终端的主目录中,我使用nano文本编辑器打开.bash_profile. (如果您还不知道如何执行此操作,则最好不要使用此解决方案.)

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

然后我将行添加到此env文件中.

I then add the line in this env file.

alias Rv340='/Library/Frameworks/R.framework/Resources/bin/R'

然后我保存更改并退出此终端会话.然后,我打开一个新的终端窗口. (因此对上述env所做的更改已合并到新的终端会话中).

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

然后,当我输入别名时:

Then when I enter the alias:

Rv340

我要的R版本打开.

您可以为"Rv340"选择其他别名.

You can choose a different alias name to "Rv340".

-

••第四解决方案:

• Fourth Solution:

在Terminal中打开相同版本的R的第二种更永久的解决方案如下.

A second more permanent solution for opening the same version of R in Terminal is as follows.

根据上面的R.home()命令,复制R.app中显示的路径,然后将该路径添加到.bash_profile中的PATH中. (如果您尚不知道如何执行此操作,请忽略此解决方案.)请按照以下步骤进行操作.

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.

export PATH="/Library/Frameworks/R.framework/Resources:$PATH"

据我了解,这可以确保bash在此处查找R(以及其他任何内容),然后继续执行PATH中的其他路径.由于这会将此路径添加到$ PATH开头(一个env变量),因此bash首先在此处查找,首先查找较新的版本,然后停止查找.

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_profile中设置的环境中的PATH,以下两个链接会很有帮助.

When it comes to understanding PATH in the env set up in .bash_profile the following two links were helpful.

关于PATH.

如何正确向PATH添加路径.

此解决方案可能与anaconda调用R无关.我尚未对此进行检查.

This solution may muck with anaconda's invocation of R. I have yet to check this.

这篇关于从Mac OSX终端运行R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆