Why will this R package not install and how can I fix it?
Asked Answered
K

2

7

I would like to include a Fortran subroutine in an R package. I have always only built packages using devtools and roxygen (so my knowledge may be pretty limited). I am getting an error that prevents me from the package getting installed after it has been built about it not being a Win32 application...

I am using Rtools 3.3. My session info:

> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] roxygen2_5.0.1 devtools_1.9.1

loaded via a namespace (and not attached):
[1] magrittr_1.5  tools_3.2.2   Rcpp_0.12.1   memoise_0.2.1 stringi_1.0-1 stringr_1.0.0 digest_0.6.8 

To initially build the package, I run this:

library(devtools)
library(roxygen2)

setwd("C:/panterasBox")
create("myPack")
setwd("C:/panterasBox/myPack")
dir.create("C:/panterasBox/myPack/src")

This is the fortran code, saved as myFunc.f in the /src file:

         subroutine myFunc(x)
         implicit none
         real(8) x

         x = x + 2

         return
         end

The R wrapper I am using to call it (saved in the /R file):

#' @title A test
#' @description a test function.
#' @param x this is a number
#' @useDynLib myPack
#' @export
myFunc <- function(x){
  if (!is.loaded('myFunc')) {
    dyn.load("/src/myPack.dll")
  }
  myCall <- NULL
  myCall <- .Fortran("myFunc", x=as.double(x), PACKAGE="myPack")
  return(myCall$x)
}

Now, to create the documentation and install the package, I run this:

> document()
Updating myPack documentation
Loading myPack
Re-compiling myPack
"C:/Users/pantera/DOCUME~1/R/R-32~1.2/bin/x64/R" --no-site-file --no-environ --no-save --no-restore CMD INSTALL  \
"C:\panterasBox\myPack" --library="C:\Users\pantera\AppData\Local\Temp\RtmpQdJJko\devtools_install_1df837dd6c29" --no-R  \
--no-data --no-help --no-demo --no-inst --no-docs --no-exec --no-multiarch --no-test-load 

* installing *source* package 'myPack' ...
** libs
gfortran -m64     -O2  -mtune=core2 -c myFunc.f -o myFunc.o
gcc -m64 -shared -s -static-libgcc -o myPack.dll tmp.def myFunc.o -Ld:/RCompile/r-compiling/local/local320/lib/x64 -Ld:/RCompile/r-compiling/local/local320/lib -lgfortran -LC:/Users/pantera/DOCUME~1/R/R-32~1.2/bin/x64 -lR
installing to C:/Users/pantera/AppData/Local/Temp/RtmpQdJJko/devtools_install_1df837dd6c29/myPack/libs/x64
* DONE (myPack)
First time using roxygen2. Upgrading automatically...
Updating roxygen version in  C:\panterasBox\myPack/DESCRIPTION 
Writing NAMESPACE
Writing myFunc.Rd
> install("myPack")
Installing myPack
"C:/Users/pantera/DOCUME~1/R/R-32~1.2/bin/x64/R" --no-site-file --no-environ --no-save --no-restore CMD INSTALL  \
"C:/panterasBox/myPack" --library="C:/Users/pantera/Documents/R/R-3.2.2/library" --install-tests 

* installing *source* package 'myPack' ...
** libs

*** arch - i386
make: Nothing to be done for `all'.
installing to C:/Users/pantera/Documents/R/R-3.2.2/library/myPack/libs/i386

*** arch - x64
make: Nothing to be done for `all'.
installing to C:/Users/pantera/Documents/R/R-3.2.2/library/myPack/libs/x64
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
*** arch - i386
Error in inDL(x, as.logical(local), as.logical(now), ...) : 
  unable to load shared object 'C:/Users/pantera/Documents/R/R-3.2.2/library/myPack/libs/i386/mypack.dll':
  LoadLibrary failure:  %1 is not a valid Win32 application.

Error: loading failed
Execution halted
*** arch - x64
ERROR: loading failed for 'i386'
* removing 'C:/Users/pantera/Documents/R/R-3.2.2/library/myPack'
Error: Command failed (1)

I have also tried to build and check the package via the command line using R CMD build myPack and then R CMD check myPack_*tar.gz. The only error I get doing that is something about my LaTeX package.

Thank you for reading this, and I appreciate any help offered.

Disclaimer: I did ask this question previously, but I wanted to ask again in a "minimal fashion".

Kwangchow answered 12/1, 2016 at 13:4 Comment(4)
At a quick glance, I see that you should at least do return(myCall$x) (instead of your current return(mycall$x)).Milner
@JoshO'Brien Thanks. That was sloppy on my part. It didn't fix the problem, but I fixed this.Kwangchow
Are you sure you are not mixing i386 and x86_64 files? unable to load shared object 'C:/Users/pantera/Documents/R/R-3.2.2/library/myPack/libs/i386/mypack.dll': contains i386 but you have compiled for 64bit.Lusatian
Try to switch to 32bit R and redo, I still suspect you mixed i386 and x86_64 binary. Use R.version to check.Jewell
V
5

It's clear that this is an architecture issue. Looks like x64 version of your package (which is probably what you need) was built successfully, but x86 build and, thus, the overall task, failed. Try the following:

  1. Add --no-multiarch option to install call. This will tell RCmd not to build for x86, because your main arch is x64.
  2. (Probably, optional, but just for convenience.) Add --no-test-load option to install call. This will tell RCmd not to judge success of the build task by success of loading the package.
  3. Load the package manually with library('myPack') and see if it works.

To summarize, replace your install call with:

install('myPack', args=c('--no-multiarch','--no-test-load'))
library('myPack')
Vern answered 15/1, 2016 at 7:3 Comment(1)
Thank you. Excellent answer. Totally worth the bounty:)Kwangchow
F
0

It looks like your are loading dyn.load("/src/myPack.dll")

but during install it is looking for:

'C:/Users/pantera/Documents/R/R-3.2.2/library/myPack/libs/i386/mypack.dll'

(i.e. no capital P)

*Sorry I don't have enough rep to put that as a comment.

Foliar answered 12/1, 2016 at 17:14 Comment(1)
This got rid of the warning and note I posted about the Undeclared package in foreign function calls. I updated the question, thanks.Kwangchow

© 2022 - 2024 — McMap. All rights reserved.