Pretty much the same!
path.expand("~")
#[1] "/Users/Simon"
path.expand
will expand a path name by replacing a leading tilde by the user's home directory (if defined on that platform).
And Sys.getenv()
to get the value of environmental variables defined on your system, e.g.
# Path to R home directory
Sys.getenv( "R_HOME" )
#[1] "/Library/Frameworks/R.framework/Resources"
# Path to default R library
Sys.getenv("R_LIBS")
#[1] "~/R64Libs"
To see the available environment variables...
head( names(Sys.getenv()) )
#[1] "__CF_USER_TEXT_ENCODING" "Apple_PubSub_Socket_Render" "Apple_Ubiquity_Message"
#[4] "COMMAND_MODE" "DISPLAY" "EDITOR"
Defining a new environmental variable
To set an environment variable to make it always available to R you need to set that variable in a file called .Renviron
which by default is located in your {$HOME}
directory. So for instance to make the environment variable R_WORKSPACE
available I add the line
R_WORKSPACE = ~/Documents/R/StackOverflow
To /Users/Simon/.Renivron
. Then when I load up R you see that path expansion is done automatically...
# Clean workspace - commented out so you don't wipe your session!
# rm( list = ls() )
# See that variable is now available in R
Sys.getenv( "R_WORKSPACE" )
[1] "~/Documents/R/StackOverflow"
See the answer here for a bit more info and options.