我想在R包中包含一个Fortran子程序。我一直只使用devtools和roxygen构建包(所以我的知识可能非常有限)。我收到一个错误,该错误阻止我在软件包生成后安装它,因为它不是Win32应用程序。。。
我正在使用Rtools 3.3。我的会话信息:
> 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
为了最初构建包,我运行以下程序:
library(devtools)
library(roxygen2)
setwd("C:/panterasBox")
create("myPack")
setwd("C:/panterasBox/myPack")
dir.create("C:/panterasBox/myPack/src")
这是fortran代码,保存为/src
文件中的myFunc.f:
subroutine myFunc(x)
implicit none
real(8) x
x = x + 2
return
end
我用来调用它的R包装器(保存在/R
文件中):
#' @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)
}
现在,为了创建文档并安装包,我运行以下命令:
> 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:panterasBoxmyPack" --library="C:UserspanteraAppDataLocalTempRtmpQdJJkodevtools_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:panterasBoxmyPack/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)
我还尝试使用R CMD build myPack
和R CMD check myPack_*tar.gz
通过命令行构建和检查包。我做这件事的唯一错误是关于我的LaTeX包。
谢谢你读到这篇文章,我感谢你的帮助。
免责声明:我之前确实问过这个问题,但我想以"最低限度的方式"再问一次。
很明显,这是一个架构问题。看起来您的包的x64版本(可能正是您所需要的)已成功生成,但x86生成以及整个任务失败。尝试以下操作:
- 将
--no-multiarch
选项添加到install
调用中。这将告诉RCmd
不要为x86构建,因为您的主拱门是x64 - (可能是可选的,但只是为了方便。)将
--no-test-load
选项添加到install
调用中。这将告诉RCmd
不要通过成功加载包来判断构建任务的成功 - 用
library('myPack')
手动加载包,看看它是否有效
总之,将您的install
呼叫替换为:
install('myPack', args=c('--no-multiarch','--no-test-load'))
library('myPack')
看起来您正在加载dyn.load("/src/myPack.dll")
但在安装过程中,它正在寻找:
'C:/Users/pantera/Documents/R/R-3.2.2/library/myPack/libs/i386/mypack.dll'
(即无资本p)
*抱歉,我没有足够的代表发表评论。