Você está na página 1de 4

How to use R/Tutorials/Connecting Fortran and R

How to use R/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
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 [1].

Sample program
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
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.

How to use R/Tutorials/Connecting Fortran and R

Inside R
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 [2]). 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
The process below is optional, but it makes the usage of the "final product" much more simple and user friendly.

Source code
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)

How to use R/Tutorials/Connecting Fortran and R Which leads to not-so-surprising result: [1] 10

Special cases
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 [3].

External links
For further reading about the topic: Calling NAG Fortran Routines from R [1] Using external compilers with R [4] Using Fortran with R in Windows [5]

References
[1] [2] [3] [4] [5] http:/ / www. nag. co. uk/ numeric/ RunderWindows. asp http:/ / www. stats. uwo. ca/ faculty/ murdoch/ software/ compilingDLLs/ index. html#badname http:/ / www. canaimasoft. com/ f90vb/ onlinemanuals/ usermanual/ TH_64. htm http:/ / www. stats. uwo. ca/ faculty/ murdoch/ software/ compilingDLLs/ http:/ / www. stats. uwo. ca/ faculty/ murdoch/ software/ compilingDLLs/ fortran. html

Article Sources and Contributors

Article Sources and Contributors


How to use R/Tutorials/Connecting Fortran and R Source: http://en.wikiversity.org/w/index.php?oldid=1145568 Contributors: Gbaor, 1 anonymous edits

Image Sources, Licenses and Contributors


image:Smiley.svg Source: http://en.wikiversity.org/w/index.php?title=File:Smiley.svg License: Public Domain Contributors: Pumbaa80

License
Creative Commons Attribution-Share Alike 3.0 //creativecommons.org/licenses/by-sa/3.0/

Você também pode gostar