Skip to content


R: detecting on which machine R is running

When developing some R program, I guess it’s a common occurrence to use one desktop machine with a comfortable GUI to code and run early tests, and then push this code to a bigger, more powerful machine for running the program “for real”. I suppose it’s also a common occurrence that those two machine don’t use the same operating system, or the same precise version of R, or at least not the same folder structure. For all those reasons, it might be comfortable, to avoid back-and-forth editing, to have a piece of code to determine the machine on which the program is running.

Well, luckily there’s a function that can help: Sys.info(). It returns information such as OS name (Windows, Linux), version, computer name, etc. Here are 2 example outputs:

> Sys.info()
       sysname        release        version       nodename        machine          login           user effective_user 
     "Windows"        "7 x64"   "build 7600"       "DR-WHO"       "x86-64"        "Admin"        "Admin"        "Admin

> Sys.info()
                              sysname                               release
                              "Linux"     "2.6.38.2-grsec-xxxx-grs-ipv6-64"
                              version                              nodename
"#2 SMP Thu Aug 25 16:40:22 UTC 2011"                             "clust02"
                              machine                                 login
                             "x86_64"                               "david"
                                 user                        effective_user
                              "david"                               "david"

It’s interesting to notice that the “release” and “version” fields won’t exactly return the same information on Windows and Linux. On Windows, release will be what I see most often called version, e.g. (Windows) 7 x64, while on Linux it will display the kernel release just like uname -r. On Windows, version will be the build number, so probably only different between service packs, on Linux it will display the kernel version mainly as a date, just like uname -v.

Anyway, because you’ll probably be using this just to differentiate 2 machines, the choice of what item to pick shouldn’t be too hard. Because I have one machine on Windows and the other on Linux, I just use this:
if(Sys.info()["sysname"]=="Windows") {setwd('G:/DATA/R/Prog1');} # here
if(Sys.info()["sysname"]=="Linux") {setwd('~/MyProgs/Prog2');} # on the server

Finally, if you’re wondering what this does on Mac OS X, here’s a clue.

Posted in R (R-project).


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.

Sorry about the CAPTCHA that requires JS. If you really don't want to enable JS and still want to comment, you can send me your comment via e-mail and I'll post it for you.

Please solve the CAPTCHA below in order to fight spamWordPress CAPTCHA