简体   繁体   English

如何在OSX上自动加载R中的设置? 如何找到R_HOME,配置Rprofile.site等?

[英]How to automatically load settings in R on OSX? How to find R_HOME, configure Rprofile.site, etc?

I have a Macintosh and I am trying to automatically load packages, homemade functions, and use modified setting every time I start R. I believe this can be done with a file called Rprofile.site, and by creating the functions .First and .Last in that file. 我有一个Macintosh,我试图自动加载包,自制函数,并在每次启动R时使用修改后的设置。我相信这可以通过一个名为Rprofile.site的文件来完成,并通过创建函数.First和.Last在那个文件中。

One problem is, I have no idea what my R_HOME directory is, what it is used for, or if it even exists. 一个问题是,我不知道我的R_HOME目录是什么,它用于什么,或者它是否存在。 I found two functions that I thought both gave me its location but I am getting different results. 我找到了两个功能,我认为它们都给了我它的位置,但我得到了不同的结果。

Here's the first 这是第一个

> Sys.getenv("R_home")
R_home 
    ""

And the second 第二个

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

As far as I can tell that second directory doesn't even exist on my machine. 据我所知,我的机器上甚至不存在第二个目录。 I am currently running R from my applications directory. 我目前正在从我的应用程序目录运行R.

Over the years I have come to rely on the help(Startup) documentation as the best place to read up on this. 多年来,我已经开始依赖help(Startup)文档作为阅读此内容的最佳位置。 There are numerous per-user and per-site configuration file as is customary for rich applications. 按照丰富的应用程序的惯例,有许多每用户和每站点配置文件。 It may seem like overkill at first but it is a really good system. 一开始可能看起来有些过分,但这是一个非常好的系统。 And once you grok Renviron versus Renviron.site and dito for Rprofile , you appreciate the consistent behaviour across platforms. 一旦你了解了RenvironRenviron.site和dito for Rprofile ,你会欣赏跨平台的一致行为。

Michael, I too have found this topic to be a bit confusing. 迈克尔,我也发现这个话题有点令人困惑。 I'm on a Mac as well. 我也在Mac上。 I created an "Rprofile" file which has all my customizations in it. 我创建了一个“Rprofile”文件,其中包含我的所有自定义。 Here's how mine works (I don't think there is anything special about my set up): 这是我的工作方式(我不认为我的设置有什么特别之处):

  1. The "Rprofile" goes in /Users/michael “Rprofile”进入/ Users / michael
  2. The "Rprofile" has to be composed of commands that R will understand (for instance, you can source it). “Rprofile”必须由R将理解的命令组成(例如,您可以获取它)。
  3. The "Rprofile" has to be called .Rprofile The leading period means that the file is hidden from the normal operating system. 必须调用“Rprofile”.Rprofile前导时段表示文件对正常操作系统隐藏。 You have to open a terminal window and do an >ls -la to see it (assuming you cd to that directory, if necessary). 您必须打开一个终端窗口并执行> ls -la才能看到它(假设您在必要时cd到该目录)。 Plus you'll see lots of other hidden files. 另外,你会看到很多其他隐藏文件。 And it probably doesn't exist until you create it, next step. 它可能在您创建下一步之前不存在。
  4. I use TextEdit to create a file called R.txt and put the commands in there (start simple for testing purposes). 我使用TextEdit创建一个名为R.txt的文件并将命令放在那里(开始时很简单,用于测试)。
  5. Then, in a terminal window, I type >cp R.txt .Rprofile which copies the visible R.txt to the invisible .Rprofile You can check by doing >ls -la again to see it in the directory listing. 然后,在终端窗口中,我键入> cp R.txt .Rprofile,它将可见的R.txt复制到不可见的.Rprofile您可以通过再次执行> ls -la来检查它是否在目录列表中查看它。
  6. Restart R and see if it worked. 重新启动R并查看它是否有效。 For instance, if you put library(ggplot2) in your R.txt, that library should be loaded upon start up. 例如,如果将库(ggplot2)放在R.txt中,则应在启动时加载该库。 If it doesn't, then a command from that library won't work, like qplot(x = 1:10, y = 1:10). 如果没有,则来自该库的命令将不起作用,如qplot(x = 1:10,y = 1:10)。 Other people put in commands like cat("My .Rprofile works!\\n) which should display during launching. 其他人输入像cat(“我的.Rprofile工作!\\ n)”这样的命令,这些命令应该在启动时显示。

HTH Bryan HTH布莱恩

If you run getwd() , then you will see your R startup directory. 如果你运行getwd() ,那么你将看到你的R启动目录。 On a mac it is typically /User/login_name That is where I leave my .RProfile where I load custom functions and also frequently used packages. 在mac上它通常是/User/login_name这是我离开我的.RProfile地方我加载自定义函数和经常使用的包。

Also see: Useful little functions in R (to put in your .RProfile) and Expert R users, what's in your .Rprofile?… 另请参阅: R中的有用的小函数 (放入.RProfile)和Expert R用户,你的.Rprofile中有什么?...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM