R (programming language)/Tutorials/Connecting Fortran and R
This is to briefly describe the process of connecting the programming languages: Fortran and R. At least basic knowledge of both programming languages is assumed here.
Basic requirements
[edit | edit source]- R - R should be installed on your computer. In this tutorial R version 2.10.1 (2009-12-14) was used on a Windows XP machine. For information about setup visit the installation how-to.
- The Fortran program - You have to have a program source code, that you want to use in R. Please note, that R is able to call only subroutines. In case of functions a so called "wrapper subroutine" should be used. You find more about this here.
Sample program
[edit | edit source]Copy the program below to a text file, and name it: multiply.f
c sample subroutine to include for R subroutine multiply(ax,bx,cx) implicit none integer ax,bx,cx cx=ax*bx end
Making a dll
[edit | edit source]In order to connect these two programming languages you need to compile the Fortran code. You will need a Fortran compiler for this one. The following example assumes a gfortran compiler.
To make a dll (Dynamic-link library) from the source code, open the command prompt, navigate to the directory with the source code and type:
gfortran -shared -o multiply.dll multiply.f
Where multiply.f is the name of the file which contains the source code.
In Lahey Fortran compiler you should use the -dll switch instead of -shared.
Inside R
[edit | edit source]- Open R. The simplest case is to move the newly created dll file to the working directory of R. (Note: type getwd() to find out your working directory.)
- You have to load the dll file to R. For this use dyn.load() like this:
dyn.load("d:/work/R_work_files/general/multiply.dll")
Where d:/work/R_work_files/general/multiply is the path to the dll file.
- To test if it was loaded correctly, type:
is.loaded("multiply")
If you get "[1] TRUE", you are ok. If not, find out the cause of it (some hints are here).
- The last thing you have to do is call the compiled Fortran subroutine from R using ".Fortran()" like this:
a=5 b=2 .Fortran("multiply",as.integer(a),as.integer(b),c=integer(1))
and you will get:
[[1]] [1] 5 [[2]] [1] 2 $c [1] 10
Where $c equals 10, which is exactly 5 times 2 .
Move to R function
[edit | edit source]The process below is optional, but it makes the usage of the "final product" much more simple and user friendly.
Source code
[edit | edit source]- Open a new script, paste the following code there and save it as fortranConnection.r
# script to test the link between Fortran and R star = function(a,b){ # res holds the results for the Fortran call res =.Fortran("multiply",as.integer(a),as.integer(b),c=integer(1)) return(res$c) }
Note: As an outcome you are interested just in the value of c, so the return value will be res$c and not the whole res.
- In R open Files> Source R code..., find fortranConnection.r and source it in.
- Use your brand new R function as:
star(5,2)
Which leads to not-so-surprising result:
[1] 10
Special cases
[edit | edit source]- Some compilers leave a trailing underscore after the subroutine name inside the dll file. If the is.loaded test above gives a FALSE for you, try this as one of the possible solutions:
is.loaded("multiply_")
- Also other compiler specific issues might come in, such as for Lahey the following line could be inserted into the Fortran source code:
DLL_EXPORT mySubroutine
Where mySubroutine is the subroutine name to be exported to a dll file. More information here.
External links
[edit | edit source]For further reading about the topic: