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!
Hello, Anis, can't say anything about your 1st question, but w.r.t. the 2nd ?file.path should do what you want. Regards -- Gerrit On Tue, 2 Apr 2013, Asis Hallab 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.
> -----Original Message----- > 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?No - at least, not generally, for the more or less obvious reason that *nix tools are not generally present on Windows. But if you have a system call with _exactly_ the same name and options it should work (windows isn't case-sensitive for system calls/tools; unix is) Under windows, too, constructing batch files with suitable names (eg 'ls.bat') can go some way to making unix-like things at least run windows equivalents. But you'd have to do a lot of work to get identical output.> 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?R already does that automatically. ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
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.