R call fortran

来源:互联网 发布:中小学生出作业软件 编辑:程序博客网 时间:2024/06/06 01:18

Object: Call fortran subroutine using R

Platform: window 10, Fortranfortran (Intel Visual Fortran), R (3.1.3)

Testing code source: https://en.wikiversity.org/wiki/R_(programming_language)/Tutorials/Connecting_Fortran_and_R

Fortran code (multiply.f):

c sample subroutine to include for R       subroutine multiply(ax,bx,cx)       implicit none       integer ax,bx,cx       cx=ax*bx       end

R code (testfortran.R):

setwd('C:/Personal/code/C/RcallF')system('R CMD SHLIB multiply.f')dyn.load('C:/Personal/code/C/RF/multiply.dll')star = function(a,b){  x<-.Fortran('multiply',as.integer(a),as.integer(b),c=integer(1))  return(x$c)}a=5b=2y<-star(a,b)

Note:  multiply.f path for this test is: 
'C:/Personal/code/C/RcallF'

In R, use setwd to set this path. Just run R file, the link is generated automatically for fortran files.This is easier than building C++ and Fortran connection.


Result:







0 0
原创粉丝点击