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

发布于 2024-10-21 19:57:53 字数 430 浏览 3 评论 0原文

我有一台 Macintosh,我试图自动加载包、自制函数,并在每次启动 R 时使用修改后的设置。我相信这可以通过名为 Rprofile.site 的文件并创建函数 .First 和 .Last 来完成在该文件中。

一个问题是,我不知道我的 R_HOME 目录是什么、它的用途是什么,或者它是否存在。我发现了两个函数,我认为它们都给了我它的位置,但我得到了不同的结果。

这是第一个

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

和第二个

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

据我所知,第二个目录在我的机器上甚至不存在。我当前正在从我的应用程序目录运行 R。

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.

One problem is, I have no idea what my R_HOME directory is, what it is used for, or if it even exists. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

梦言归人 2024-10-28 19:57:54

迈克尔,我也发现这个话题有点令人困惑。我也在 Mac 上。我创建了一个“Rprofile”文件,其中包含我的所有自定义内容。这是我的工作原理(我不认为我的设置有什么特别的):

  1. “Rprofile”进入 /Users/michael
  2. “Rprofile”必须由 R 能够理解的命令组成(例如,您可以来源)。
  3. “Rprofile”必须称为 .Rprofile 前导句点意味着该文件对正常操作系统是隐藏的。您必须打开一个终端窗口并执行 >ls -la 才能看到它(假设您在必要时 cd 到该目录)。另外,您还会看到许多其他隐藏文件。在您下一步创建它之前,它可能并不存在。
  4. 我使用 TextEdit 创建一个名为 R.txt 的文件并将命令放入其中(出于测试目的从简单开始)。
  5. 然后,在终端窗口中,输入 >cp R.txt .Rprofile,它将可见的 R.txt 复制到不可见的 .Rprofile 您可以通过再次执行 >ls -la 进行检查,以在目录列表中查看它。
  6. 重新启动 R 并查看是否有效。例如,如果您将库(ggplot2)放入 R.txt 中,则应在启动时加载该库。如果没有,那么该库中的命令将不起作用,例如 qplot(x = 1:10, y = 1:10)。其他人输入的命令如 cat("My .Rprofile Works!\n) 应该在启动过程中显示。HTH

Bryan

Michael, I too have found this topic to be a bit confusing. I'm on a Mac as well. I created an "Rprofile" file which has all my customizations in it. Here's how mine works (I don't think there is anything special about my set up):

  1. The "Rprofile" goes in /Users/michael
  2. The "Rprofile" has to be composed of commands that R will understand (for instance, you can source it).
  3. The "Rprofile" has to be called .Rprofile The leading period means that the file is hidden from the normal operating system. You have to open a terminal window and do an >ls -la to see it (assuming you cd to that directory, if necessary). 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).
  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.
  6. Restart R and see if it worked. For instance, if you put library(ggplot2) in your R.txt, that library should be loaded upon start up. If it doesn't, then a command from that library won't work, like qplot(x = 1:10, y = 1:10). Other people put in commands like cat("My .Rprofile works!\n) which should display during launching.

HTH Bryan

夏有森光若流苏 2024-10-28 19:57:54

如果您运行 getwd(),那么您将看到 R 启动目录。在 Mac 上,通常为 /User/login_name
这就是我将 .RProfile 留在其中加载自定义函数和常用包的地方。

另请参阅:R 中有用的小函数(放入您的 .RProfile 中)
专家 R 用户,您的 .Rprofile 中有什么?...

If you run getwd(), then you will see your R startup directory. 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.

Also see: Useful little functions in R (to put in your .RProfile)
and Expert R users, what's in your .Rprofile?…

浪漫人生路 2024-10-28 19:57:53

多年来,我开始依赖 help(Startup) 文档作为阅读此内容的最佳位置。按照丰富应用程序的惯例,有许多每个用户和每个站点的配置文件。乍一看似乎有点矫枉过正,但它确实是一个很好的系统。一旦您了解了 RenvironRenviron.site 以及 dito for Rprofile,您就会欣赏到跨平台的一致行为。

Over the years I have come to rely on the help(Startup) documentation as the best place to read up on this. 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.

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