[R] It is possible to use a Shell command inside a R script?

Duncan Murdoch murdoch at stats.uwo.ca
Fri Aug 24 16:57:46 CEST 2007

On 8/24/2007 10:33 AM, Dirk Eddelbuettel wrote:
> On Fri, Aug 24, 2007 at 08:32:00AM -0400, Duncan Murdoch wrote:
>> On 8/24/2007 6:58 AM, Ronaldo Reis Junior wrote:
>> > Hi,
>> > 
>> > It is possible to use a shell command inside a R script?
>> > 
>> > I'm write a R script and I like to put somes shell commands inside to R. 
>> > Somethink like: convert fig01.png fig01.xpm or sed ..., etc.
>> The details and available functions depend on the platform, but you want 
>> to look at ?system, ?shell, and/or ?shell.exec.  (These all exist in 
>> Windows; on Unix-alikes, you probably won't have the latter two.)
> Don't forget pipes. 
> R's ability to consistently work on connections that may be local
> files, remotes files, program output, ... is a true treasure (and
> thanks and credits to, I believe, Brian Ripley to make it so).
> Eg you can do this 
>   OD <- read.table(pipe("links -dump http://cran.r-project.org/src/contrib/ | awk '/tar.gz/ {print $3, $4}'"), header=FALSE, col.names=c("file", "date"))
> to get files and dates of files on CRAN.   
> As I recall, this also works on that other operating system, provided
> you do all the legwork of installing other tools, setting PATHs etc
> to provide what works out of the box on the supposedly unfriendlier OS.

The pipe command you list doesn't work in Windows.  I'd guess this is 
because the pipe syntax "|" within the command is unsupported:  it tries 
to execute "links", with the rest of the line passed as arguments.  But 
I haven't traced through to check on this.

Duncan Murdoch

More information about the R-help mailing list