Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: determine if a script is running in Windows or Linux

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

like image 808
JD Long Avatar asked Jan 19 '10 19:01

JD Long


4 Answers

if(.Platform$OS.type == "unix") { } else {  } 
like image 64
Dan Avatar answered Oct 17 '22 04:10

Dan


Sys.info()["sysname"]
like image 42
rcs Avatar answered Oct 17 '22 06:10

rcs


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.

like image 27
mightypile Avatar answered Oct 17 '22 04:10

mightypile


.Platform$OS.type

returns

[1] "unix"

or something else.

like image 24
Spacedman Avatar answered Oct 17 '22 06:10

Spacedman