r - 从 Bash 访问 R 的环境变量

标签 r bash

我正在尝试访问 R 的 environmental variables来自 bash。安装指南最初让我相信 $R_HOME 是在 bash 中声明的。然而,这似乎并不准确。

有没有办法从 bash 访问这些环境变量?

前面的问题询问了如何使用 Sys.getenv() 或 Sys.setenv() 从 R 中访问这些变量。这个问题的重点是能够通过bash访问信息。简单地调用 echo $R_HOME 不会在 bash 变量中产生适当的路径。

例如,我可以使用复杂的方法获取信息:

R_HOME=$(Rscript -e "Sys.getenv('R_HOME')" | grep -Po '".*?"' | sed 's/"//g')

所以我会收到: [1]“/usr/lib64/R”

Grep 然后给出: “/usr/lib64/R”

最后是 sed: /usr/lib64/R

我真的更愿意找到一种方法,让我无需经历此过程即可访问所有环境变量。

R_HOME/etc/Renviron 中列出了一些环境变量的定义。 。然而,它们在 bash 中都不可用。

最佳答案

这可能比前面两个答案建议的更容易:

因为 RHOME 非常重要,所以执行 R RHOME:

edd@don:~$ R RHOME
/usr/lib/R
edd@don:~$ val=$(R RHOME)
edd@don:~$ echo ${val}
/usr/lib/R
edd@don:~$ 

基本上所有其他功能都可以通过R CMD config ...

edd@don:~$ R CMD config --help | head -20
Usage: R CMD config [options] [VAR]

Get the value of a basic R configure variable VAR which must be among
those listed in the 'Variables' section below, or the header and
library flags necessary for linking against R.

Options:
  -h, --help            print short help message and exit
  -v, --version         print version info and exit
      --cppflags        print pre-processor flags required to compile
            a C/C++ file using R as a library
      --ldflags         print linker flags needed for linking a front-end
                        against the R library
      --no-user-files  ignore customization files under ~/.R
      --no-site-files  ignore site customization files under R_HOME/etc

Variables:
  BLAS_LIBS     flags needed for linking against external BLAS libraries
  CC            C compiler command
  CFLAGS        C compiler flags
edd@don:~$ 

最后,对于正在运行的 R session ,您可以启动一个并访问它:

edd@don:~$ Rscript -e 'cat(Sys.getenv("PATH"))'     # manual break
/home/edd/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:\
/sbin:/bin:/usr/games:/usr/local/gamesedd@don:~$ 

关于r - 从 Bash 访问 R 的环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28044030/

相关文章:

r - 按多列的值组合数据集

regex - 使用 grep 在源代码中查找字符串中的所有查询

PHP脚本被杀死没有解释

R irlba 稀疏数据表示

r - 在 RStudio 中为 R CMD 构建启用小插图压缩

r - 语音到文本转换 R

r - 如何使用 httr 为基于证书的身份验证指定证书、 key 和根证书?

linux - 如何从网页中提取数据(用户名)

linux - 如何压缩多个文件夹,每个文件夹都压缩到自己的 zip 存档中?

python - 当在 bash 中调用的 python 脚本遇到错误时,如何停止 bash 脚本?