Issues compiling Rpackage: error in asNamespace(ns) using Rcpp - r

Working on a small Rcpp package to use Boost and some of its geometry functions in C.
Finished writing the functions and everything was working well. Tested that everything was working properly (Clean and Rebuild and testing the functions) one last time before pushing up to GitHub. Once I double-checked the directory was all cleanly stored in GitHub I removed the directory from my local machine. It should also be noted that I have Roxygen2 running on this and managing the NAMESPACE file.
Upon cloning the directory back and Clean and Rebuild I get the following error:
==> Rcpp::compileAttributes()
* Updated R/RcppExports.R
==> R CMD INSTALL --preclean --no-multiarch --with-keep.source MinimumRcpp
* installing to library ‘/Library/Frameworks/R.framework/Versions/3.5/Resources/library’
* installing *source* package ‘MinimumRcpp’ ...
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include -fPIC -Wall -g -O2 -c RcppExports.cpp -o RcppExports.o
** libs
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include -fPIC -Wall -g -O2 -c findParetoSet.cpp -o findParetoSet.o
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include -fPIC -Wall -g -O2 -c kintersection.cpp -o kintersection.o
clang++ -std=gnu++11 -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 MinimumRcpp.so RcppExports.o findParetoSet.o kintersection.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
installing to /Library/Frameworks/R.framework/Versions/3.5/Resources/library/MinimumRcpp/libs
** R
** byte-compile and prepare package for lazy loading
Error in asNamespace(ns) : not a namespace
Error : unable to load R code in package ‘MinimumRcpp’
ERROR: lazy loading failed for package ‘MinimumRcpp’
* removing ‘/Library/Frameworks/R.framework/Versions/3.5/Resources/library/MinimumRcpp’
Exited with status 1.
Of course, nothing changed in the code. However, I do have the following .gitignore files:
# /.gitignore
inst/lib
.Rproj.user ## Could this be an issue?
.Rproj.user/ ## Could this be an issue?
.Rproj.user/** ## Could this be an issue?
src/*.o
src/*.so
src/*.dll
src/symbols.rds
.Rhistory
.RData
.DS_Store
## QtCreator
Rcpp.pro
Rcpp.pro.user
*.autosave
.#*
*.tar.gz
vignettes/*_cache
## GNU global
GPATH
GRTAGS
GTAGS
##
local/
## docker helpers
docker/*sh
docker/*/*.sh
# /.Rbuildignore
debian
inst/doc/html
inst/doc/latex
inst/doc/man
inst/doc/jss.bst
vignettes/jss.bst
^.*\.Rproj$
^\.Rproj\.user$
vignettes/notyet
doxyfile
\.travis\.yml
\.dir-locals\.el
\.clang_format
vignettes/getCurrentVersionsOfCitedPackages.R
^Contributing.md$
^\.codecov\.yml$
inst/include/Rcpp.h.new
inst/include/Rcpp.h.old
LICENSE
.*\.tar\.gz$
\.editorconfig
docker
^GPATH
^GRTAGS
^GTAGS
^local
vignettes/Makefile
vignettes/rmd
\.github
Is it possible that some package data that was necessary for building the package was left behind and is causing the current issues?
Any assistance would be appreciated. Additionally, any advice on how to debug this would help as the current output messages are quite nebulous and are leaving me directionless.
Here is the GitHub repo for reproducibility: https://github.com/eduardoRubioG/MinimumRcpp

Rookie mistake, but a serious one:
never ever keep script files in your R/ directory
everything (and we mean everything) in the directory gets sourced (provided it looks like R code, _i.e. end in .R)
you left a script with a library(MinimumRcpp) call in there so now your package byte-code compilation wants to source itself --> not a good plan
in short, keep such scripts but put them in e.g. local/ and exclude local/ via .Rbuildignore.
Plus an important style lesson
do not leave rm(list = ls(all = TRUE)) in your code
So if you do mv R/script.R R/script.R.txt and rebuild, all is good.
(I get a half-dozen warnings because n is not a size_t but compared to one. You may want to cast it earlier.)

Related

Shared .so file not found when loading R package with Rcpp component Linux systems

I apologize in advance if this question has been asked before but I haven't been able to find a suitable answer after days of searching.
I have a package that has a couple of small functions written in C++ using Rcpp (package can be found here). When trying to install this package on Linux systems (both local install and via Github Actions - Ubuntu 18.04, 20.04) the compiling procedure was successful but R cannot load the package because an .so file was not found. Mac and Windows systems work fine and there were no R CMD CHECK issues when tested on these systems.
I was wondering if there is any way to solve this issue? I was thinking of adding something in .Makevars but I don't know what to add.
Thank you so much in advance.
─ installing *source* package ‘CBEA’ ...
** using staged installation
** libs
g++ -std=gnu++11 -I"/usr/local/lib/R/include" -DNDEBUG -I'/__w/_temp/Library/Rcpp/include' -I/usr/local/include -fpic -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -Wall -pedantic -c RcppExports.cpp -o RcppExports.o
g++ -std=gnu++11 -I"/usr/local/lib/R/include" -DNDEBUG -I'/__w/_temp/Library/Rcpp/include' -I/usr/local/include -fpic -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -Wall -pedantic -c gmeans.cpp -o gmeans.o
g++ -std=gnu++11 -shared -L/usr/local/lib/R/lib -L/usr/local/lib -o CBEA.so RcppExports.o gmeans.o -L/usr/local/lib/R/lib -lR
installing to /tmp/RtmpwFufM0/Rinst8982598ffd0c/00LOCK-CBEA/00new/CBEA/libs
** R
** data
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
Error: package or namespace load failed for ‘CBEA’ in library.dynam(lib, package, package.lib):
shared object ‘cbea.so’ not found
Error: loading failed
Execution halted
ERROR: loading failed
─ removing ‘/tmp/RtmpwFufM0/Rinst8982598ffd0c/CBEA’
Your (generated) NAMESPACE file has two entries referring to the package:
useDynLib(CBEA, .registration = TRUE)
useDynLib(cbea, .registration = TRUE)
Remove the second one (i.e. remove whereever you tell roxygen2 to add it, and you will be fine. The rror message you see and quote indicates a case issue as on Linux, cbea.so is different from CBEA.so (whereas macOS and Windows let you get away with it).

Unable to install packages via renv::restore() ("R was unable to find one or more FORTRAN libraries during compilation.")

I've downloaded an old repo on a new laptop (running on MacOS Catalina) and attempted to run a R script.
The repo has a renv lock file - meaning, I should be able to restore all dependencies as they were.
But as I run the renv::restore() command, this error message appears:
Installing pbapply [1.4-3] ...
OK [linked cache]
Installing mvtnorm [1.1-2] ...
FAILED
Error installing package 'mvtnorm':
===================================
* installing to library ‘~/renv/staging/2’
* installing *source* package ‘mvtnorm’ ...
** package ‘mvtnorm’ successfully unpacked and MD5 sums checked
** libs
clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c C_FORTRAN_interface.c -o C_FORTRAN_interface.o
clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c miwa.c -o miwa.o
gfortran -fPIC -g -O2 -c mvt.f -o mvt.o
clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c mvtnorm-init.c -o mvtnorm-init.o
gfortran -fPIC -g -O2 -c tvpack.f -o tvpack.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 mvtnorm.so C_FORTRAN_interface.o miwa.o mvt.o mvtnorm-init.o tvpack.o -L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin15/6.1.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: warning: directory not found for option '-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin15/6.1.0'
ld: warning: directory not found for option '-L/usr/local/gfortran/lib'
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mvtnorm.so] Error 1
ERROR: compilation failed for package ‘mvtnorm’
* removing ‘~/renv/staging/2/mvtnorm’
------------------------------------------------------------------------------
R was unable to find one or more FORTRAN libraries during compilation.
This often implies that the FORTRAN compiler has not been properly configured.
Please see https://stackoverflow.com/q/35999874 for more information.
Reason(s):
- 'ld: library not found for -lgfortran'
Error: install of package 'mvtnorm' failed [error code 1]
Traceback (most recent calls last):
13: renv::restore()
12: renv_restore_run_actions(project, diff, current, lockfile, rebuild)
11: renv_install(records)
10: renv_install_staged(records)
9: renv_install_default(records)
8: handler(package, renv_install_impl(record))
7: renv_install_impl(record)
6: withCallingHandlers(renv_install_package_local(record), error = function(e) {
vwritef("\tFAILED")
writef(e$output)
})
5: renv_install_package_local(record)
4: renv_install_package_local_impl(package, path)
3: r_cmd_install(package, path)
2: r_exec_error(package, output, "install", status)
1: stop(error)
I can't make sense of the link in the error message (copied here: Mac OS X R error "ld: warning: directory not found for option").
From my problem search, I've come across some possible solutions that are above my R knowledge level, e.g.:
"[...] creating the file ~/.R/Makevars within my home directory." (Unable to install fortran based packages in R - "gfortran -m32:not found")
"[...] installed gcc & gcc-fortran and that solved it" (Unable to install a package on R)
Similar ideas here: OS X package installation depends on gfortran-4.8
Some guidance here would be deeply appreciated. Please let me know if there's any other info you need (first time posting here).
Thanks in advance!
Solution
Download and install "gfortran-6.1.pkg (OS X 10.11+, signed, 64-bit)" from this page: https://cran.r-project.org/bin/macosx/tools/

Linux issue happens on Windows? "fatal error: fftw3.h: No such file or directory"

I am running MRO 3.5.0 on Win10, and trying to install a package image.CannyImage from bnosac/image on the github. But it keeps reporting fatal errors as following.
* installing *source* package 'image.CannyEdges' ...
** libs
c:/Rtools/mingw_64/bin/g++ -m64 -I"C:/PROGRA~1/MICROS~1/ROPEN~1/R-35~1.0/include" -DNDEBUG -I"C:/Users/eric1/Documents/R/win-library/3.5/Rcpp/include" -I"C:/swarm/workspace/External-R-3.5.0/vendor/extsoft/include" -O2 -Wall -mtune=core2 -c RcppExports.cpp -o RcppExports.o
c:/Rtools/mingw_64/bin/gcc -m64 -I"C:/PROGRA~1/MICROS~1/ROPEN~1/R-35~1.0/include" -DNDEBUG -I"C:/Users/eric1/Documents/R/win-library/3.5/Rcpp/include" -I"C:/swarm/workspace/External-R-3.5.0/vendor/extsoft/include" -O2 -Wall -std=gnu99 -mtune=core2 -c adsf.c -o adsf.o
c:/Rtools/mingw_64/bin/g++ -m64 -I"C:/PROGRA~1/MICROS~1/ROPEN~1/R-35~1.0/include" -DNDEBUG -I"C:/Users/eric1/Documents/R/win-library/3.5/Rcpp/include" -I"C:/swarm/workspace/External-R-3.5.0/vendor/extsoft/include" -O2 -Wall -mtune=core2 -c rcpp_canny.cpp -o rcpp_canny.o
In file included from rcpp_canny.cpp:13:0:
canny.h:8:19: fatal error: fftw3.h: No such file or directory
#include <fftw3.h>
^
compilation terminated.
make: *** [C:/PROGRA~1/MICROS~1/ROPEN~1/R-35~1.0/etc/x64/Makeconf:215: rcpp_canny.o] Error 1
ERROR: compilation failed for package 'image.CannyEdges'
* removing 'C:/Users/eric1/Documents/R/win-library/3.5/image.CannyEdges'
In R CMD INSTALL
Installation failed: Command failed (1)
^
compilation terminated.
Google says I can solve the issue by sudo apt-get remove libfftw3-dev. Unfortunately, I am on Windows, and some advised, apply lib /machine:i386 /def:libfftw3-3.def on cmd mode. Ouch, there is no such command or file called lib.exe on Win10.
Please advise, how I can solve this issue on my Win10+MRO system. Thanks.
Even though this question is already a bit old:
By now (Feb 2020) there are precompiled packages available that work also under Windows:
Canny Edges - Package
and more general:
List of all available packages
I have tried installing them and (at least for me) it worked.

Rcpp installation hangs on Module.cpp on lightweight box [duplicate]

I tried to install RcppEigen on amazon EC2 (on a t1.micro to
make sure It works) & ubuntu 13.04. I tried with both R version was 2.15.2 and 3.0, 32bit ubuntu and 64bit ubuntu. Any time I got the following:
root#ip-10-224-4-239:/home/ubuntu# R CMD INSTALL RcppEigen_0.3.1.2.1.tar.gz
* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘RcppEigen’ ...
** package ‘RcppEigen’ successfully unpacked and MD5 sums checked
** libs
g++ -I/usr/share/R/include -DNDEBUG -I"/usr/local/lib/R/site-library/Rcpp/include" -I../inst/include -fpic -O2 -pipe -g -c RcppEigen.cpp -o RcppEigen.o
g++ -I/usr/share/R/include -DNDEBUG -I"/usr/local/lib/R/site-library/Rcpp/include" -I../inst/include -fpic -O2 -pipe -g -c fastLm.cpp -o fastLm.o
g++: internal compiler error: Killed (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-4.7/README.Bugs> for instructions.
make: *** [fastLm.o] Error 4
ERROR: compilation failed for package ‘RcppEigen’
* removing ‘/usr/local/lib/R/site-library/RcppEigen’
Anyone has an idea on how to get RcppEigen to run on EC2?
(Rcpp installs flawlessly....)
That looks like a "simple" out of memory issue.
C++ can be demanding as is, and complex template code (which Eigen surely is) demands even more resources. Simply try allocating more memory -- otherwise it is hard to kill g++.

R :: override C compiler using Makevars

Our R installation define in:
R$HOME/etc/Makeconf that CC = gcc -std=gnu99
I have one specific package (mix of C++ and C code) that need to be compiled using
CC = gcc
without -std=gnu99
As far as I understood I have 3 ways of doing that:
1) system wide, edit R$HOME/etc/Makeconf
2) per user basis, play with ~/.R/Makevars
3) per package basis, set PACKAGE/src/Makevars
Even if 1 and 2 is not what I want I tested the 3 options, using 1 and 2
R CMD INSTALL -l pack.tgz is OK "gcc -std=gnu99" is corectly replaced by "gcc"
But when using PACKAGE/src/Makevars approach it fails
I must admit that I'm lost at this point, where should I look ?
edit.
this is not really a duplicate with Building R Packages using Alternate GCC
for sure I have read the previous one.is the one that pointed me to Makevars
my key concern is that PACKAGE/src/Makevars is not taken in account for CC=alternate compiler while other one are working prefectly.
I had a similar problem in fortran. Anyway I made a model of your package and I found an half solution. It seems that not all the variables in PACKAGE/src/Makevars are considered and used. In order to make it work I used this Makevars file:
MY_PKG_LIBS =
MY_PKG_CCLAGS = -I/usr/share/R/include -DNDEBUG -fpic -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g
all: $(SHLIB)
hello.o: hello.c
gcc $(MY_PKG_CCLAGS) -c hello.c -o hello.o $(MY_PKG_LIBS)
PKG_LIB = -std=gnu++11
Obviously the hello.c file should be replaced by your_file_name.c. If you can not change the CC and you adopt my workaround, the real problem become that when the shared file .so is created, the compiler flags should be overwritten using PKG_CFLAGS or PKG_CPPFLAGS as stated in Writing R Extensions (again in the Makevars file). In my personal situation (Ubuntu 15.04, R 3.1.2), I tried those and other vars following the guidelines in /etc/R/Makeconf file:
ALL_CFLAGS = $(R_XTRA_CFLAGS) $(PKG_CFLAGS) $(CPICFLAGS) $(SHLIB_CFLAGS) $(CFLAGS)
ALL_CPPFLAGS = $(R_XTRA_CPPFLAGS) $(PKG_CPPFLAGS) $(CPPFLAGS) $(CLINK_CPPFLAGS)
The only thing that worked in adding a flag to the -shared final compiling of the package was adding the library linker flag (As I did originally with my fortran code) PKG_LIB = -std=gnu++11 in the PACKAGE/src/Makevars.
My final result in installing the package is:
Installing package into ‘/home/home/R/x86_64-pc-linux-gnu-library/3.1’
(as ‘lib’ is unspecified)
* installing *source* package ‘question1’ ...
** libs
gcc -I/usr/share/R/include -DNDEBUG -fpic -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g -c hello.c -o hello.o
gcc -std=gnu99 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o question1.so hello.o -std=gnu++11 -L/usr/lib/R/lib -lR
installing to /home/dgarolini/R/x86_64-pc-linux-gnu-library/3.1/question1/libs
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (question1)

Resources