Question

What is a cross-platform way of getting the username of the current user in R? I am currently using

system('whoami', intern=TRUE)

However this assumes that the user has shell access, and that the whoami program is available. Is there a more native to get this information in R?

Was it helpful?

Solution

I would do this :

 Sys.getenv("USERNAME")  ## works under windows

or better more robust:

 Sys.info()[["user"]]

But under unix-like system the result is sometimes different of system('whoami', intern=TRUE) :

whoami outputs the username that the user is working under, whereas $USER outputs the username that was used to login.

For example, if the user logged in as John and su into root, whoami displays root and echo $USER displays John. This is because the su command does not invoke a login shell by default.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top