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

Gabor Grothendieck ggrothendieck at gmail.com
Fri Aug 24 22:41:02 CEST 2007


On 8/24/07, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
> On 8/24/2007 1:05 PM, Gabor Grothendieck wrote:
> > 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)
>
> Which R version is that?  It doesn't work for me in Rgui, though it does
>  in Rterm, both R-devel versions.
>

I am using Rgui

> R.version.string
[1] "R version 2.5.1 (2007-06-27)"

on Windows XP.  "lynx --version" gives:

Lynx Version 2.8.5rel.1 (04 Feb 2004)
libwww-FM 2.14FM, SSL-MM 1.4.1, OpenSSL 0.9.7d-dev
Compiled by Borland C++ (Feb  5 2004 17:35:58).

Copyrights held by the University of Kansas, CERN, and other contributors.
Distributed under the GNU General Public License.
See http://lynx.isc.org/ and the online help for more information.

See http://www.moxienet.com/lynx/ for information about SSL for Lynx.
See http://www.openssl.org/ for information about OpenSSL.



More information about the R-help mailing list