Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

Passing characters/strings from R to Fortran

$
0
0

I have a Fortran subroutine that selects a function based on the value of a string and then executes that function.

!! file:select.f90
module funcs
    contains
        subroutine add(x, y, xy)
            real :: x, y, xy
            xy = x + y
            return
        end subroutine

        subroutine diff(x, y, xy)
            real :: x, y, xy
            xy = x - y
            return
        end subroutine
end module

subroutine simple(modname)
    use funcs

    procedure(), pointer :: model => null()

    character(10) :: modname
    real :: x, y, xy

    print *, "-",modname,"-"
    select case (modname)
        case("add")
            model => add
        case("diff")
            model => diff
        case default
            print *, "No model with that name!"
            stop
    end select

    x = 4
    y = 3
    call model(x, y, xy)
    print *, xy

end subroutine

I would like to call this subroutine from an R script.

# file:select.R
dyn.load("select.so")
.Fortran("simple", "add")
.Fortran("simple", "diff")

As a standalone Fortran program that takes a command line argument, this runs perfectly fine. It's even insensitive to spaces before or after modname. However, when I try to pass in a character as an argument from R, it correctly re-prints the character (without any extra spaces), but then doesn't recognize it as a case and skips to the default. What is going on here? Is there some encoding issue with R characters that makes them incompatible with Fortran?


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>