46

Is there a simple way to programmatically determine if an R script is being executed in Windows vs. Linux?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
JD Long
  • 59,675
  • 58
  • 202
  • 294
  • 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 Answers4

57
if(.Platform$OS.type == "unix") {
} else {

}
Marek
  • 49,472
  • 15
  • 99
  • 121
Dan
  • 6,008
  • 7
  • 40
  • 41
18
Sys.info()["sysname"]
rcs
  • 67,191
  • 22
  • 172
  • 153
13
.Platform$OS.type

returns

[1] "unix"

or something else.

Marek
  • 49,472
  • 15
  • 99
  • 121
Spacedman
  • 92,590
  • 12
  • 140
  • 224
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