[R] loop, pipe connection, quote/unquote

Gabor Grothendieck ggrothendieck at gmail.com
Wed Aug 2 14:38:54 CEST 2006


Try something along these lines assuming that the current
directory is \Program Files\R\R-2.3.1pat.  Note use
of paste to create the command line to pass to pipe:

# search for indicated string in each of the files and
# for each match output the file name
Files <- c("CHANGES", "COPYING", "NEWS", "NEWS")
for(f in Files) print(readLines(pipe(paste("findstr /m out-of-range", f))))


On 8/2/06, Marco Grazzi <grazzi at sssup.it> wrote:
> Hi all,
>
> I have the following problem.
> Inside R, I am trying to run a loop on several files.
> The data are stored in these files in a peculiar way, thus, at the same time I load the data, I would like to invoke a utility. I do this with "pipe". (The utility I am using is gbget from the package gbutils. It works correctly from shell, and it is not the problem.)
> The problem is that from within pipe I do not know how to have the loop running. I guess I should manage to unquote the quotation mark inside the pipe for the loop to run, but I do not how to do it.
> In the following examples, for instance, I would like pipe to interpret the "i" as the file.names of the loop specified above.
>
> # specify the files on which I want to have the loop running
> file.names <- system("ls ???.gz", intern=TRUE)
> # Start loop
> for(i in file.names){
>        dati     <- read.table(pipe("gbget  'i[160](1)' '[37](1)' '[145](1)' |gbget '()D' "))
>        # [...] some statistical analysis follows [...]
> }
>
> Thanks for your help (hoping I manged to be enough clear),
> marco
>
>
> --
>
>
> Marco Grazzi
>
> -----
>
> PhD Candidate in Economics and Management
> LEM-Sant'Anna School of Advanced Studies
> Piazza Martiri della Liberta', 33
> 56127 Pisa, Italy
> Tel. +39-050-883365 Fax +39-050-883344
> Web site: https://mail.sssup.it/~grazzi
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>



More information about the R-help mailing list