[R] Trouble returning 2D array into R from Fortran

paulfjbrowne paulfj.browne at gmail.com
Sat Oct 20 17:18:19 CEST 2012


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

#Source positions in lens plane under annual parallax
xypos_parallax <- function(year,ra,dec,ti,model_par) {
  if (!is.loaded('xypos_parallax_r')){
  returned_data = .Fortran('xypos_parallax_r',
                     as.double(array("double",dim=c(2,length(ti)))) )[[12]]

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.

More information about the R-help mailing list