Re: [R] Counterpart for Matlab's 'feval'?

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Fri 24 Jun 2005 - 19:34:32 EST

On Fri, 24 Jun 2005, Fredrik Thuring wrote:

> I've just begun writing a program that searches for the minimum of a
> function with golden section search. In order to do this in a nice way I
> need a function that takes a function name and an argument and returns the
> function value for that argument, i .e just like Matlab's 'feval'. Is
> there any?

Really? Take a look at the interface for optimize(): in R you almost always want to pass the function not its name.

You could use

feval <- function(fn, x) get(fn)(x)

but it would be rather inefficient to keep looking up the function (and there are scoping issues to consider here).

You could also use do.call(fn, list(x)), but that is even less direct.

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@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
Received on Fri Jun 24 19:39:13 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:02 EST