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

From: Philipp Pagel <p.pagel_at_wzw.tum.de>
Date: Fri, 14 Nov 2008 18:11:50 +0100

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

______________________________________________
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 Fri 14 Nov 2008 - 17:16:30 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 Fri 14 Nov 2008 - 17: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