sgdata
sgdata

Reputation: 2763

How can I pull the user name from Sys.info()?

In the effort of making a script as flexible as possible, I'm trying to reference the username in a path automatically, i.e. Admin in "C:/Users/Admin".

R base's Sys.info() gives me the information I'm looking for:

sysname   | release | version                      | nodename          | machine  | login
"Windows" | "7 x64" | "build 7601, Service Pack 1" | "WINDOWS MACHINE" | "x86-64" | "Admin" 

But far I've been unable to pull this as a variable/string of any kind to perform operations with.

Thanks.

Upvotes: 3

Views: 1733

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368409

These things tend to be very OS-specific. On the one I use, Sys.getenv("USER") works. But it is a good idea to abstract this away -- and as luck will have it Gabor has done that for you:

R> whoami::username()
[1] "mynmaeonthisbox"
R> 

The (micro-)package has a few other accessors too, and is on CRAN.

Edit: As much as I like the whoami package, @Joy was quite correct that the narrow answer to the question is to subset the object returned from Sys.info() -- and this will work on any R version without any additional packages. So Sys.info()["user"]

Upvotes: 5

Related Questions