[R] How to do platform independent system calls?

annoporci annoporci at gmail.com
Tue Apr 2 19:59:04 CEST 2013


I'm new to R and learning so I probably don't have much of value to share,
but I do use Windows and kUbuntu interchangeably and need to share code
across these two platforms, so let me tell you what I've found -- it may
or may
not help you.

Most of the R packages can be used on any platform. So you don't usually
need to worry about your syntax being os-specific. But there are
exceptions. When I run into exceptions, i usually use a conditional
statement (see below).

I find it very useful to store commonly used paths into an environment
file. I have an environment file for each OS. These are my settings. I
comment out the Linux section for Windows and vice versa:

---------------------
# Windows .Renviron
      R_LIBS="C:/Program Files/RStudio/R/library"
      R_LIBS_USER="C:/R/library"
      R_USER="C:/R"
      R_DOC_DIR="C:/R"
      HOME="C:"

# Linux Renviron.site
# R_LIBS_USER="~/R/library"
# R_USER="~/R"
# R_DOC_DIR="~/R"
# HOME="/home/patrick" # may not be needed, check your system
----------------------------

You'll need to figure out where to store them on your system.

Having defined my home directories this way, relative paths can be used.
If you need to create a specific path, one approach is to use:

paste0(.Platform$file.sep,"directoryname")

I am vaguely aware that there contexts in which it doesn't work (maybe
DOS), so you'll need to test if you can construct the file path using the
paste0 function and Platform$file.sep

I typically write conditional statements for things that are
platform-specific. To illustrate with a simple example: to define the
current directory, you can do this:

### Define directories, note the path does not end with a slash /
      if(.Platform$OS.type == "windows"){
        currentdir <- "c:/R"
      } else {
      currentdir <- "~/R"}
      setwd(currentdir)
      rm(currentdir)

These may not be great but they have worked for me so far.

Patrick.



On Tue, 02 Apr 2013 17:42:01 +0800, Asis Hallab <asis.hallab at gmail.com>
wrote:

> Dear R experts,
>
> I hope everyone has had a happy easter break.
>
> Recently my work included writing R function that need to call external  
> tools.
> I did this using R's system function for example:
>
> system( paste( 'tool', '-input', path_to_input, '-output',
> path_to_output, '-other_switch', some_val ) )
>
> I have two question about this:
>
> 1) Is there a way to implement such calls to external tools, so they
> become platform independent? I mean, so that these calls will work
> both on a *nix and a Windows system?
>
> 2) Is there a way to generate platform independent paths? So that
> "path/2/input.tbl" on *nix systems becomes "path\2\input.tbl" on a
> Windows system?
>
> Your help will be much appreciated!
> Kind regards!
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide  
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


-- 
Patrick Toche.



More information about the R-help mailing list