Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives to system() in R for calling sed, rsync, ssh etc.: Do functions exist, should I write my own, or am I missing the point?

Tags:

bash

r

Recently, I found the base::files commands. Along with other commands like getwd, write.lines, file.show, dir, etc. there seem to be a number of R equivalents of bash functions.

I have also written some functions in R that streamline calls to ssh and rsync through system.

for example:

rsync <- function(from, to){
  system(paste('rsync -outi', from, to, sep = ' '), intern=TRUE)
}

But before I go to much further with this, I have a few questions:

  • does R already have built in commands for common shell programs, if so, where can I find them?
  • if not, are there reasons to avoid writing my own functions?
  • is there a better alternative to the approach outlined in the rsync example above?
  • would a collection of such functions warrant a package?
like image 843
David LeBauer Avatar asked Aug 24 '11 00:08

David LeBauer


2 Answers

does R already have built in commands for common shell programs, if so, where can I find them?

There are some function like grep that mimic shell progams. Search for them as you would any other function – the names are often the same.

if not, are there reasons to avoid writing my own functions?

No obvious problems.

is there a better alternative to the approach outlined in the rsync example above?

Looks good, but you need to be very careful about checking user input if things are passed to the shell.

would a collection of such functions warrant a package?

Absolutely. Go for it.

like image 133
Richie Cotton Avatar answered Oct 12 '22 23:10

Richie Cotton


I started to go down that route with wrapping git functions for devtools, but eventually realised what I needed was:

bash <- function() system("bash")

with a bit of wrapping to make sure I ended up in the right directory.

like image 40
hadley Avatar answered Oct 12 '22 22:10

hadley