[R] Calling LAPACK functions directly from R

William Constantine wconstan at gmail.com
Thu Apr 12 01:02:39 CEST 2007


I am interested in tapping into LAPACK functionality directly from R.
Using R-2.4.1 for Windows, I was able to do so ala:

dyn.load("bin/Rlapack.dll")
is.loaded("dstebz") # returns TRUE

N <- 100
NW <- 4
n.tapers <- 5
tpW <- (2 * pi * NW)/N
otNmo <- 1:N
D <- as.double(cos(tpW) * ((N - 1 - 2 * (0:(N - 1)))/2)^2)
E <- as.double((otNmo * (N - otNmo))/2)

z <- .Fortran("dstebz",
  "I", "B", as.integer(N), double(1), double(1),
  as.integer(N - n.tapers + 1), as.integer(N), double(1),
  D, E, integer(1), integer(1), double(N), integer(N), integer(N),
  double(4 * N), integer(3 * N), integer(1))[13:15]

I then extended this approach in developing an R package
where I added the following to the appropriate .First.lib():

  lapack.path <- file.path(R.home(), ifelse(.Platform$OS.type == "windows",
    file.path("bin", "Rlapack"), file.path("lib", "libRlapack")))
  dyn.load(paste(lapack.path,.Platform$dynlib.ext, sep=""))

which loads the LAPACK shared objects in lib/libRlapack.so for LINUX/UNIX
and bin/Rlapack.dll for Windows.

Q1: Is there a better or more robust way of loading LAPACK symbols
into R, (e.g., one that is not platform dependent)?

---------------------------------------------------------------------------------------------------------------------

My R package does not currently contain any C or FORTRAN code. However,
it has been suggested to me that creating a src/Makevars file containing
the line:

  PKG_LIBS=$(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)

is a better means of loading LAPACK symbols as it
would eliminate the need to use dyn.load() as shown in my .First.lib().
In Windows, however, this suggestion fails and results (for example)
in the above code returning a "missing dstebz symbol" error. My understanding
is that one need only create such a src/Makevar if they are interested in
tapping into LAPACK functionality from their src/ C or FORTRAN code
and so am doubtful of this suggestion.

Q2: Given that I have no C/FORTRAN code in my package, am I
correct to assume that creating a Makevars files in such a way
does not eliminate the need to use dyn.load() as in the above?

Q3: For future releases of R, should I expect the path of the LAPACK
library to remain as they are noted above in my .First.lib() example?

Thanks!



More information about the R-help mailing list