[ADMB Users] running admb from R using system()
John Sibert
sibert at hawaii.edu
Tue Jun 1 12:36:10 PDT 2010
Have a look at the R object .Platform. It enables you check whether R is
running under windoze. This his how we solve the problem with kftrack
package. cmd is a string containing the fully qualified path of the file
you want to execute.
sys<-function (cmd)
if (.Platform$OS.type == "windows") {
shell(cmd, invisible = TRUE)
} else {
system(cmd)
}
Of course some error checking would be nice. System (and I guess shell,
too) returns a value that can be checked so that it is possible to exit
gracefully.
On 06/01/2010 02:46 AM, Benedikt Gehr wrote:
> thanks for all the help. I simply exchanged the system() command with
> shell() command while keeping everything else equal, and now it worked
> and R doesn't crash anymore.
> Not sure what the problem was.
>
> Mark, I appreciate sending me the code but I think this code is a bit
> to advanced for me, I didn't really understand the code and how to
> apply it to my problem. I'm not that experienced but maybe I have to
> look at it for longer. but for now I think it works with shell().
>
> thanks anyway
>
> cheers
>
> Beni
>
> Mark Maunder wrote:
>>
>> Here is the whole code if you want the see the details
>>
>>
>>
>> lwmreg.admb<-function(x,y,rtype="regression",ams=1000000,gbs=100000000,cbs=100000000,maxfn=500,thetaphz=1,thetainit=1,exedr)
>>
>>
>> {
>>
>> #Modified from glmm.admb orignially written by Hans Julius Skaug
>>
>> #written by Mark maunder
>>
>> #need to put executable file in the working directory
>>
>> #This needs to be a directory with no spaces in the name
>>
>> #for example c:\test\
>>
>> #use this command to set working directory setwd("C:/test")
>>
>>
>>
>> ams<-as.integer(ams)
>>
>> gbs<-as.integer(gbs)
>>
>> cbs<-as.integer(cbs)
>>
>> nn <- nrow(x)
>>
>> p <- ncol(x)
>>
>>
>>
>> if (!(nn == length(y)))
>>
>> stop("x and y not same length")
>>
>> if (missing(exedr) )
>>
>> stop("Need to give directory for execuitable")
>>
>> if (!file.exists(paste(exedr,"lwmreg.exe",sep="")))
>>
>> stop("Cant find execuitable")
>>
>>
>>
>> cmdoptions = paste("-maxfn",maxfn,"-ams",ams,"-gbs",gbs,"-cbs",cbs)
>>
>> file_name = "lwmreg"
>>
>>
>>
>> if(p==1) phz<--1
>>
>> else phz<-c(-1,rep(1,p-1))
>>
>>
>>
>> if(rtype=="regression") rt<-1
>>
>> else
>>
>> {
>>
>> if (rtype=="average") rt<-0
>>
>> else stop("rtype= regression or average")
>> }
>>
>>
>>
>>
>> dat_list = list(rtype = rt, p = p,phz=phz, thetaphz=thetaphz, n =
>> nn,x = x, y = y)
>>
>> pin_list = list(theta=thetainit,lnlambda=rep(0,p))
>>
>>
>>
>> dat_write(file_name, dat_list)
>>
>> pin_write(file_name, pin_list)
>>
>> std_file = paste(file_name, ".std", sep = "")
>>
>> file.remove(std_file)
>>
>> if (.Platform$OS.type == "windows") {
>>
>> shell(paste(exedr, file_name,
>>
>> ".exe", " ", cmdoptions, sep = ""), invisible = TRUE)
>>
>> }
>>
>> else {
>>
>> system(paste("cp ", exedr,
>>
>> file_name, " .", sep = ""))
>>
>> system(paste("./", file_name, " ", cmdoptions, sep = ""))
>>
>> unlink(file_name)
>>
>> }
>>
>> if (!file.exists(std_file))
>>
>> stop("The function maximizer failed")
>>
>>
>>
>> out<-read.fit(file_name)
>>
>> out<-c(out,reptoRlist(paste(file_name, ".rep",sep="")))
>>
>>
>>
>> if (abs(out$maxgrad) >= 0.001)
>>
>> warning("Proper convergence could not be reached gradiant >=
>> 0.001")
>>
>> return(out)
>>
>> }
>>
>>
>>
>>
>>
>>
>>
>> Mark Maunder
>>
>> Head of the Stock Assessment Program
>>
>> Inter-American Tropical Tuna Commission
>> 8604 La Jolla Shores Drive
>> La Jolla, CA, 92037-1508, USA
>> Tel: (858) 546-7027
>> Fax: (858) 546-7133
>> mmaunder at iattc.org <mailto:mmaunder at iattc.org>
>> http://www.fisheriesstockassessment.com/TikiWiki/tiki-index.php?page=Mark+Maunder
>>
>>
>>
>>
>> Visit the AD Model Builder project at
>> http://admb-project.org/
>>
>>
>>
>> See the following website for information on fisheries stock assessment
>>
>> http://www.fisheriesstockassessment.com/
>>
>>
>>
>> *From:* Nathan Taylor [mailto:n.taylor at fisheries.ubc.ca]
>> *Sent:* Monday, May 31, 2010 10:00 AM
>> *To:* Mark Maunder; Benedikt Gehr; users at admb-project.org
>> *Subject:* RE: [ADMB Users] running admb from R using system()
>>
>>
>>
>> It's much less elegant than Mark's code because directories and model
>> names are hard wired but for multiple instances such as multiple mcmc
>> chains, i've been successful using system() as shown below (64 bit
>> windows, R version 2.11.0).
>>
>> setwd("dirname1")
>> system("model
>> -commandoptions",wait=F,show.output.on.console=F,invisible=F)
>>
>> setwd("dirname2")
>> system("model
>> -commandoptions",wait=F,show.output.on.console=F,invisible=F)
>>
>>
>> It's been a while since i set this up but remember having some issues
>> with the show.output.on.console option using system(). The default
>> for this option is show.output.on.console = TRUE.
>>
>> good luck.
>>
>> NG
>>
>> -----Original Message-----
>> From: users-bounces at admb-project.org on behalf of Mark Maunder
>> Sent: Mon 31/05/2010 9:08 AM
>> To: Benedikt Gehr; users at admb-project.org
>> Subject: Re: [ADMB Users] running admb from R using system()
>>
>> Try this
>>
>> if (.Platform$OS.type == "windows") {
>> shell(paste(exedr, file_name,
>> ".exe", " ", cmdoptions, sep = ""), invisible = TRUE)
>> }
>> else {
>> system(paste("cp ", exedr,
>> file_name, " .", sep = ""))
>> system(paste("./", file_name, " ", cmdoptions, sep = ""))
>> unlink(file_name)
>> }
>>
>>
>>
>> Mark Maunder
>> Head of the Stock Assessment Program
>> Inter-American Tropical Tuna Commission
>> 8604 La Jolla Shores Drive
>> La Jolla, CA, 92037-1508, USA
>>
>> Tel: (858) 546-7027
>> Fax: (858) 546-7133
>> mmaunder at iattc.org
>> http://www.fisheriesstockassessment.com/TikiWiki/tiki-index.php?page=Mar
>> k+Maunder
>>
>> Visit the AD Model Builder project at
>> http://admb-project.org/
>>
>> See the following website for information on fisheries stock assessment
>> http://www.fisheriesstockassessment.com/
>>
>> -----Original Message-----
>> From: users-bounces at admb-project.org
>> [mailto:users-bounces at admb-project.org] On Behalf Of Benedikt Gehr
>> Sent: Monday, May 31, 2010 1:44 AM
>> To: users at admb-project.org
>> Subject: [ADMB Users] running admb from R using system()
>>
>> Hi
>>
>> I'm trying to run an admb model from R by using the system () command.
>> The admb model runs fine when running it from the admb command line or
>> when using emacs. However when I try it with system() then R crashes
>> every time.
>> And I tried using the R command line and RGui and in both it crashes. I
>> also tried it from different computers.
>>
>> What I do is I first set the directory where I keep the admb template
>> file with the corresponding admb data file and then invoke the system
>> command either step by step or directly. I can build the model but when
>> executing the model.exe file R crashes.
>>
>> ##########################
>> #setting the working directory
>> setwd("Documents and Settings\\Beni User\\Desktop\\Transfer\\Model
>> Fournier\\admb models\\simulated data\\stochastic\\pooled age classes")
>>
>> #building the model -> this works fine
>> system('makeadm stocpool')
>>
>> #running the model -> makes R crash
>> system('stoc.exe')
>> ###########################
>> #or directly like this:
>> setwd("Documents and Settings\\Beni User\\Desktop\\Transfer\\Model
>> Fournier\\admb models\\simulated data\\stochastic\\pooled age classes")
>> system("./stocpool") -> makes R crash right away
>>
>>
>> Does anyone know why this happens and how I can make this work?
>>
>> Thanks a lot for the help!!
>>
>> cheers
>>
>> Beni
>>
>>
>>
>>
>>
>> _______________________________________________
>> Users mailing list
>> Users at admb-project.org
>> http://lists.admb-project.org/mailman/listinfo/users
>> _______________________________________________
>> Users mailing list
>> Users at admb-project.org
>> http://lists.admb-project.org/mailman/listinfo/users
>>
> _______________________________________________
> Users mailing list
> Users at admb-project.org
> http://lists.admb-project.org/mailman/listinfo/users
>
--
John Sibert
Emeritus Researcher, SOEST
University of Hawaii at Manoa
Visit the ADMB project http://admb-project.org/
More information about the Users
mailing list