[R] the number of the arguments of function is not sure

Philipp Pagel p.pagel at wzw.tum.de
Fri Nov 14 18:11:50 CET 2008


On Fri, Nov 14, 2008 at 05:05:32PM +0100, Chang Jia-Ming wrote:
> I want to write a function to plot a picture for each inputting file like
> the following example.
> plot_test <- function(f1,f2)
> {
>   plot_file(f1);
>   plot_file(f2);
> }
> 
> However, the above function just could plot two input file.
> If the number of input file is not sure, it could be 2, 3 or more.
> plot_test(f1, f2)
> plot_test(f1, f2, f3) .... or more
> I would like to run function, for instance "plot_file", for each input file.
> How could I do?
> Is there any solution like argv and argc in C to read arguments in R?


I would make the function accept only one argument which would be
a vector of filenames:

foo <- function(files) {
	for (filename in files) {
		do_something(filename)
	}
}

foo( c('File1', 'AnotherFile', 'File3') )


Alternatively, you could use "...":

foo <- function(...) {
	for (filename in list(...)) {
		do_something(filename)
	}
}

foo( 'File1', 'AnotherFile', 'File3')


cu
	Philipp


-- 
Dr. Philipp Pagel
Lehrstuhl für Genomorientierte Bioinformatik
Technische Universität München
Wissenschaftszentrum Weihenstephan
85350 Freising, Germany
http://mips.gsf.de/staff/pagel



More information about the R-help mailing list