2016-01-12 90 views
6

我想在R包中包含一個Fortran子例程。我一直只使用devtools和roxygen構建包(所以我的知識可能相當有限)。我得到一個錯誤,防止我的包被安裝後,它已被建立它不是一個Win32應用程序...這個R軟件包爲什麼不能安裝,我該如何解決?

我正在使用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:\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) 

我也試圖建立並通過使用R CMD build myPack,然後R CMD check myPack_*tar.gz命令行檢查包裝。我得到的唯一的錯誤是關於我的LaTeX軟件包。

感謝您閱讀本文,我感謝您提供的任何幫助。

聲明:我之前曾問過這個問題,但我想以「最小的方式」再次提問。

+0

快速瀏覽,我看到你至少應該做'回報(MYCALL $ X)'(而非目前'回報(的MyCall $ X)')。 –

+0

@ JoshO'Brien謝謝。這對我來說是蠻橫的。它沒有解決問題,但我解決了這個問題。 – panterasBox

+1

你確定你沒有混合i386和x86_64文件嗎? '無法加載共享對象'C:/Users/pantera/Documents/R/R-3.2.2/library/myPack/libs/i386/mypack.dll':'包含'i386',但是您已經編譯爲64位。 –

回答

5

很明顯,這是一個架構問題。看起來像你的軟件包的x64版本(這可能是你需要的)成功構建,但x86構建,因此,整體任務失敗。請嘗試以下操作:

  1. --no-multiarch選項添加到install調用。這將告訴RCmd不能爲x86構建,因爲您的主拱是x64。
  2. (可能,可選,但只是爲了方便。)將--no-test-load選項添加到install調用。這將告訴RCmd不要通過加載包來判斷構建任務是否成功。
  3. library('myPack')手動加載軟件包,看它是否有效。

總之,更換你的install電話用:

install('myPack', args=c('--no-multiarch','--no-test-load')) 
library('myPack') 
+0

謝謝。優秀的答案。完全值得賞金:) – panterasBox

0

它看起來像你的加載dyn.load("/src/myPack.dll")

,但在安裝過程中它正在尋找:

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

(即沒有資本P)

*對不起,我沒有足夠的代表將其作爲評論。

+0

這擺脫了警告,並注意到我發佈了關於「在外部函數調用中未聲明的包」。我更新了問題,謝謝。 – panterasBox

相關問題