Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing functions in R as .Fortran arguments
    text
    copied!<p>After spending some days already searching for something like this on the internet, I still couldn't manage to find anything describing this problem. Reading through the (otherwise quite recommendable) 'Writing R Extensions' dind't offer a solution as well. Thus, here's my most urgent question:</p> <p>Is it possible to pass functions (for simplicity's sake, assume a simple R function - in reality, the problem is even uglier) as function/subroutine parameters to Fortran via .Fortran(...) call - and if so, how?</p> <p>I wrote two simple functions in order to test this, first a Fortran subroutine (tailored to use the function I originally intended to pass, thus the kinda weird dimensions in the interface):</p> <pre><code>subroutine foo(o, x) implicit none interface subroutine o(t, y, param, f) double precision, intent(in) :: t double precision, dimension(58), intent(in) :: y, param double precision, dimension(22), intent(out) :: f end subroutine end interface double precision, dimension(22), intent(out) :: x double precision, dimension(58) :: yt, paramt integer :: i do i = 1, 58 yt(i) = rand(0) paramt(i) = rand(1) end do call o(dble(4.2), yt, paramt, x) end subroutine </code></pre> <p>and a simple R function to pass to the above function:</p> <pre><code>asdf &lt;- function(a, s, d, f){x &lt;- c(a, s, d, f)} </code></pre> <p>Calling <code>.Fortran("foo", asdf, vector(mode="numeric", length=22))</code> yields <code>Error: invalid mode (closure) to pass to Fortran (arg 1)</code> and passing "asdf" (as a string) results in a segfault, as the argument obviously doesn't fit the expected type (namely, a function).</p> <p>FYI, I don't expect the code to do anything meaningful (that would be the task of another function), I mainly would like to know, whether passing functions (or function pointers) from R is possible at all or wether I better give up on this approach instantly and look for something that might work.</p> <p>Thanks in advance,</p> <p>Dean</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload