Package building in R with Rcpp -- failure to build a package - r

I am very new to building packages in R, but I am trying to build a package of already written cpp code to use it in a foreach code, as I am running my code on a Windows machine and I can't pass the cpp code to the nodes.
The cpp code I am trying to wrap in an R package comes from:
https://github.com/darinchristensen/conley-se
I am following the instructions from this website to build the package:
http://web.mit.edu/insong/www/pdf/rpackage_instructions.pdf
Here is the code I am using:
setwd("C:/Users/Tim/Documents/code/")
library(Rcpp)
library(RcppArmadillo)
RcppArmadillo.package.skeleton("itirun")
Then, I try
setwd("C:/Users/Tim/Documents/code/itirun/")
compileAttributes(verbose=TRUE)
And then in terminal I go to the directory, and run the following command which gives me the following error message. I am a newbie at building packages, and I fail to understand what I am doing wrong. I also tried adding compiler flags, but wasn't successful which is probably due to my lack of understanding:
C:\Users\Tim\Documents\code> R CMD build itirun
* checking for file 'itirun/DESCRIPTION' ... OK
* preparing 'itirun':
* checking DESCRIPTION meta-information ... OK
* cleaning src
installing the package to process help pages
Warning: running command '"C:/PROGRA~1/MICROS~2/ROPEN~1/R-
34~1.2/bin/x64/Rcmd.ex
e" INSTALL -l "C:\Users\Tim\AppData\Local\Temp\RtmpA39cZK\Rinst2c0631ac5b" -
-no
-multiarch "C:/Users/Tim/AppData/Local/Temp/RtmpA39cZK/Rbuild2c02fc3722a/itirun
"' had status 1
-----------------------------------
* installing *source* package 'itirun' ...
** libs
c:/Rtools/mingw_64/bin/g++ -m64 -I"C:/PROGRA~1/MICROS~2/ROPEN~1/R-34~1.2/include
" -DNDEBUG -I"C:/Users/Tim/Documents/R/win-library/3.4/Rcpp/include" -I"C:/Use
rs/Tim/Documents/R/win-library/3.4/RcppArmadillo/include" -I"C:/swarm/workspa
ce/External-R-3.4.2/vendor/extsoft/include" -fopenmp -O2 -Wall -mtune=core2
-c RcppExports.cpp -o RcppExports.o
In file included from C:/Users/Tim/Documents/R/win-library/3.4/RcppArmadillo/in
clude/armadillo:53:0,
from C:/Users/Tim/Documents/R/win-library/3.4/RcppArmadillo/in
clude/RcppArmadilloForward.h:46,
from C:/Users/Tim/Documents/R/win-library/3.4/RcppArmadillo/in
clude/RcppArmadillo.h:31,
from RcppExports.cpp:4:
C:/Users/Tim/Documents/R/win-library/3.4/RcppArmadillo/include/armadillo_bits/c
ompiler_setup.hpp:487:118: note: #pragma message: WARNING: support for OpenMP re
quires C++11/C++14; add -std=c++11 or -std=c++14 to compiler flags
#pragma message ("WARNING: support for OpenMP requires C++11/C++14; add -std=
c++11 or -std=c++14 to compiler flags")
^
c:/Rtools/mingw_64/bin/g++ -m64 -I"C:/PROGRA~1/MICROS~2/ROPEN~1/R-34~1.2/include
" -DNDEBUG -I"C:/Users/Tim/Documents/R/win-library/3.4/Rcpp/include" -I"C:/Use
rs/Tim/Documents/R/win-library/3.4/RcppArmadillo/include" -I"C:/swarm/workspa
ce/External-R-3.4.2/vendor/extsoft/include" -fopenmp -O2 -Wall -mtune=core2
-c cpp-functions.cpp -o cpp-functions.o
In file included from C:/Users/Tim/Documents/R/win-library/3.4/RcppArmadillo/in
clude/armadillo:53:0,
from C:/Users/Tim/Documents/R/win-library/3.4/RcppArmadillo/in
clude/RcppArmadilloForward.h:46,
from C:/Users/Tim/Documents/R/win-library/3.4/RcppArmadillo/in
clude/RcppArmadillo.h:31,
from cpp-functions.cpp:4:
C:/Users/Tim/Documents/R/win-library/3.4/RcppArmadillo/include/armadillo_bits/c
ompiler_setup.hpp:487:118: note: #pragma message: WARNING: support for OpenMP re
quires C++11/C++14; add -std=c++11 or -std=c++14 to compiler flags
#pragma message ("WARNING: support for OpenMP requires C++11/C++14; add -std=
c++11 or -std=c++14 to compiler flags")
^
In file included from C:/Users/Tim/Documents/R/win-library/3.4/RcppArmadillo/in
clude/RcppArmadillo.h:38:0,
from cpp-functions.cpp:4:
C:/Users/Tim/Documents/R/win-library/3.4/RcppArmadillo/include/RcppArmadilloAs.
h: In instantiation of 'Rcpp::traits::Exporter<arma::Cube<eT> >::cube_t Rcpp::tr
aits::Exporter<arma::Cube<eT> >::get() [with T = long long int; Rcpp::traits::Ex
porter<arma::Cube<eT> >::cube_t = arma::Cube<long long int>]':
C:/Users/Tim/Documents/R/win-library/3.4/RcppArmadillo/include/RcppArmadilloAs.
h:549:48: required from here
C:/Users/Tim/Documents/R/win-library/3.4/RcppArmadillo/include/RcppArmadilloAs.
h:514:49: error: invalid cast from type 'Rcpp::Vector<19>::iterator {aka Rcpp::i
nternal::Proxy_Iterator<Rcpp::internal::generic_proxy<19> >}' to type 'long long
int*'
dims[0], dims[1], dims[2], false);
^
make: *** [cpp-functions.o] Error 1
Warning: running command 'make -f "Makevars.win" -f "C:/PROGRA~1/MICROS~2/ROPEN~
1/R-34~1.2/etc/x64/Makeconf" -f "C:/PROGRA~1/MICROS~2/ROPEN~1/R-34~1.2/share/mak
e/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHL
IB="itirun.dll" WIN=64 TCLBIN=64 OBJECTS="RcppExports.o cpp-functions.o rcpparma
_hello_world.o"' had status 2
ERROR: compilation failed for package 'itirun'
* removing 'C:/Users/Tim/AppData/Local/Temp/RtmpA39cZK/Rinst2c0631ac5b/itirun'
-----------------------------------
ERROR: package installation failed

Related

R package source install, no compiler

I am trying to install some packages from source on a linux (RHEL) server. But whenever the package requires some C++ compilation it fails.
For example, I try to install Ckmeans.1d.dp package. If I call R CMD INSTALL Ckmeans.1.dp_4.2.1.tar.gz I get the following output (truncated) where you can see the compiler is omitted.
...* installing *source* package ‘Ckmeans.1d.dp’ ...
** libs
I/usr/include/R -DNDEBUG -I/usr/local/include -c Ckmeans.1d.dp.cpp -o Ckmeans.1d.dp.o
make: I/usr/include/R: Command not found
...
-shared -L/usr/local/lib64 -o Ckmeans.1d.dp.so Ckmeans.1d.dp.o Ckmeans.1d.dp_main.o dynamic_prog.o fill_SMAWK.o fill_log_linear.o fill_quadratic.o select_levels.o weighted_select_levels.o -L/usr/lib64/R/lib -lR
/bin/sh: line 2: -shared: command not found
make: *** [Ckmeans.1d.dp.so] Error 127
ERROR: compilation failed for package ‘Ckmeans.1d.dp’
I am currently stuck with a server that only has R 3.1.1 and I do not have admin privileges. The $Rhome/etc/Makeconf has CXX and CC defined. I have also verified g++ and gcc are installed.
What could be happening here?
Invariably an environment variable with an 'empty' value gets expanded. Maybe (just guessing here) the package would use C++11, so $(CXX11} -shared gets expanded and ... becomes -shared and an error is triggered.
Look at the R settings, eg via less $(R HOME)/etc/Makevars and see what it expects.
I think you can get by, if you must, installing gcc et al below, say, ~/bin. Some packages will require a compiler...

Install ROracle in Mac

I'm on Mac OS Sierra.
I have tried doing - Installation of RODBC/ROracle packages on OS X Mavericks but it didn't help.
This is what I did:
Downloaded from http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html - Basic, Sql plus, odbc
Unzipped in my home folder in instantclient_12_1
ln -s libclntsh.dylib.12.1 libclntsh.dylib
export DYLD_LIBRARY_PATH=/Users/dhananjaygoel/instantclient_12_1:$DYLD_LIBRARY_PATH
R CMD INSTALL --configure-args='--with-oci-lib=/Users/dhananjaygoel/instantclient_12_1' ROracle_1.3-1.tar.gz
It gives me the following error:
* installing to library ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library’
* installing *source* package ‘ROracle’ ...
** package ‘ROracle’ successfully unpacked and MD5 sums checked
configure: creating ./config.status
config.status: creating src/Makevars
** libs
clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/Users/dhananjaygoel/instantclient_12_1/sdk/include -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -fPIC -Wall -mtune=core2 -g -O2 -I/opt/local/include -c rodbi.c -o rodbi.o
rodbi.c:2432:53: warning: format specifies type 'long long' but the argument has
type 'sb8' (aka 'long') [-Wformat]
sprintf(err_buf, RODBI_ERR_BIND_VAL_TOOBIG, bndsz);
^~~~~
/usr/include/secure/_stdio.h:47:56: note: expanded from macro 'sprintf'
__builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
^~~~~~~~~~~
rodbi.c:2469:53: warning: format specifies type 'long long' but the argument has
type 'sb8' (aka 'long') [-Wformat]
sprintf(err_buf, RODBI_ERR_BIND_VAL_TOOBIG, bndsz);
^~~~~
/usr/include/secure/_stdio.h:47:56: note: expanded from macro 'sprintf'
__builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
^~~~~~~~~~~
rodbi.c:2816:55: warning: format specifies type 'long long' but the argument has
type 'sb8' (aka 'long') [-Wformat]
sprintf(err_buf, RODBI_ERR_BIND_VAL_TOOBIG, bndsz);
^~~~~
/usr/include/secure/_stdio.h:47:56: note: expanded from macro 'sprintf'
__builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
^~~~~~~~~~~
rodbi.c:2861:55: warning: format specifies type 'long long' but the argument has
type 'sb8' (aka 'long') [-Wformat]
sprintf(err_buf, RODBI_ERR_BIND_VAL_TOOBIG, bndsz);
^~~~~
/usr/include/secure/_stdio.h:47:56: note: expanded from macro 'sprintf'
__builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
^~~~~~~~~~~
4 warnings generated.
clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/Users/dhananjaygoel/instantclient_12_1/sdk/include -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -fPIC -Wall -mtune=core2 -g -O2 -I/opt/local/include -c rooci.c -o rooci.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o ROracle.so rodbi.o rooci.o -L/Users/dhananjaygoel/instantclient_12_1 -lclntsh -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
installing to /Library/Frameworks/R.framework/Versions/3.3/Resources/library/ROracle/libs
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
Error in dyn.load(file, DLLpath = DLLpath, ...) :
unable to load shared object '/Library/Frameworks/R.framework/Versions/3.3/Resources/library/ROracle/libs/ROracle.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.3/Resources/library/ROracle/libs/ROracle.so, 6): Library not loaded: #rpath/libclntsh.dylib.12.1
Referenced from: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/ROracle/libs/ROracle.so
Reason: image not found
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/ROracle’
Someone please help.
The following has worked for me on a few machines running El Capitan, so perhaps it will work on High Sierra. Unlike some other instructions, this does not require you to disable SIP.
Install the Oracle Instant Client Basic, SDK, and SQL*Plus packages. I'm pretty sure I'm not using the ODBC package, though it shouldn't get in the way if you do have it.
Create or edit ~/.R/Makevars. Add the following:
CC=/usr/bin/clang -rpath /Users/dhananjaygoel/instantclient_12_1
LDFLAGS=-L/Users/dhananjaygoel/instantclient_12_1
Both the -rpath setting in CC and the -L linked directory in LDFLAGS should be the location of the Instant Client's .dylib files. If you've moved them to another location (I personally prefer to make a /lib subdirectory and put them there), that's where you should point.
Run the install command:
R CMD INSTALL --configure-args='--with-oci-lib=/Users/dhananjaygoel/instantclient_12_1' ROracle_1.3-1.tar.gz
with --with-oci-lib= pointing to the root of the Instant Client (unlike the references in Makevars, this does not, as far as I know, rely on the location of the .dylib files).
I found this googling around.
Just Ctrl+f (or your alternative on Mac, I'm using linux) the word "image not found" and it'll take you to the problem.
The page will lead you eventually to here.
It's very similar to your error, however, I don't have any experience in using Mac, hope it helps though :)

Tell Rstudio to use mpif90 instead of gfortran

In order to learn how to create a R packages that uses Fortran I created a simple one to calculate Pi with a monte carlo simulation. Now I'm trying to make the Fortran code parallel with MPI.
When I add use MPI to my fortran code and click the Build and Reload buton on Rstudio I get the following error.
==> R CMD INSTALL --no-multiarch --with-keep.source MyPi
* installing to library ‘/home/ignacio/R/x86_64-pc-linux-gnu-library/3.2’
* installing *source* package ‘MyPi’ ...
gfortran -fpic -g -O2 -fstack-protector-strong -c Fpi.f90 -o Fpi.o
/usr/lib/R/etc/Makeconf:161: recipe for target 'Fpi.o' failed
** libs
Fpi.f90:2.4:
use mpi
1
Fatal Error: Can't open module file 'mpi.mod' for reading at (1): No such file or directory
make: *** [Fpi.o] Error 1
ERROR: compilation failed for package ‘MyPi’
* removing ‘/home/ignacio/R/x86_64-pc-linux-gnu-library/3.2/MyPi’
* restoring previous ‘/home/ignacio/R/x86_64-pc-linux-gnu-library/3.2/MyPi’
Exited with status 1.
I think the problem is that Rstudio is trying to compile using gfortran instead of mpi90. I'm able to follow this post to do a very basic hello world, but there you compile the code with mpif90 -fpic -shared -o test.so test.f90
How can I tell Rstudio to use mpif90 -fpic -shared instead of gfortran?
I added this to src/Makevars
FC=mpif90
This is what I get:
==> R CMD INSTALL --no-multiarch --with-keep.source MyPi
* installing to library ‘/home/ignacio/R/x86_64-pc-linux-gnu-library/3.2’
* installing *source* package ‘MyPi’ ...
gfortran -fpic -g -O2 -fstack-protector-strong -c Fpi.f90 -o Fpi.o
/usr/lib/R/etc/Makeconf:161: recipe for target 'Fpi.o' failed
** libs
Fpi.f90:2.4:
USE MPI
1
Fatal Error: Can't open module file 'mpi.mod' for reading at (1): No such file or directory
make: *** [Fpi.o] Error 1
ERROR: compilation failed for package ‘MyPi’
* removing ‘/home/ignacio/R/x86_64-pc-linux-gnu-library/3.2/MyPi’
* restoring previous ‘/home/ignacio/R/x86_64-pc-linux-gnu-library/3.2/MyPi’
Exited with status 1.
You just need to set the FC flag in your Makevars file to indicate the fortran compiler you wish to use.
FC=mpif90
EDIT - The above doesn't work if included locally in your package
The FC macro will be overwritten when specified within a package Makevars because the src/Makevars file is read before $RHOME/etc/Makeconf which overwrites the FC macro. In order to change the default compiler is to create a Makevars file in a .R/ directory in your $HOME directory.
Then just create the file with the same line as above. Restart R and try to compile again (this worked on my Ubuntu system).

install RMySQL on windows

I am trying to install the package RMYSQL on windows.
To do so i followed this tutorial : http://www.ahschulz.de/2013/07/23/installing-rmysql-under-windows/
But i am getting an error when i execute
install.packages(“RMySQL”, type = “source”)
In deed R returns me the following :
Installing package into ‘D:/Users/quentin/Documents/R/win-library/3.1’
(as ‘lib’ is unspecified)
trying URL 'http://cran.rstudio.com/src/contrib/RMySQL_0.9-3.tar.gz'
Content type 'application/x-gzip' length 165363 bytes (161 Kb) opened URL
downloaded 161 Kb
* installing *source* package 'RMySQL' ...
** package 'RMySQL' correctement décompressé et sommes MD5 vérifiées
checking for $MYSQL_HOME... C:/Program Files (x86)/MySQL/mysql-5.6.19-win32
cygwin warning:
MS-DOS style path detected: C:/Program
Preferred POSIX equivalent is: /cygdrive/c/Program
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
test: Files: unknown operand
** libs
Avis : this package has a non-empty 'configure.win' file,
so building only the main architecture
cygwin warning:
MS-DOS style path detected: C:/PROGRA~1/R/R-31~1.0/etc/x64/Makeconf
Preferred POSIX equivalent is: /cygdrive/c/PROGRA~1/R/R-31~1.0/etc/x64/Makeconf
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
gcc -m64 -I"C:/PROGRA~1/R/R-31~1.0/include" -DNDEBUG -I"C:/Program Files (x86)/MySQL/mysql-5.6.19-win32"/include - I"d:/RCompile/CRANpkg/extralibs64/local/include"
-O2 -Wall -std=gnu99 -mtune=core2 -c
RS-DBI.c -o RS-DBI.o
RS-DBI.c: In function 'RS_na_set':
RS-DBI.c:1219:11: warning: variable 'c' set but not used [-Wunused-but-set-variable]
gcc -m64 -I"C:/PROGRA~1/R/R-31~1.0/include" -DNDEBUG -I"C:/Program Files (x86)/MySQL/mysql-5.6.19-win32"/include - I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall - std=gnu99 -mtune=core2 -c RS-MySQL.c -o RS-MySQL.o
RS-MySQL.c: In function 'RS_MySQL_fetch':
RS-MySQL.c:657:13: warning: variable 'fld_nullOk' set but not used [-Wunused-but-set- variable]
RS-MySQL.c: In function 'RS_DBI_invokeBeginGroup':
RS-MySQL.c:1137:30: warning: variable 'val' set but not used [-Wunused-but-set-variable]
RS-MySQL.c: In function 'RS_DBI_invokeNewRecord':
RS-MySQL.c:1158:20: warning: variable 'val' set but not used [-Wunused-but-set- variable]
RS-MySQL.c: In function 'RS_MySQL_dbApply':
RS-MySQL.c:1219:38: warning: variable 'fld_nullOk' set but not used [-Wunused-but-set- variable]
gcc -m64 -shared -s -static-libgcc -o RMySQL.dll tmp.def RS-DBI.o RS-MySQL.o C:/Program Files (x86)/MySQL/mysql-5.6.19-win32/bin/libmySQL.dll - Ld:/RCompile/CRANpkg/extralibs64/local/lib/x64 -Ld:/RCompile/CRANpkg/extralibs64/local/lib -LC:/PROGRA~1/R/R-31~1.0/bin/x64 -lR
collect2: ld returned 5 exit status
aucune DLL n'a pas été créé
ERROR: compilation failed for package 'RMySQL'
* removing 'D:/Users/quentin/Documents/R/win-library/3.1/RMySQL'
Warning in install.packages :
running command '"C:/PROGRA~1/R/R-31~1.0/bin/x64/R" CMD INSTALL -l "D:\Users\quentin\Documents\R\win-library\3.1" D:\Users\quentin\AppData\Local\Temp\2\RtmpOwB1UH/downloaded_packages/RMySQL_0.9-3.tar.gz' had status 1
Warning in install.packages :
installation of package ‘RMySQL’ had non-zero exit status
The downloaded source packages are in
‘D:\Users\quentin\AppData\Local\Temp\2\RtmpOwB1UH\downloaded_packages’
I couldn't manage to fix this problem so if someone have an idea.
i tried all the steps from all the instructions i could get, but still got the same problems as you got.
I finallly solved accidently by follow this code:
install.packages('RMySQL')
instead of
install.packages(“RMySQL”, type = “source”)
I cannot explain the reason, but it truly works.
I solved it by installing from a .zip file not from .tar.gz.
Make sure your MYSQL_HOME environment variable is set correctly and libmysql.dll is copied to bin folder!!!
Run install.packages('RMySQL') then when the "Do you want to install from sources..." window pops up select No.
Then copy the downloaded binary packages location from console.
Go to Packages -> Install, paste the location into Package archive and click Install.

Rcpp install fails on Mac 10.8

Rcpp is failing to install for me. When I run the following within the R console (R 2.15.1 on Mac 10.8):
install.packages("Rcpp")
I get the following error:
/usr/bin/clang++ -I/usr/local/Cellar/r/2.15.1/R.framework/Resources/include \
-DNDEBUG -I../inst/include/ -I/usr/local/Cellar/readline/6.2.4/include \
-isystem /usr/local/include -I/opt/X11/include -fPIC -Os -w -pipe - \
march=native -Qunused-arguments -mmacosx-version-min=10.8 \
-c exceptions.cpp -o exceptions.o
exceptions.cpp:82:14: fatal error: 'bits/exception_defines.h' file not found
#include <bits/exception_defines.h>
^
1 error generated.
make: *** [exceptions.o] Error 1
ERROR: compilation failed for package ‘Rcpp’
What am I doing wrong?
Which version of Rcpp? This is fixed in SVN:
2012-07-06 Dirk Eddelbuettel <edd#debian.org>
* inst/include/Rcpp/config.h: In order to not attempt to include
exception_defines.h if on OS X (as the clang runtime may not have
predictable access to g+++ headers providing these), do not define
RCPP_HAS_DEMANGLING which is used in src/exceptions.cpp
and was discussed on the rcpp-devel list.
I tried to accomodate OS X and clang >= 3.0, but the interaction of clang and g++ is a little tricky. Try to fetch the file from SVN or just edit the section in it to yield
#ifdef __GNUC__
// from http://sourceforge.net/apps/mediawiki/predef/index.php?\
// title=Operating_Systems#MacOS
#ifndef __APPLE__
#ifndef __MACH__
#define RCPP_HAS_DEMANGLING
#endif
#endif
#endif
which turns this off for all OS X instances.

Resources