[R] Calling external file

Michael Dewey lists at dewey.myzen.co.uk
Sat Oct 3 14:47:01 CEST 2015


You did put the declaration of the function fn into the file you are 
sourcing, didn't you?

If it were me I would
1 - make fn a parameter of max.calls
2 - use the ellipsis ... so I could pass other arguments in to MaxLike
3 - fix the errors I got from making it a package. It does not lie when 
it tells you things are undefined and, who knows, they may need to be 
defined.

On 03/10/2015 09:47, Steven Yen 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

-- 
Michael
http://www.dewey.myzen.co.uk/home.html



More information about the R-help mailing list