[R] Writing R package that call Fortran codes

Eduardo M. A. M.Mendes emammendes at gmail.com
Wed Jun 15 23:55:56 CEST 2016


Hi

Have you tried to load and run the fortran code using just a wrapper function in R?   I do that as the first step in order to build a package.  

Example:   fortran sources -> rk4_mod_r.f90 ,derive_henonheilles.f90, poincare_section.f90

a) I use R CMD SHLIB rk4_mod_r.f90 ,derive_henonheilles.f90, poincare_section.f90 - o poincare_section_henonheilles_rk4.so 
b) Then I write a wrapper function in R, poinc_section_henonheilles.R
...
 dyn.load("poincare_section_henonheilles_rk4.so")
  
  out<-.Fortran("section_crossing",
                h=as.numeric(h),
                nphas=as.integer(nphas),..
...
c) and call the function as usual.

Please note that the function called by .Fortran is the name of the subroutine within poincare_section.f90 and not the filename.

I take the opportunity to thank R-developers for making the calling of C and Fortran in R very easy.   

I hope this helps.

regards

Ed

PS.  If you need an example of a package using Fortran90, please check https://github.com/emammendes/mittagleffler <https://github.com/emammendes/mittagleffler>



> On Jun 15, 2016, at 5:20 PM, Kodalore Vijayan, Vineetha W <vwkv13 at mun.ca> wrote:
> 
> Hi,
> 
> I'm trying to write an R package that calls a Fortran subroutine on my  Mac
> os x El Capitan with Xcode 7 and gfortran 6.1, R 3.3.0.   I can build and
> load the library but when I try to use it in R I get this error:
>> library(NEpidemic)
>> random_epi(variable_names)
> 
> Error in .Fortran("random_epi", : "random_pi" not resolved from current
> namespace (NEpidemic).
> 
> Then I  tried adding useDynLib(random_epi.f95) in the NAMESPACE file,
> additional to useDynLib(NEpidemic). After doing that I couldn't build the
> package and it gave me another error:
> 
> Error in library.dynam(lib, package, package.lib) :
>  shared object ‘random_epi.so’ not found
> Error: loading failed
> Execution halted
> ERROR: loading failed
> 
> When I checked my src folder, there is only random_epi.o file.  How can I
> fix this issue? Any help would be much appreciated. I'm vey new to both R
> and Fortran coding, especially in package building.
> 
> Thanks in advance!
> Vineetha
> 
> 	[[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.


	[[alternative HTML version deleted]]



More information about the R-help mailing list