[R] Calling external file

Steven Yen syen04 at gmail.com
Sun Oct 4 00:56:25 CEST 2015


Thanks Bill. Simplified content of max.calls.R (with repeated calls to
maxLik removed) are shown below in the message. No, fn does not exist in
the environment. I call a routine (say probit.R compiled into a library) to
use maxLik. Inside this routine,
1. In probit.R. likelihood function is defined yet in another nested
routine;
2. Function "max.calls" is also nested in that  probit.R;
Then, a call to max.calls works.

What I am trying to accomplish is, instead of inserting the identical
function (or set of lines) in every routine like probit.R, I like to either
compile max.calls.R or source it from inside probit.R. Thanks.


On Sat, Oct 3, 2015 at 4:47 AM, Steven Yen <syen04 at gmail.com> wrote:

> Hi
> I collect a list of calls to a package in a function (routine) so that I
> do not need to repeat the same sets of codes from program to program. In
> the following, inserting the function into each program works. Then, I
> place the function elsewhere in a PC folder, and include in with a 'source'
> command. This does not work; it complains about a function (fn below) not
> defined.
> Compiling the function into a library file does not work either (with all
> sorts of error messages saying this and that not defined).
> Steven Yen
>
> fn <- function(beta){
>   f<-... (define f in this routine)
> return(f)
> }
>
> max.calls<-function(method){
> # *******************************************************
> # Call maxLike with alternative algorithms
> # *******************************************************
>   many<-c("NR","BFGS","BFGSR","BHHH","SANN","CG","NM")
>   if (method %in% many){
>     ML<-maxLik(logLik=fn,grad=NULL,hess=NULL,start,
>                method,print.level,constraints=NULL, ...)}
> return(ML)
> }
> # This works:
> ML<-max.calls(method)
>
> # Does not work:
> source("z:\\R\\yenlib\\lib\\max.calls.R")
> ML<-max.calls(method)
>
> Error: object 'fn' not found
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list