Is there a simple way to programmatically determine if an R script is being executed in Windows vs. Linux?
Asked
Active
Viewed 1.0k times
46
-
I am a little late to this party but consider this Google Code Search: http://www.google.com/codesearch?as_q=linux+windows&btnG=Search+Code&hl=en&as_lang=r -- i.e. specify 'as_lang=r' to get R-based solutions only when looking for strings 'linux' and 'windows'. And it's right there.... – Dirk Eddelbuettel Jan 19 '10 at 20:10
-
fantastic! I didn't know about the GOOG code search. I struggled and struggled to search for this but kept getting unrelated results. – JD Long Jan 20 '10 at 14:53
4 Answers
57
-
i shouldn't have wasted that time with the first comment. Beaten to the punch again. – Dan Jan 19 '10 at 19:40
-
1If it had been a useful comment, I would be sympathetic... But that one was really wasted time! :) – Pekka Jan 19 '10 at 19:42
-
-
1actually this returns 'unix' on OSX also. I guess you'd need to also check `Sys.info()["sysname"]=='Darwin'` to distinguish. – conjugateprior Jan 28 '13 at 11:34
13
.Platform$OS.type
returns
[1] "unix"
or something else.
13
I run the same code from any of three Linux or Windows machines. I use the following to set up working directories:
if(R.Version()$os == "linux-gnu"){
dir.pre <- "/home"
} else {
dir.pre <- "C:/Users"
}
On my debian linux server and my Ubuntu laptop:
> .Platform$OS.type
[1] "unix"
> R.Version()$os
[1] "linux-gnu"
On my Windows 10 laptop, in RStudio:
> .Platform$OS.type
[1] "windows"
> R.Version()$os
[1] "mingw32"
Feel free to edit and add to this list.

Mus
- 7,290
- 24
- 86
- 130

mightypile
- 7,589
- 3
- 37
- 42