Re: [R] Automation: Batch mode or Loop?

From: jim holtman <jholtman_at_gmail.com>
Date: Fri, 28 Mar 2008 20:05:15 -0500

You can do it interactively with a script. If you know how large your output will be, you can preallocate an object and then just store the results in it. The loop would look something like this:

files <- Sys.glob("*.csv") # get names of files to process result <- numeric(length(files)) # preallocate assuming single value from each file
for (i in seq_along(files)){

    input <- read.csv(files[i]) # or however you want to read it     ... computations ...
    result[i] <- answer
}

On Fri, Mar 28, 2008 at 7:42 PM, Stropharia <stevenworthington_at_hotmail.com> wrote:
>
> R users,
>
> I've been searching around for a solution to this for a few days, any help
> would be appreciated.
>
> I have working R code (a custom function) that transforms 3D coordinate data
> into angles (between any three sets of coordinates). The code outputs a
> single scalar (an angle). For my research I will have several hundred .csv
> files containing coordinate data - each file is a set of coordinates on an
> organism. I would like to run the code on each of these files in turn and
> then output the resulting angles to a new .csv file, appending that file
> with a new scalar each time the code completes it's operation. Ideally, at
> the end I would have a new .csv file with a single column with several
> hundred scalars - which are the results of the analysis on each individual
> coordinate data file.
>
> I am using R on OS X (Leopard), ver. 2.6.2. I generally use the console.
>
> Is it possible to automate this series of operations, so that several
> hundred .csv coordinate data files placed in one folder could be
> sequentially analyzed? If so, would it be best to do this in "Batch Mode" or
> interactively using a loop? I don't understand how to run batch mode from
> the terminal on OS X (online instructions seem to be for linux or windows).
> I've read about using the Source and Sink commands, but this seems only for
> interactive use, and sink seems to output only to .txt files. I have tried
> output as write.csv and this works for a single file, but I don't understand
> how to automate it. Thanks.
>
> Steve
> --
> View this message in context: http://www.nabble.com/Automation%3A-Batch-mode-or-Loop--tp16365096p16365096.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help_at_r-project.org 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.
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
R-help_at_r-project.org 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.
Received on Sat 29 Mar 2008 - 01:09:06 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 29 Mar 2008 - 09:30:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive