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

Gabor Grothendieck ggrothendieck at gmail.com
Fri Aug 24 19:05:51 CEST 2007


On 8/24/07, Dirk Eddelbuettel <edd at debian.org> wrote:
> On Fri, Aug 24, 2007 at 10:57:46AM -0400, Duncan Murdoch wrote:
> > 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.
>
> Hm, wishful thinking must have gotten the better of me then. Sorry for
> spreading misinformation about the capabilities of that other OS.

This works for me on Windows:

> tab <- read.table(pipe("lynx --nolist --dump http://cran.r-project.org/src/contrib/ | findstr tar.gz"), as.is = TRUE)
> head(tab[3:5])
                               V3          V4    V5
1             ADaCGH_1.3-1.tar.gz 14-May-2007 12:04
2                  AIS_1.0.tar.gz 31-Jul-2007 16:38
3             AMORE_0.2-10.tar.gz 11-Apr-2007 10:17
4               ARES_1.2-2.tar.gz 19-Mar-2007 20:53
5 AcceptanceSampling_0.1-1.tar.gz 07-Jul-2007 20:46
6           AdaptFit_0.2-1.tar.gz 04-Aug-2007 09:51



More information about the R-help mailing list