How can I make R read my environmental variables?
You want Sys.getenv()
as in Sys.getenv("PATH")
, say.
Or for your example, try
SIR <- Sys.getenv("SIR")
system(paste("ec2-cancel-spot-instance-requests", SIR))
As for setting variables at startup, see help(Startup)
to learn about ~/.Renvironment
etc
Accessing environment variables set in R session from shell
The environmental variable dies with the process.
Each process has its own set of environmental variables, inherited from the parent process. When you create the environmental variable BLAH
, you create it in the environment of the R process you're running, but not in the environment of the parent process.
If you want another process to access this environmental variable, you'll need to start the process from within R. Then the child process will inherit BLAH
. This documentation for Sys.setenv
mentions this:
Sys.setenv sets environment variables (for other processes called from within R or future calls to Sys.getenv from this R process).
For example:
Sys.setenv(BLAH="blah")
system("echo $BLAH")
# blah
How to access a bash environment variable from within R in emacs-ess
I don't know about R and self-defined environment variables, but I set the PATH variable in emacs to the same value as in my bashrc. I modified my code to your problem, give it a shot and let me know if it works.
;; set env variable in Emacs
(getenv "SETTINGS")
(setenv "SETTINGS" "/home/user/settings.xml")
Original code (for PATH) is:
;; Emacs has its own path variable
(getenv "PATH")
(setenv "PATH"
(concat
"/usr/local/texlive/2011/bin/x86_64-linux" ":"
(getenv "PATH")))
Reading global environment from within a function in R
Probably cleanest to pass the environment:
fun <- function(envir = parent.frame()) ls(envir = envir)
fun()
This lists the objects in the caller but also lets the user change which environment is used. For example, they could force the global environment to be used:
fun(.GlobalEnv)
set environment variables for system() in R?
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
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`
etc pp.
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.
Accessing R's Environmental Variables from Bash
It may be easier than the two earlier answers suggest:
Because RHOME
is so central, do R RHOME
:
edd@don:~$ R RHOME
/usr/lib/R
edd@don:~$ val=$(R RHOME)
edd@don:~$ echo ${val}
/usr/lib/R
edd@don:~$
Essentially all others are available via 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:~$
Lastly, for a running R session you can start one and access it:
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:~$
Related Topics
Centos Directory Structure as Tree
How to Capture Network Packets Per Pid
Linux Bash - Parse Date in Custom Format
Bash: Difference Between "Export K=1" VS. "K=1"
How to Check If Hadoop Daemons Are Running
Possible to Use a .Dll on Linux
How to Sleep for 1 Second Between Each Xargs Command
View a Particular Line of a File Denoted by a Number
Linux Command to Empty All Files of a Directory
Arch Linux - Apt-Get Update Equivalent Command
How to Recursively Copy a Directory into Another and Replace Only the Files That Have Not Changed
Link a Static Library to a Shared One During Build
Using Sed to Remove a Block of Text
Doesn't Sh Support Process Substitution <(...)
Create New File But Add Number If Filename Already Exists in Bash
Getting Disconnection Notification Using Tcp Keep-Alive on Write Blocked Socket