Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8289523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:31:37+00:00 2026-06-08T12:31:37+00:00

After spending some days already searching for something like this on the internet, I

  • 0

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:

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?

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):

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

and a simple R function to pass to the above function:

asdf <- function(a, s, d, f){x <- c(a, s, d, f)}

Calling .Fortran("foo", asdf, vector(mode="numeric", length=22)) yields
Error: invalid mode (closure) to pass to Fortran (arg 1) and passing “asdf” (as a string) results in a segfault, as the argument obviously doesn’t fit the expected type (namely, a function).

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.

Thanks in advance,

Dean

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:31:38+00:00Added an answer on June 8, 2026 at 12:31 pm

    You can’t pass R objects via .Fortran. You would need to use the .Call or .External interface to pass the R objects to C/C++ code.

    You could write a C/C++ wrapper for your R function, which you could then call from your Fortran code (see Calling-C-from-FORTRAN-and-vice-versa in Writing R Extensions).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After spending roughly two days on this, I'm getting a little rattled. Although by
Earlier today I asked this question . After spending some time investigating this issue,
After spending few hours on the internet to find about push notification system for
After spending some time wireframing my ideas, I want to start building my rails
So after spending some time googling I finally got autotest to work on ubuntu
I'm spending some time with Java again after a long break on the .NET
After spending much time researching about this on Google, I could not come across
After spending some hours with the Ruby Debugger I finally learned that I need
After becoming somewhat estranged open source, and spending some years developing web applications in
After spending some hours reading about Unit Testing and test driven development here on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.