[R] sample codes for writting a function with unknown number of arguments

Duncan Mackay mackay at northnet.com.au
Fri Mar 21 22:13:39 CET 2008


At 04:28 22/03/2008, you wrote:
>Hi,
>
>I need to scan some files, and then do a boxplot graph of them.  I also 
>save the output from the boxplot function, so I can process the data later.
>
>For example, I need to scan 2 (it can be x, 0<x) files here.
>A<-scan(F1)/100;
>B<-scan(F2)/100;
>
>Now let's try graph it. N1 is the label for F1, and so on.
>bitmap(file=Name, type="png16m")
>STATS<-boxplot(A,B,c(A,B),col="red", main="MyMain", names=c(N1, N2, 
>"All"), xlab="Hardware", ylab="milliseconds")
>dev.off();
>
>The problem is I don't really know what I will have at runtime.  For 
>example, I may have to scan 3 or 4 files at runtime.  Then, I will have to 
>pass different arguments to boxplot, like
>
>STATS<-boxplot(A,B,C,c(A,B,C),col="red", main="MyMain", names=c(N1, N2, 
>N3, "All"), xlab="Hardware", ylab="milliseconds")
>
>
>The function should look like:
>process<-function(Name, F1, F2...N1,N2...)
>
>How should I write a function that is flexible enough to handle this 
>situation?  Thank you in advance.
>
>
>
>
>
>         [[alternative HTML version deleted]]
>
>______________________________________________
>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.

Here is something to think about to get you started

   flist <-  list.files(path    = "D:/R/Rfiles/",
                        pattern = ".+[.][rR]",
                        full.names = TRUE)

   # Source the files:
   for (j in 1:length(flist)) {

     your commmands

   }

   # cleanup
   rm(flist)

Regards

Duncan Mackay

Department of Agronomy and Soil Science
University of New England
ARMIDALE NSW 2351
email: dmackay9 at pobox.une.edu.au
home  mackay at northnet.com.au



More information about the R-help mailing list