Detecting Operating System in R (E.G. for Adaptive .Rprofile Files)

detecting operating system in R (e.g. for adaptive .Rprofile files)

I'm not sure about using Sys.info() since the help page says it is not implemented on all R platforms; maybe use .Platform instead? ?.Platform has a lot of useful information, since:

‘.Platform’ is a list with some
details of the platform under which R
was built. This provides means to
write OS-portable R code.

It also seems the packages included with R use .Platform much more frequently than Sys.info.

josh: /c/R/R-2.12.0-src/src/library
> grep ".Platform" */R/* | wc -l
144
josh: /c/R/R-2.12.0-src/src/library
> grep ".Platform\$OS.type" */R/* | wc -l
99
josh: /c/R/R-2.12.0-src/src/library
> grep "Sys.info" */R/* | wc -l
4

How to check the OS within R

use Sys.info() for all information about the system, Sys.info()['sysname'] gives you the OS.

R.Version() gives you the version of R, including which architecture you're running (32bit - i386 - versus 64bit - x64 - ).

R.home() and system.file(package="xxx") give you information of the location of the root resp. the package files.

Set working directory in Rstudio based on OS

You can get the OS details by simply type Sys.info() at the console. I don't have access to R currently but I think the answer should be like this:

a = Sys.info()[1]
if( a == "Windows") { set the working dir in windows}
if( a != "Windows") { set the working dir in other OS}

Sys.info() returns a vector containing the following values which you can find also in the help by ?Sys.info:

sysname 
The operating system name.

release
The OS release.

version
The OS version.

nodename
A name by which the machine is known on the network (if any).

machine
A concise description of the hardware, often the CPU type.

login
The user s login name, or "unknown" if it cannot be ascertained.

user
The name of the real user ID, or "unknown" if it cannot be ascertained.

effective_user
The name of the effective user ID, or "unknown" if it cannot be ascertained. This may differ from the real user in ‘set-user-ID’ processes.

You can return the first element for OS name.

In R, what's the canonical way to detect if the OS is 64-bit?

Actually none of those methods would be canonical, which I take to mean "what would Brian Ripley say". Try this:

?.Machine

sizeof.pointer........the number of bytes in a C SEXP type. Will be 4 on 32-bit builds and 8 on 64-bit builds of R.

 64bit <- .Machine$sizeof.pointer == 8
64bit
#[1] TRUE

As for your nominations only one of them returns TRUE on my machine:

> Sys.info()["machine"] == "x86-64"
machine
FALSE
> .Platform$r_arch == "x64"
[1] FALSE
> version$arch == "x86_64"
[1] TRUE

Is it possible to identify if R is being run in unix or windows environment

The command .Platform returns details of the platform. You can access the information about the operating system with

.Platform$OS.type

This returns either "unix" or "windows".

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

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

}

Change working directory based on Operating System or Conditionals

The data about your machine should be available in Sys.info().

Mac OS will typically have a "Darwin" as it's name so you could use it to check if you are on a mac machine:

sysname <- Sys.info()["sysname"]

if(sysname == "Darwin") {
setwd("~/Google Drive/projectX") # example on mac machine
} else if(sysname == "Linux") {
setwd("~/GoogleDrive/projextX") # example on linux machine
} else if(sysname == "Windows") {
setwd("C:/Users/sweetusername/Google Drive/projectX") # example on win machine
}

Thanks to @RLave for providing the sysname value for Windows.


Alternative way is to only check based on file-paths and set the working directory to the first available directory in your list:

locations <- c("~/Google Drive/projectX",
"C:/Users/sweetusername/Google Drive/projectX",
"C:/Users/sweetusername_butslightlydifferent/Google Drive/projectX",
)

setwd(Find(dir.exists, locations))

Paths starting with tilde ~ will not be available on Windows machines - so this will also differentiate between macos and windows.

How to automate working directory change

Usually, for each project or analysis that I start I use a "config-like" R file which looks more or less like this:

.job <- list ()

## rootDir in my laptop
.job$base_data_dir <- file.path ("", "home", "dmontaner", "datos")

## rootDir in my server
##.job$base_data_dir <- file.path ("", "scratch", "datos")

In this "config" file I set the root directory where I am keeping the data in each machine. I keep a different "config" file in each machine and do not synchronize them via dropbox.

Then I start my R scripts with this line:

try (source (".job.r"))

and when I have to address any file or folder I do:

setwd (file.path (.job$base_data_dir, "raw_data"))
...
setwd (file.path (.job$base_data_dir, "results"))

Like this, if you keep the internal structure of the data directory in both machines, you are able to set the base or root dir where it is allocated and reach the data in both machines.

Also the file.path function takes care of the changes in operative system.

In the R session I call the config variable starting with a dot for it to be a hidden variable so I do not see it when I do a ls () or similar things.



Related Topics



Leave a reply



Submit