3

I want to add a Fortran routine (from a package that I found on the internet) to my own R package. My R package compiles just fine, and so does the package with the Fortran code, but when I add that code to my package I get an error. I have come up with the following toy example

R code in R folder

#' @export 
testfun<-function() { addc(1,2) + add_Fortran(1,2) }
#' @useDynLib testpac addf
#' @export
add_Fortran <- function(x, y) {
  .Fortran(addf, x, y, numeric(1))[[3]]
}

C and Fortran Code in src folder

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double addc(double x, double y) {
   return x*y;
}
subroutine addf(x, y, answer)
    double precision x, y, answer
    answer = x + y
end

and now when I run devtools::document() I get the error

from 1mRcppExports.cpp:4:
   1mRcppExports.cpp:28:34: 1;31merror: '1maddf_' was not declared in this scope
      28 |     {"addf", (DL_FUNC) &F77_NAME(1;31maddf), 3},
         |                                  1;31m^~~~
   1mC:/R/include/R_ext/RS.h:104:25: 1;36mnote: in definition of macro '1mF77_CALL'
     104 | # define F77_CALL(x)    1;36mx ## _
         |                         1;36m^
   1mRcppExports.cpp:28:25: 1;36mnote: in expansion of macro '1mF77_NAME'
      28 |     {"addf", (DL_FUNC) &1;36mF77_NAME(addf), 3},
         |                         1;36m^~~~~~~~
   make: *** [C:/R/etc/x64/Makeconf:296: RcppExports.o] Error 1
   ERROR: compilation failed for package 'testC'

So somehow I need to declare the routine addf somewhere, but I have not been able to find out how or where.

Wolfgang

4
  • 1
    Have you seen this: stackoverflow.com/a/52136973? Commented Feb 27 at 20:18
  • stackoverflow.com/search?q=%5Br%5D+rcpp+Fortran+ Commented Feb 28 at 1:47
  • Yes, I have looked at both of these. Running Rcpp::compileAttributes() doesn't result in any errors but also doesn't seem to do anything. Then running devtools::install() yields the same error. I assume that addf should be mentioned in NAMESPACE before running install(), but it should be added by document(), which doesn't run. Commented Feb 28 at 10:54
  • As to the underscore that should be there, my understanding is that document should add that? Or do all the names of the Fortran routines have to have an underscore to start? Anyway I am using .Fortran, and so an underscore shouldn't be needed at all? Commented Feb 28 at 10:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.