See inline. On 20-10-2012, at 17:18, paulfjbrowne <paulfj.bro...@gmail.com> wrote:
> Hello, > > I have been trying to use a collection of Fortran subroutines to return a 2D > array of calculated values to my R code, calling a Fortran wrapper > subroutine from R. I've done this successfully before with C & C++ code. > > The Fortran wrapper subroutine which is to be called by R takes a set of > input arguments & then should return an array of 2 columns & the specified > number of rows. > > I've tested the wrapping subroutine from another Fortan main program & it > does work as expected, so the Fortran works, but my problem has been with > the correct syntax for retrieving the output double array from within R. > > The wrapping Fortran subroutine is defined; > > subroutine xypos_parallax_r(k,year,ra,dec,ti,t0,tE,alpha,u0,piee,pien,y) > > k is the number of rows & y an assumed size double array, y(2,*), the other > arguments are other input variables for the Fortran. > > I've tried putting the Fortran function call inside an R function, with this > syntax; > > #Source positions in lens plane under annual parallax > xypos_parallax <- function(year,ra,dec,ti,model_par) { > if (!is.loaded('xypos_parallax_r')){ > dyn.load('parallax.so')} > returned_data = .Fortran('xypos_parallax_r', > as.integer(length(ti)), > as.integer(year), > as.double(ra), > as.double(dec), > as.double(ti), > as.double(model_par$t0), > as.double(model_par$tE), > as.double(model_par$alpha), > as.double(model_par$u0), > as.double(model_par$piee), > as.double(model_par$pien), > as.double(array("double",dim=c(2,length(ti)))) )[[12]] > } > Why as.double(array("double",dim=c(2,length(ti))? You are passing a character array to your fortran subroutine. Use matrix(0,nrow=2,ncol=length(ti)) which will be a matrix of double precision's. Berend > The input arguments to the Fortran call include the number of rows of the > output array, with the final argument in the .Fortran call intended to be > the output array itself. ti is a vector of same length as the number of rows > needed in the output array. > > Testing this R function calling the Fortran code returns; > > Error in xypos_parallax(year, ra, dec, ti, model_par) : > NA/NaN/Inf in foreign function call (arg 12) > In addition: Warning message: > In xypos_parallax(year, ra, dec, ti, model_par) : > NAs introduced by coercion > > The Fortran code does work as intended, so the problem must be with how I've > written the R function making the Fortran call, but I can't see where I've > gone wrong. > > Would anyone have any idea, or experience with returning multi-dimensional > arrays from external code into R? > > Thanks in advance > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Trouble-returning-2D-array-into-R-from-Fortran-tp4646862.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.