在 R 中为 system() 设置环境变量? [英] set environment variables for system() in R?

查看:67
本文介绍了在 R 中为 system() 设置环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在 Ubuntu 中使用 R 来使用 system() 进行系统调用,例如启动 Amazon EC2 实例、管理 S3 上的文件等.如果我从命令行启动 R一切正常.但是,如果我从使用 Rscript 的脚本或从 ESS 启动 R,我会遇到未设置环境变量的问题.

I've been using R in Ubuntu to make system calls using system() for things like spinning up Amazon EC2 instances, managing files on S3, etc. If I start R from the command line everything works fine. But if I start R from a script using Rscript, or from ESS, I have issues with environment variables not being set.

认为这是一个问题,我没有正确理解在 Ubuntu 中设置环境变量的位置.我认为正确的地方"(对于正确"的某些定义)是在 ~/.bashrc 中设置用户环境变量.这是我设置 export EC2_HOME=/home/jd/ec2 之类的地方,但是当我从 ESS 执行 R 并进行系统调用时,.bashrc 脚本没有运行.我试过谷歌搜索,我看到很多关于 Ubuntu 环境变量的解释,例如 这个.我的下意识反应是尝试上述线程中的每个建议,并在其中一个选项起作用时停止大便.但后来我得到了非标准的设置,这让我后来很生气.

I think this is an issue with me not properly grokking where to set environment variables in Ubuntu. I thought the "right place" (for some definition of "right") was to set user environment variables in ~/.bashrc. This is where I set things like export EC2_HOME=/home/jd/ec2 but when I execute R from ESS and make system calls, the .bashrc script is not being run. I've tried Googing about and I see many an exegesis on environment variables in Ubuntu, such as this one. My knee jerk reaction is to try each recommendation in the aforementioned thread and stop giving a shit as soon as one of the options works. But then I end up with non-standard settings which bite me in the ass later.

那么我应该如何设置环境变量,以便在 R 中运行 system() 调用时正确设置它们?

So how should I set environment variables so that they are properly set when I run a system() call in R?

推荐答案

我认为你混淆了这个问题.我担心这可能与登录 shell 与非登录 shell 有关.请参阅 bash 手册页以了解细则……过去曾让我发疯.

I think you are confusing the issue. I fear this may be about login shells versus non-login shells. See the bash manual page for the fine print ... which has driven me bonkers in the past.

也就是说,如果您可以在系统范围内设置环境变量,那么您有几个选择:

That said, if you can set environment variables system-wide, you have a few options:

  • /etc/environment 是一个非常好的地方,因为如果您使用不同的 shell,它与 shell 无关
  • 对于登录和非登录 shell,获得完全控制的一种我认为合适的方法是将 my 更改为 ~/.local_bashrc 之类的东西
  • 添加.~/.local_bashrc from and and all of

  • /etc/environment is a very good place as it is shell-agnostic should you ever use a different shell
  • for login versus non-login shells, the one way to get complete control that I found suitable was to put my changes into something like ~/.local_bashrc
  • the add . ~/.local_bashrc from and and all of

  • ~./bashrc
  • ~/.bash_profile
  • ~/.profile`

等等.

您可以在采购之前使用 echo Hello from FILE 在其中将 FILE 替换为文件名.这向您展示了从登录(例如通过 gdm 等)、通过 ssh 连接、通过新的 xterm 等终端开始的 shell 之间的区别等等开.

You can precede the sourcing with a echo Hello from FILE where you replace FILE with the name of the file. That shows you the difference between shells starting from login (eg via gdm et al), via ssh connection, via new xterm etc terminals and so on.

这篇关于在 R 中为 system() 设置环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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