Skip to content
Prev 361755 / 398506 Next

Writing R package that call Fortran codes

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>