How to call a Fortran program from R

心不动则不痛 提交于 2020-08-25 07:09:40

问题


I am totally new to Fortran and well versed with R. I was handed down a huge Fortran program with about 30 subroutines and about 15 functions and many other lines of code. I was told that I needed to call the Fortran program from R. I have been searching online for ways to create this bridge between R and Fortran with very little success. I can successfully execute the Fortran exe file from the command line and create the desired outputs. The fortran file is called "FortFish.f"

A question:

From R, Do I call the Fortran program or do I have to call Fortran functions and subroutines separately?

From R, Do I call the entire Fortran program like this?: R CMD SHLIB FortFish.f and then use: dyn.load("FortFish.so")

If I cannot run the entire Fortran program at once, I will post a couple of small fortran functions and subroutines upon request. Does anyone have a running example of using R and Fortran that can share?

My Fortran code is extremely large, otherwise, I would post it here. Thank you.


回答1:


I see three possibilities:

  1. You compile the Fortran program separately, and call it with R function system(). You will have to pass data through files, in a format that this program can read.

  2. You compile a DLL that you load from R with dyn.load(), then you call a Fortran function with .Fortran(). You can easily pass numeric data (scalars, vectors or arrays), but string data is more difficult to handle. And arrays are copied.

  3. This mechanism to call a DLL function is considered too simplistic and now .Call() is prefered, but to use .Call() you would have to write C wrappers.

I will give an example of the second possibility. Consider a subroutine in Fortran that evaluates a polynomial by Horner's algorithm:

subroutine horner(n, a, x, y)
    implicit none
    integer :: n, i
    double precision :: a(n), x, y

    y = a(n)
    do i = n - 1, 1, -1
        y = y * x + a(i)
    end do
end subroutine

Compile from the command line with:

R CMD SHLIB horner.f90

To call it from R:

dyn.load("horner.dll")

horner <- function(a, x) {
  .Fortran("horner", as.integer(length(a)), a, x, y=0)$y
}

horner(c(-2, 0, 1), 1.414)

If you want your Fortran subroutines to print something to RStudio console, you need to do (at least on Windows):

Sys.unsetenv("GFORTRAN_STDOUT_UNIT")
Sys.unsetenv("GFORTRAN_STDERR_UNIT")

This is really a trivial example, and a more complex program will require more work, but you get the idea.


If your Fortran program is standalone (it has a 'program' unit and is supposed to be compiled to an executable called from the command line), and if you are new to Fortran, I would suggest to stick with the first choice, which will be much simpler. That's what the seasonal packaged does: call the executable of Census' X13AS from within R. The executable is in the x13binary package.



来源:https://stackoverflow.com/questions/63459875/how-to-call-a-fortran-program-from-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!