gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:47:31 25 4
gpt4 key购买 nike

我正在尝试访问 R 的 environmental variables来自庆典。安装指南最初让我相信 $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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com