[R] passing R array to Fortran subroutine
Timothy W. Hilton
twh142 at psu.edu
Thu Feb 14 17:21:02 CET 2008
Hello,
I am trying to run a piece of Fortran code from R, and I am having trouble passing an array to the fortran subroutine. My attempts to pass an array into the fortran are producing memory errors, and I cannot find an example that performs the task. I have looked at "Writing R Extensions", and understand in concept what needs to happen, but all the examples are written in C.
Below are my fortran subroutine, the R function that calls it, and the R output.
====== foo.F90 ======
SUBROUTINE testRwrapper(n)
DOUBLE PRECISION :: n(:)
INTEGER :: i
! answer = factorial(n)
do i=1,3
n(i) = n(i) + i/1.0
end do
END SUBROUTINE testRwrapper
====== test.R ======
foo <- function(n) {
# If the library hasn't been loaded yet, load it
if (!is.loaded('testRwrapper')) { dyn.load('foo.dylib') }
returned_data = .Fortran('testRwrapper',
n=as.double(n))#,
# Return the value of the result parameter
return(returned_data$n)
}
====== R result ======
> source('test.R')
> foo(c(1.1, 2.2, 3.3, 4.4))
*** caught segfault ***
address 0x3ff19999, cause 'memory not mapped'
Traceback:
1: .Fortran("testRwrapper", n = as.double(n))
2: foo(c(1.1, 2.2, 3.3, 4.4))
Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
=========================
I have tried various combinations of pointers in the fortran subroutine. I can pass a single value without a problem. That is, eliminating the do loop from foo.F90 and making n a scalar works. Could someone suggest the proper Fortran syntax, or point me toward an F90/R example?
Many thanks,
Tim
More information about the R-help
mailing list