Error while installing R package in AMI - r

I am using my R studio via an EC2 instance in AWS.
I tried to install package =tidyverse in R stduio. but while installation i am getting an error
Warning in install.packages : installation of package ‘tidyverse’
had non-zero exit status
The downloaded source packages are in
‘/tmp/RtmpcH9Dy9/downloaded_packages’
Could anyone tell me how I could figure this out? Any lead would be helpful
EDIT: Console output
> install.packages("tidyverse")
> Installing package into ‘/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4’
> (as ‘lib’ is unspecified)
> also installing the dependencies ‘mnormt’, ‘psych’, ‘broom’, ‘haven’, ‘modelr’, ‘readr’
>
> trying URL 'https://cran.rstudio.com/src/contrib/mnormt_1.5-5.tar.gz'
> Content type 'application/x-gzip' length 37169 bytes (36 KB)
> ==================================================
> downloaded 36 KB
>
> trying URL 'https://cran.rstudio.com/src/contrib/psych_1.7.8.tar.gz'
> Content type 'application/x-gzip' length 3311758 bytes (3.2 MB)
> ==================================================
> downloaded 3.2 MB
>
> trying URL 'https://cran.rstudio.com/src/contrib/broom_0.4.3.tar.gz'
> Content type 'application/x-gzip' length 1397648 bytes (1.3 MB)
> ==================================================
> downloaded 1.3 MB
>
> trying URL 'https://cran.rstudio.com/src/contrib/haven_1.1.1.tar.gz'
> Content type 'application/x-gzip' length 189925 bytes (185 KB)
> ==================================================
> downloaded 185 KB
>
> trying URL 'https://cran.rstudio.com/src/contrib/modelr_0.1.1.tar.gz'
> Content type 'application/x-gzip' length 83629 bytes (81 KB)
> ==================================================
> downloaded 81 KB
>
> trying URL 'https://cran.rstudio.com/src/contrib/readr_1.1.1.tar.gz'
> Content type 'application/x-gzip' length 233793 bytes (228 KB)
> ==================================================
> downloaded 228 KB
>
> trying URL 'https://cran.rstudio.com/src/contrib/tidyverse_1.2.1.tar.gz'
> Content type 'application/x-gzip' length 61647 bytes (60 KB)
> ==================================================
> downloaded 60 KB
>
> * installing *source* package ‘mnormt’ ...
> ** package ‘mnormt’ successfully unpacked and MD5 sums checked
> ** libs
> gfortran -m64 -fpic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -I/usr/lib64/gfortran/modules -c biv-nt.f -o biv-nt.o
> f951: Warning: Nonexistent include directory ‘/usr/lib64/gfortran/modules’ [-Wmissing-include-dirs]
> biv-nt.f:371:11:
>
> hs = sign( one, dh - r*dk )
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> biv-nt.f:372:11:
>
> ks = sign( one, dk - r*dh )
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> gfortran -m64 -fpic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -I/usr/lib64/gfortran/modules -c sadmvnt.f -o sadmvnt.o
> f951: Warning: Nonexistent include directory ‘/usr/lib64/gfortran/modules’ [-Wmissing-include-dirs]
> sadmvnt.f:976:3:
>
> 10 END DO
> 1
> Warning: Label 10 at (1) defined but not used
> sadmvnt.f:80:15:
>
> INFORM = MVNNIT( N, CORREL, LOWER, UPPER, INFIN, INFIS, D, E )
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:217:38:
>
> IF ( MINCLS .LT. 0 ) SBRGNS = WORK(LENWRK)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:275:21:
>
> RGNCLS = RGNCLS*(MESH(DIVAXN)+1)/MESH(DIVAXN)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:276:21:
>
> NWRGNS = NWRGNS*(MESH(DIVAXN)+1)/MESH(DIVAXN)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:1274:24:
>
> POINTS = PONTRS(SUBTMP)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:1275:24:
>
> POINTP = PONTRS(SUBTMP+1)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:1283:21:
>
> POINTS = PONTRS(SUBTMP)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:1303:21:
>
> POINTS = PONTRS(SUBTMP)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:324:15:
>
> TOP = PONTRS(1)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:331:21:
>
> RGNCLS = RGNCLS*MESH(I)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:336:18:
>
> RGNCLS = RGNCLS*(MESH(DIVAXN)+1)/MESH(DIVAXN)
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:1585:3:
>
> 10 END DO
> 1
> Warning: Label 10 at (1) defined but not used
> sadmvnt.f:1432:15:
>
> INFORM = MVTNIT( N, NU, CORREL, LOWER, UPPER, INFIN, INFIS, D, E )
> 1
> Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1) [-Wconversion]
> sadmvnt.f:1661:0:
>
> & /( 1 + BMIN**2/(NU+I-1) )**( (NU+I-2)/TWO )
>
> Warning: ‘bmin’ may be used uninitialized in this function [-Wmaybe-uninitialized]
> sadmvnt.f:1658:0:
>
> & /( 1 + AMIN**2/(NU+I-1) )**( (NU+I-2)/TWO )
>
> Warning: ‘amin’ may be used uninitialized in this function [-Wmaybe-uninitialized]
> sadmvnt.f:1127:0:
>
> INTEGER S, IXCHNG, LXCHNG, I, L
>
> Warning: ‘lxchng’ may be used uninitialized in this function [-Wmaybe-uninitialized]
> sadmvnt.f:804:0:
>
> END
>
> Warning: ‘__result_bvn’ may be used uninitialized in this function [-Wmaybe-uninitialized]
> sadmvnt.f:762:0:
>
> DOUBLE PRECISION FUNCTION BVN ( LOWER, UPPER, INFIN, CORREL )
>
> note: ‘__result_bvn’ was declared here
> sadmvnt.f:1046:0:
>
> IF ( INFI(I) .EQ. 2 ) Y(I) = ( AMIN + BMIN )/2
>
> Warning: ‘bmin’ may be used uninitialized in this function [-Wmaybe-uninitialized]
> sadmvnt.f:1046:0: Warning: ‘amin’ may be used uninitialized in this function [-Wmaybe-uninitialized]
> gcc -m64 -std=gnu99 -shared -L/usr/lib64/R/lib -L/usr/local/lib64 -o mnormt.so biv-nt.o sadmvnt.o -lgfortran -lm -lquadmath -L/usr/lib64/R/lib -lR
> /usr/bin/ld: cannot find -lgfortran
> /usr/bin/ld: cannot find -lquadmath
> collect2: error: ld returned 1 exit status
> make: *** [mnormt.so] Error 1
> ERROR: compilation failed for package ‘mnormt’
> * removing ‘/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/mnormt’
> Warning in install.packages :
> installation of package ‘mnormt’ had non-zero exit status
> * installing *source* package ‘readr’ ...
> ** package ‘readr’ successfully unpacked and MD5 sums checked
> ** libs
> g++ -m64 -I/usr/include/R -DNDEBUG -I"/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include" -I"/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include" -I/usr/local/include -fpic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -c Collector.cpp -o Collector.o
> In file included from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/shared_ptr.hpp:28:0,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/shared_ptr.hpp:17,
> from Collector.h:5,
> from Collector.cpp:4:
> /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/detail/shared_count.hpp:355:33:
> warning: ‘template<class> class std::auto_ptr’ is deprecated
> [-Wdeprecated-declarations]
> explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
> ^~~~~~~~
> In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
> from /usr/include/c++/7/locale:43,
> from /usr/include/c++/7/iomanip:43,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/RcppCommon.h:61,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/Rcpp.h:27,
> from Collector.cpp:1:
> /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
> template<typename> class auto_ptr;
> ^~~~~~~~
> In file included from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/shared_ptr.hpp:17:0,
> from Collector.h:5,
> from Collector.cpp:4:
> /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/shared_ptr.hpp:256:65:
> warning: ‘template<class> class std::auto_ptr’ is deprecated
> [-Wdeprecated-declarations]
> template< class T, class R > struct sp_enable_if_auto_ptr< std::auto_ptr< T >, R >
> ^~~~~~~~
> In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
> from /usr/include/c++/7/locale:43,
> from /usr/include/c++/7/iomanip:43,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/RcppCommon.h:61,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/Rcpp.h:27,
> from Collector.cpp:1:
> /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
> template<typename> class auto_ptr;
> ^~~~~~~~
> In file included from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/shared_ptr.hpp:17:0,
> from Collector.h:5,
> from Collector.cpp:4:
> /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/shared_ptr.hpp:471:31:
> warning: ‘template<class> class std::auto_ptr’ is deprecated
> [-Wdeprecated-declarations]
> explicit shared_ptr( std::auto_ptr<Y> & r ): px(r.get()), pn()
> ^~~~~~~~
> In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
> from /usr/include/c++/7/locale:43,
> from /usr/include/c++/7/iomanip:43,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/RcppCommon.h:61,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/Rcpp.h:27,
> from Collector.cpp:1:
> /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
> template<typename> class auto_ptr;
> ^~~~~~~~
> In file included from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/shared_ptr.hpp:17:0,
> from Collector.h:5,
> from Collector.cpp:4:
> /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/shared_ptr.hpp:484:22:
> warning: ‘template<class> class std::auto_ptr’ is deprecated
> [-Wdeprecated-declarations]
> shared_ptr( std::auto_ptr<Y> && r ): px(r.get()), pn()
> ^~~~~~~~
> In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
> from /usr/include/c++/7/locale:43,
> from /usr/include/c++/7/iomanip:43,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/RcppCommon.h:61,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/Rcpp.h:27,
> from Collector.cpp:1:
> /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
> template<typename> class auto_ptr;
> ^~~~~~~~
> In file included from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/shared_ptr.hpp:17:0,
> from Collector.h:5,
> from Collector.cpp:4:
> /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/shared_ptr.hpp:567:34:
> warning: ‘template<class> class std::auto_ptr’ is deprecated
> [-Wdeprecated-declarations]
> shared_ptr & operator=( std::auto_ptr<Y> & r )
> ^~~~~~~~
> In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
> from /usr/include/c++/7/locale:43,
> from /usr/include/c++/7/iomanip:43,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/RcppCommon.h:61,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/Rcpp.h:27,
> from Collector.cpp:1:
> /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
> template<typename> class auto_ptr;
> ^~~~~~~~
> In file included from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/shared_ptr.hpp:17:0,
> from Collector.h:5,
> from Collector.cpp:4:
> /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/shared_ptr.hpp:576:34:
> warning: ‘template<class> class std::auto_ptr’ is deprecated
> [-Wdeprecated-declarations]
> shared_ptr & operator=( std::auto_ptr<Y> && r )
> ^~~~~~~~
> In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
> from /usr/include/c++/7/locale:43,
> from /usr/include/c++/7/iomanip:43,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/RcppCommon.h:61,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/Rcpp.h:27,
> from Collector.cpp:1:
> /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
> template<typename> class auto_ptr;
> ^~~~~~~~
> In file included from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/shared_ptr.hpp:17:0,
> from Collector.h:5,
> from Collector.cpp:4:
> /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/shared_ptr.hpp:
> In member function ‘boost::shared_ptr<T>&
> boost::shared_ptr<T>::operator=(std::auto_ptr<_Up>&&)’:
> /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/BH/include/boost/smart_ptr/shared_ptr.hpp:578:38:
> warning: ‘template<class> class std::auto_ptr’ is deprecated
> [-Wdeprecated-declarations]
> this_type( static_cast< std::auto_ptr<Y> && >( r ) ).swap( *this );
> ^~~~~~~~
> In file included from /usr/include/c++/7/bits/locale_conv.h:41:0,
> from /usr/include/c++/7/locale:43,
> from /usr/include/c++/7/iomanip:43,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/RcppCommon.h:61,
> from /home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/Rcpp/include/Rcpp.h:27,
> from Collector.cpp:1:
> /usr/include/c++/7/bits/unique_ptr.h:51:28: note: declared here
> template<typename> class auto_ptr;
> ^~~~~~~~
> virtual memory exhausted: Cannot allocate memory
> make: *** [Collector.o] Error 1
> ERROR: compilation failed for package ‘readr’
> * removing ‘/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/readr’
> Warning in install.packages :
> installation of package ‘readr’ had non-zero exit status
> ERROR: dependency ‘mnormt’ is not available for package ‘psych’
> * removing ‘/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/psych’
> Warning in install.packages :
> installation of package ‘psych’ had non-zero exit status
> ERROR: dependency ‘readr’ is not available for package ‘haven’
> * removing ‘/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/haven’
> Warning in install.packages :
> installation of package ‘haven’ had non-zero exit status
> ERROR: dependency ‘psych’ is not available for package ‘broom’
> * removing ‘/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/broom’
> Warning in install.packages :
> installation of package ‘broom’ had non-zero exit status
> ERROR: dependency ‘broom’ is not available for package ‘modelr’
> * removing ‘/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/modelr’
> Warning in install.packages :
> installation of package ‘modelr’ had non-zero exit status
> ERROR: dependencies ‘broom’, ‘haven’, ‘modelr’, ‘readr’ are not available for package ‘tidyverse’
> * removing ‘/home/deepthi/R/x86_64-redhat-linux-gnu-library/3.4/tidyverse’
> Warning in install.packages :
> installation of package ‘tidyverse’ had non-zero exit status
>
> The downloaded source packages are in
> ‘/tmp/RtmpcH9Dy9/downloaded_packages’

Related

Installing addmtoolbox on Windows : x is not a data.table (RStudio)

I'm trying to install addmtoolbox on Rstudio. every things is fine but at last i got this error:
Error: Vignette re-building failed.
Execution halted
Error: Failed to install 'addmtoolbox' from GitHub:
System command 'Rcmd.exe' failed, exit status: 1, stdout + stderr (last 10 lines):
E> Quitting from lines 80-84 (addmtoolbox_modelfit_walkthrough.Rmd)
E> Error: processing vignette 'addmtoolbox_modelfit_walkthrough.Rmd' failed with diagnostics:
E> x is not a data.table
E> --- failed re-building 'addmtoolbox_modelfit_walkthrough.Rmd'
E>
E> SUMMARY: processing the following file failed:
E> 'addmtoolbox_modelfit_walkthrough.Rmd'
E>
E> Error: Vignette re-building failed.
E> Execution halted
also i'm trying to install package from zip file. installation is successful but when i run addm_preprocess function,
i got same error:
> my.dat = addm_preprocess(choice.dat = addm_data_choice,
+ eye.dat = addm_data_eye,
+ timestep = 10,
+ rtbinsize = 100)
Error in setkeyv(x, cols, verbose = verbose, physical = physical) :
x is not a data.table
would you please help me?
thanks.
addmtoolbox:
https://rdrr.io/github/AlexanderFengler/addmtoolbox/
EDIT:
i found error code:
eye$fixdur = timestep * round(eye$fixdur/timestep)
rts = eye %>% group_by(id) %>% summarize(rt = sum(fixdur))
setkey(rts,id)
choice = choice %>% select(-rt)
choice = choice[rts]
setkey(rts,id) return error:
> rts
# A tibble: 1 x 2
id rt
* <dbl> <dbl>
1 0 0
> setkey(rts,id)
Error in setkeyv(x, cols, verbose = verbose, physical = physical) :
x is not a data.table
solution by : hdkrgr
It looks like you're providing a tibble when a data.table is expected.
You should be able to convert the object with as.data.table.
Alternatively you could also use dtyplyr for unified tibble/data.table
objects. github.com/tidyverse/dtplyr

RStan toolchain error on ubuntu

I installed RStan, had a problem, uninstalled it, and then tried reinstalling it using https://github.com/stan-dev/rstan/wiki/Installing-RStan-on-Mac-or-Linux
However, when I try using rstan, I get this error:
Error in library(rstan) : there is no package called ‘rstan’
I noticed when I run the toolchain diagnostic mentioned on the above page,
fx <- inline::cxxfunction( signature(x = "integer", y = "numeric" ) , '
return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
' )
fx( 2L, 5 ) # should be 10
I get an error:
clang: warning: argument unused during compilation: '-arch x86_64'
clang: warning: argument unused during compilation: '-arch x86_64'
file16777e2261ec.o: file not recognized: File format not recognized
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [file16777e2261ec.so] Error 1
ERROR(s) during compilation: source code errors or compiler configuration errors!
Program source:
1:
2: // includes from the plugin
3: #include <R.h>
4: #include <Rdefines.h>
5: #include <R_ext/Error.h>
6:
7:
8: // user includes
9:
10:
11: // declarations
12: extern "C" {
13: SEXP file16777e2261ec( SEXP x, SEXP y) ;
14: }
15:
16: // definition
17:
18: SEXP file16777e2261ec( SEXP x, SEXP y ){
19:
20: return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
21:
22: Rf_warning("your C++ program does not return anything");
23: return R_NilValue ;
24: }
25:
26:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! clang: warning: argument unused during compilation: '-arch x86_64'
clang: warning: argument unused during compilation: '-arch x86_64'
file16777e2261ec.o: file not recognized: File format not recognized
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [file16777e2261ec.so] Error 1
In addition: Warning message:
running command '/usr/lib/R/bin/R CMD SHLIB file16777e2261ec.cpp 2> file16777e2261ec.cpp.err.txt' had status 1
Error in fx(2L, 5) : could not find function "fx"
What is going wrong? Thank you!

Debugging - Power Calculator Functions and Plots

After several failed attempts at installing the CATS package (2013) and changing R versions, and what not, I decided to work with the source code from here
I created a single R script with all the R functions from the package, I
ran them, and then I had hopes this code would run to plot power in the example at the end of the following code :
super.cats<-function(RR,MAFmax=0.5,MAFmin=0.005,by=50,rep=1536,SNPs=1E6,ncases,ncontrols,ncases2,ncontrols2,alpha=0.05/SNPs,...){
powerList.O<-c()
powerList.J<-c()
powerList.R<-c()
powerList.F<-c()
power.O<-rep(0,length(RR))
power.F<-rep(0,length(RR))
power.J<-rep(0,length(RR))
power.R<-rep(0,length(RR))
MAF<-exp(seq(log(MAFmin),log(MAFmax),by=(log(MAFmax)-log(MAFmin))/by))
for(nmaf in 1:length(MAF)){
for(tal in 1:length(RR)){
if(power.F[tal]>0.995&power.R[tal]>0.995){
power.O[tal]<-1
power.R[tal]<-1
power.J[tal]<-1
power.F[tal]<-1
break
}
temp<-cats(risk=RR[tal],freq=MAF[nmaf],ncases=ncases,ncontrols=ncontrols,ncases2=ncases2,ncontrols2=ncontrols2,alpha=alpha,pimarkers=rep/SNPs,...)
power.O[tal]<-temp$P.one.study
power.J[tal]<-temp$P.joint
power.R[tal]<-temp$P.rep.study
power.F[tal]<-temp$P.first.stage
}
powerList.O<-cbind(powerList.O,power.O)
powerList.J<-cbind(powerList.J,power.J)
powerList.R<-cbind(powerList.R,power.R)
powerList.F<-cbind(powerList.F,power.F)
cat(nmaf," ")
}
cat("\n")
obs<-list(powerList.O=powerList.O,powerList.J=powerList.J,powerList.R=powerList.R,powerList.F=powerList.F,RR=RR,MAF=MAF,ncases=ncases,ncontrols=ncontrols,ncases2=ncases2,ncontrols2=ncontrols2,rep=rep,curve=F)
class(obs)<-"supercats"
return(obs)
}
##############################
if(FALSE){
#heat plot
rr<-seq(1,2,by=0.025)
c<-super.cats(rr,by=length(rr),ncases=765,ncontrols=1274,ncases2=100,ncontrols2=100,alpha=0.001,prevalence=0.01);
plot(c,main="power",file=NULL)
#curves
rr<-seq(1,3,by=0.05)
maf<-c(0.01,0.05,0.2,0.5)
c2<-curve.cats(rr,maf,ncases=765,ncontrols=1274,ncases2=100,ncontrols2=100,alpha=0.001,prevalence=0.01);
plot(c2,main="power2",ylab="Power",xlab="RR",file=NULL,col=1:4)
}
####
"cats" <-
function (freq=0.5,freq2=-1,ncases=500,ncontrols=500,ncases2=500,ncontrols2=500,risk=1.5,risk2=-1,pisamples=-1,prevalence=0.1,prevalence2=-1,additive=0,recessive=0,dominant=0,multiplicative=1,alpha=0.0000001,pimarkers=0.00316)
{ model<-c(additive,recessive,dominant,multiplicative)
if(sum(model==1)!=1)
stop("chose only one model. e.i. one model must be 1 the others 0")
if(sum(model==0)!=3)
stop("chose only one model. e.i. one model must be 1 the others 0")
if(freq<0|freq>1)
stop("freq must be between 0 and 1")
if((freq2<0|freq2>1)&freq2!=-1)
stop("freq2 must be between 0 and 1 (or undefined as -1)")
if((pisamples<0|pisamples>1)&pisamples!=-1)
stop("pisamples must be between 0 and 1")
if((prevalence2<0|prevalence2>1)&prevalence2!=-1)
stop("prevalence2 must be between 0 and 1 (or undefined as -1)")
if(alpha<0|alpha>1)
stop("alpha must be between 0 and 1")
if(prevalence<0|prevalence>1)
stop("prevalence must be between 0 and 1")
if(pimarkers<0|pimarkers>1)
stop("pimarkers must be between 0 and 1")
if(ncases!=as.integer(ncases)|ncases<0)
stop("ncases must be a positive integer")
if(ncases2!=as.integer(ncases2)|ncases2<0)
stop("ncases2 must be a positive integer")
if(ncontrols!=as.integer(ncontrols)|ncontrols<0)
stop("ncontrols must be a positive integer")
if(ncontrols2!=as.integer(ncontrols2)|ncontrols2<0)
stop("ncontrols2 must be a positive integer")
if(risk<0)
stop("risk must be positive")
if(risk2<0&risk2!=-1)
stop("risk2 must be positive(or undefined as -1)")
res<-.Call("cats",
as.double(freq),as.double(freq2),as.integer(ncases),as.integer(ncontrols),
as.integer(ncases2),as.integer(ncontrols2),as.double(risk),as.double(risk2),
as.double(pisamples),as.double(prevalence),as.double(prevalence2),
as.integer(additive),as.integer(recessive),as.integer(dominant),
as.integer(multiplicative),as.double(alpha),as.double(pimarkers))
options<-cbind(freq,freq2,ncases=ncases,ncontrols=ncontrols,ncases2=ncases2,ncontrols2=ncontrols2,risk,risk2,pisamples,prevalence,prevalence2,additive,recessive,dominant,multiplicative,alpha,pimarkers)
result<-list(P.one.study=res[1,1],P.first.stage=res[2,1],P.rep.study=res[3,1],P.joint.min=res[4,1],P.joint=res[5,1],pi=res[6,1],T.one.study=res[7,1],T.first.stage=res[8,1],T.second.stage.rep=res[9,1],T.second.stage.joint=res[10,1],E.Disease.freq.cases1=res[11,1],E.Disease.freq.controls1=res[12,1],E.Disease.freq.cases2=res[13,1],E.Disease.freq.controls2=res[14,1],options=options)
class(result)<-"CATS"
return(result)
}
curve.cats<-function(RR,MAF,rep=1536,SNPs=1E6,ncases,ncontrols,ncases2,ncontrols2,alpha=0.05/SNPs,...){
powerList.O<-c()
powerList.J<-c()
powerList.R<-c()
powerList.F<-c()
power.O<-rep(0,length(RR))
power.F<-rep(0,length(RR))
power.J<-rep(0,length(RR))
power.R<-rep(0,length(RR))
for(nmaf in 1:length(MAF)){
for(tal in 1:length(RR)){
if(power.F[tal]>0.995&power.R[tal]>0.995){
power.O[tal]<-1
power.R[tal]<-1
power.J[tal]<-1
power.F[tal]<-1
break
}
tempo<-cats(risk=RR[tal],freq=MAF[nmaf],ncases=ncases,ncontrols=ncontrols,ncases2=ncases2,ncontrols2=ncontrols2,alpha=alpha,pimarkers=rep/SNPs,...)
power.O[tal]<-tempo$Pone.study
power.J[tal]<-tempo$Pjoint
power.R[tal]<-tempo$Prep.study
power.F[tal]<-tempo$Pfirst.stage
}
powerList.O<-cbind(powerList.O,power.O)
powerList.J<-cbind(powerList.J,power.J)
powerList.R<-cbind(powerList.R,power.R)
powerList.F<-cbind(powerList.F,power.F)
cat(nmaf," ")
}
cat("\n")
obs<-list(powerList.O=powerList.O,powerList.J=powerList.J,powerList.R=powerList.R,powerList.F=powerList.F,RR=RR,MAF=MAF,ncases=ncases,ncontrols=ncontrols,ncases2=ncases2,ncontrols2=ncontrols2,rep=rep,curve=T)
class(obs)<-"supercats"
return(obs)
}
lines.cats<-function(x,type="Replication",col=NULL,lty=2,...){
if(type=="Joint")
power<-x$powerList.J
else if(type=="One")
power<-x$powerList.O
else if(type=="Replication")
power<-x$powerList.R
else if(type=="First")
power<-x$powerList.F
if(x$curve){
if(is.null(col))
col=1:length(x$MAF)
for(nmaf in 1:length(x$MAF))
lines(x$RR,power[,nmaf],col=col[nmaf],lwd=2,lty=lty)
}
else
cat("only for curves \n")
}
rr <- seq(1,2,by=0.05)
maf <- c(0.05,0.1,0.2,0.5)
c2 <- curve.cats(rr,maf,ncases=600,ncontrols=600,ncases2=600,ncontrols2=600, alpha=0.000001,prevalence=0.01);
plot(c2,type="One",main="power2",ylab="Power",xlab="RR",file=NULL,col=1:4)
lines.cats(c2,type="Replication",lty=3)
lines.cats(c2,type="Joint",lty=2)
lines.cats(c2,type="First",lty=4)
legend("left",c("One stage","Joint","Relication","First Stage"),lty=1:4,bty="n")
###
lines.cats<-function(x,type="Replication",col=NULL,lty=2,...){
if(type=="Joint")
power<-x$powerList.J
else if(type=="One")
power<-x$powerList.O
else if(type=="Replication")
power<-x$powerList.R
else if(type=="First")
power<-x$powerList.F
if(x$curve){
if(is.null(col))
col=1:length(x$MAF)
for(nmaf in 1:length(x$MAF))
lines(x$RR,power[,nmaf],col=col[nmaf],lwd=2,lty=lty)
}
else
cat("only for curves \n")
}
####
plot.supercats<-function(x,type="Joint",file="power.pdf",col=NULL,main=paste("POWER N=",x$ncases,":",x$ncontrols,",",x$ncases2,":",x$ncontrols2," rep=",x$rep,sep=""),...){
if(type=="Joint")
power<-x$powerList.J
else if(type=="One")
power<-x$powerList.O
else if(type=="Replication")
power<-x$powerList.R
else if(type=="First")
power<-x$powerList.F
if(!is.null(file))
pdf(file)
#curve
if(x$curve){
if(is.null(col))
col=1:length(x$MAF)
plot(x$RR,power[,1],ylim=c(0,1),main=main,col="transparent",...)
for(nmaf in 1:length(x$MAF)){
lines(x$RR,power[,nmaf],col=col[nmaf],lwd=2)
}
legend(min(x$RR),1,paste("MAF=",x$MAF),col=col,lwd=2,bty="n")
}
else{
#image
if(is.null(col))
col=heat.colors(80)
image(x$RR,x$MAF,power,col=col,main=main,log="y",ylim=c(0.005,.5),ylab="MAF",xlab="RR",...)
legend("topright",paste(1:10*10,"%"),fill=col[1:10*8],bty="n")
}
if(!is.null(file))
dev.off()
}
####
.onLoad=function(libname, pkgname)
{
library.dynam("CATS", pkgname, libname)
}
.onUnload=function(libpath)
{
library.dynam.unload("CATS", libpath)
}
####
"summary.CATS" <-
function(object, ...){
if (!inherits(object, "CATS"))
stop("Not an object of class CATS!")
cat("Options \n")
ob<-t(object$options)
colnames(ob)<-"chosen"
print(ob)
cat("Recommended thresholds:")
print(structure(list("One stage Design"=object$T.one.study,"Stage 1 Threshold"=object$T.first.stage,"Replication Threshold"=object$T.second.stage.rep,"Joint Analysis Threshold"=object$T.second.stage.joint),class="power.htest"))
cat("Eobjectpected disesase allele frequencies")
print(structure(list("Cases in stage 1"=object$E.Disease.freq.cases1,"Controls in stage 1 "=object$E.Disease.freq.controls1,"Cases in stage 2"=object$E.Disease.freq.cases2,"Controls in stage 2"=object$E.Disease.freq.controls2),class="power.htest"))
cat("Expected Power is:")
print(structure(list("For a one-stage study" = signif(object$P.one.study,
3), "For first stage in two-stage study" = signif(object$P.first.stage,
3), "For second stage in replication analysis" = signif(object$P.rep.study,
3), "For second stage in a joint analysis" = signif(object$P.joint,
3), pi = signif(object$pi, 3)), class = "power.htest"))
}
###
"print.CATS" <-
function(x, ...){
if(!inherits(x,"CATS"))
stop("Not an object of class CATS!")
cat("Expected Power is;\n")
print(structure(list("For a one-stage study"=signif(x$P.one.study,3),"For first stage in two-stage study"=signif(x$P.first.stage,3),"For second stage in replication analysis"=signif(x$P.rep.study,3),"For second stage in a joint analysis"=signif(x$P.joint,3),"pi"=signif(x$pi,3)),class="power.htest"))
}
###
"cats" <-
function (freq=0.5,freq2=-1,ncases=500,ncontrols=500,ncases2=500,ncontrols2=500,risk=1.5,risk2=-1,pisamples=-1,prevalence=0.1,prevalence2=-1,additive=0,recessive=0,dominant=0,multiplicative=1,alpha=0.0000001,pimarkers=0.00316)
{
model<-c(additive,recessive,dominant,multiplicative)
if(sum(model==1)!=1)
stop("chose only one model. e.i. one model must be 1 the others 0")
if(sum(model==0)!=3)
stop("chose only one model. e.i. one model must be 1 the others 0")
if(freq<0|freq>1)
stop("freq must be between 0 and 1")
if((freq2<0|freq2>1)&freq2!=-1)
stop("freq2 must be between 0 and 1 (or undefined as -1)")
if((pisamples<0|pisamples>1)&pisamples!=-1)
stop("pisamples must be between 0 and 1")
if((prevalence2<0|prevalence2>1)&prevalence2!=-1)
stop("prevalence2 must be between 0 and 1 (or undefined as -1)")
if(alpha<0|alpha>1)
stop("alpha must be between 0 and 1")
if(prevalence<0|prevalence>1)
stop("prevalence must be between 0 and 1")
if(pimarkers<0|pimarkers>1)
stop("pimarkers must be between 0 and 1")
if(ncases!=as.integer(ncases)|ncases<0)
stop("ncases must be a positive integer")
if(ncases2!=as.integer(ncases2)|ncases2<0)
stop("ncases2 must be a positive integer")
if(ncontrols!=as.integer(ncontrols)|ncontrols<0)
stop("ncontrols must be a positive integer")
if(ncontrols2!=as.integer(ncontrols2)|ncontrols2<0)
stop("ncontrols2 must be a positive integer")
if(risk<0)
stop("risk must be positive")
if(risk2<0&risk2!=-1)
stop("risk2 must be positive(or undefined as -1)")
res<-.Call("cats",
as.double(freq),as.double(freq2),as.integer(ncases),as.integer(ncontrols),
as.integer(ncases2),as.integer(ncontrols2),as.double(risk),as.double(risk2),
as.double(pisamples),as.double(prevalence),as.double(prevalence2),
as.integer(additive),as.integer(recessive),as.integer(dominant),
as.integer(multiplicative),as.double(alpha),as.double(pimarkers),PACKAGE="CATS")
options<-cbind(freq,freq2,ncases=ncases,ncontrols=ncontrols,ncases2=ncases2,ncontrols2=ncontrols2,risk,risk2,pisamples,prevalence,prevalence2,additive,recessive,dominant,multiplicative,alpha,pimarkers)
result<-list(P.one.study=res[1,1],P.first.stage=res[2,1],P.rep.study=res[3,1],P.joint.min=res[4,1],P.joint=res[5,1],pi=res[6,1],T.one.study=res[7,1],T.first.stage=res[8,1],T.second.stage.rep=res[9,1],T.second.stage.joint=res[10,1],E.Disease.freq.cases1=res[11,1],E.Disease.freq.controls1=res[12,1],E.Disease.freq.cases2=res[13,1],E.Disease.freq.controls2=res[14,1],options=options)
class(result)<-"CATS"
return(result)
}
#### EXAMPLE
rr<-seq(1,2,by=0.05)
maf<-c(0.05,0.1,0.2,0.5)
c2<-curve.cats(rr,maf,ncases=600,ncontrols=600,ncases2=600,
ncontrols2=600,alpha=0.000001,prevalence=0.01)
plot(c2,type="One",main="power2",ylab="Power",xlab="RR",file=NULL,col=1:4)
lines.cats(c2,type="Replication",lty=3)
lines.cats(c2,type="Joint",lty=2)
lines.cats(c2,type="First",lty=4)
legend("left",c("One stage","Joint","Relication","First Stage"),lty=1:4,bty="n")
But I get the following error:
Error in .Call("cats", as.double(freq), as.double(freq2), as.integer(ncases), : "cats" not available for .Call() for package "CATS" Called from: cats(risk = RR[tal], freq = MAF[nmaf], ncases = ncases, ncontrols = ncontrols,
ncases2 = ncases2, ncontrols2 = ncontrols2, alpha = alpha,
pimarkers = rep/SNPs, ...)
I have tried to tinker with the code, but the more I change it, the more errors appear. At this point, I would appreciate any kind of help.
Update on what I get when installing the package from R:
install.packages("CATS_1.02.tar.gz")
Warning in install.packages : package ‘CATS_1.02.tar.gz’ is not available (for R version 3.4.1)
library(CATS) Error in library(CATS) : there is no package called ‘CATS’
Update: Error when installing from command line using R CMD INSTALL CATS_1.02.tar.gz :
adris-imac:Desktop gwallace$ R CMD INSTALL CATS_1.02.tar.gz
* installing to library ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library’
* installing *source* package ‘CATS’ ...
** libs clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c CATS.c -o CATS.o In file included from CATS.c:4: ./cats.h:196:27: warning: '&&' within '||' [-Wlogical-op-parentheses] if (z > LOWER_TAIL_ONE && !upper || z > UPPER_TAIL_ZERO)
~~~~~~~~~~~~~~~~~~~^~~~~~~~~ ~~ ./cats.h:196:27: note: place parentheses around the '&&' expression to silence
this warning if (z > LOWER_TAIL_ONE && !upper || z > UPPER_TAIL_ZERO)
^
( ) CATS.c:86:7: error: non-void function 'cats' should return a value
[-Wreturn-type]
return ;
^ CATS.c:106:7: error: non-void function 'cats' should return a value
[-Wreturn-type]
return ;
^ CATS.c:133:7: error: non-void function 'cats' should return a value
[-Wreturn-type]
return ;
^ 1 warning and 3 errors generated. make: *** [CATS.o] Error 1 ERROR: compilation failed for package ‘CATS’
* removing ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library/CATS’ adris-imac:Desktop gwallace$
It would appear the code is breaking on:
res<-.Call("cats",
as.double(freq),as.double(freq2),as.integer(ncases),as.integer(ncontrols),
as.integer(ncases2),as.integer(ncontrols2),as.double(risk),as.double(risk2),
as.double(pisamples),as.double(prevalence),as.double(prevalence2),
as.integer(additive),as.integer(recessive),as.integer(dominant),
as.integer(multiplicative),as.double(alpha),as.double(pimarkers))
.Call is used to call external C/C++ code:
https://stat.ethz.ch/R-manual/R-devel/library/base/html/CallExternal.html
Without this code the R script will not work.
Also I tested installation of the package, and it seems to install fine:
> install.packages("CATS_1.02.tar.gz")
> library(CATS)
> R.version
platform x86_64-redhat-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 3
minor 4.1
year 2017
month 06
day 30
svn rev 72865
language R
version.string R version 3.4.1 (2017-06-30)
nickname Single Candle
> CATS::cats()
$P.one.study
[1] 0.961869
$P.first.stage
[1] 0.9806984
$P.rep.study
[1] 0.8297875
$P.joint.min
[1] 0.9999998
$P.joint
[1] 0.9529604
$pi
[1] 0.5
$T.one.study
[1] 5.326724
$T.first.stage
[1] 2.951729
$T.second.stage.rep
[1] 4.000192
$T.second.stage.joint
[1] 5.30794
$E.Disease.freq.cases1
[1] 0.6
$E.Disease.freq.controls1
[1] 0.4888889
$E.Disease.freq.cases2
[1] 0.6
$E.Disease.freq.controls2
[1] 0.4888889
$options
freq freq2 ncases ncontrols ncases2 ncontrols2 risk risk2 pisamples prevalence prevalence2 additive recessive dominant multiplicative alpha pimarkers
[1,] 0.5 -1 500 500 500 500 1.5 -1 -1 0.1 -1 0 0 0 1 1e-07 0.00316
attr(,"class")
[1] "CATS"
UPDATE
Based on latest clang error maybe try:
R CMD INSTALL --configure-args="CFLAGS=-Wno-return-type CXXFLAGS=-Wno-return-type" CATS_1.02.tar.gz
UPDATE 2
Also try to add the following to ~/.R/Makevars:
CFLAGS=-Wno-return-type
CXXFLAGS=-Wno-return-type
Then re-install package:
R CMD INSTALL --clean --preclean CATS_1.02.tar.gz

RcppArmadillo: Error when calling cppFunction [duplicate]

I am working through the book "Seamless R and C++ Integration with Rcpp". I am using R version 3.1.0 on Ubuntu 12.04. I cannot figure out how to properly link the necessary libraries. I have the following code in R:
R> library(Rcpp)
R> library(RcppArmadillo)
R> suppressMessages(require(inline))
R> code <- '
+ arma::mat coeff = Rcpp::as<arma::mat>(a);
+ arma::mat errors = Rcpp::as<arma::mat>(u);
+ int m = errors.n_rows;
+ int n = errors.n_cols;
+ arma::mat simdata(m,n);
+ simdata.row(0) = arma::zeros<arma::mat>(1, n);
+ for (int row=1; row < m; row++) {
+ simdata.row(row) = simdata.row(row-1)*trans(coeff)
+ + errors.row(row);
+ }
+ return Rcpp::wrap(simdata);
+ '
R> rcppSim <- cxxfunction(signature(a="numeric", u="numeric"),
+ code, plugin="RcppArmadillo")
/usr/bin/ld: cannot find -lgfortran
collect2: error: ld returned 1 exit status
make: *** [file167d1a7cd1ad.so] Error 1
ERROR(s) during compilation: source code errors or compiler configuration errors!
Program source:
1:
2: // includes from the plugin
3: #include <RcppArmadillo.h>
4: #include <Rcpp.h>
5:
6:
7: #ifndef BEGIN_RCPP
8: #define BEGIN_RCPP
9: #endif
10:
11: #ifndef END_RCPP
12: #define END_RCPP
13: #endif
14:
15: using namespace Rcpp;
16:
17:
18: // user includes
19:
20:
21: // declarations
22: extern "C" {
23: SEXP file167d1a7cd1ad( SEXP a, SEXP u) ;
24: }
25:
26: // definition
27:
28: SEXP file167d1a7cd1ad( SEXP a, SEXP u ){
29: BEGIN_RCPP
30:
31: arma::mat coeff = Rcpp::as<arma::mat>(a);
32: arma::mat errors = Rcpp::as<arma::mat>(u);
33: int m = errors.n_rows;
34: int n = errors.n_cols;
35: arma::mat simdata(m,n);
36: simdata.row(0) = arma::zeros<arma::mat>(1, n);
37: for (int row=1; row < m; row++) {
38: simdata.row(row) = simdata.row(row-1)*trans(coeff)
39: + errors.row(row);
40: }
41: return Rcpp::wrap(simdata);
42:
43: END_RCPP
44: }
45:
46:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created!
/usr/bin/ld: cannot find -lgfortran
collect2: error: ld returned 1 exit status
make: *** [file167d1a7cd1ad.so] Error 1
Calls: cxxfunction -> compileCode
In addition: Warning message:
running command '/usr/lib/R/bin/R CMD SHLIB file167d1a7cd1ad.cpp 2>
file167d1a7cd1ad.cpp.err.txt' had status 1
Based on this response to a similar question,
http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2014-February/007245.html
it would appear that I simply need to install
the FORTRAN compiler. However, I have installed gfortran using the Ubuntu package manager and still receive the same error. From terminal:
$ dpkg -s gfortran
Package: gfortran
Status: install ok installed
Priority: optional
Section: devel
Installed-Size: 33
Maintainer: Ubuntu Developers <ubuntu-devel-discuss#lists.ubuntu.com>
Architecture: i386
Source: gcc-defaults (1.112ubuntu5)
Version: 4:4.6.3-1ubuntu5
Provides: fortran-compiler
Depends: cpp (>= 4:4.6.3-1ubuntu5), gcc (>= 4:4.6.3-1ubuntu5), gfortran-4.6
(>= 4.6.3-1~)
Suggests: gfortran-multilib, gfortran-doc
Description: GNU Fortran 95 compiler
This is the GNU Fortran 95 compiler, which compiles Fortran 95 on platforms
supported by the gcc compiler. It uses the gcc backend to generate optimized
code.
This is a dependency package providing the default GNU Fortran 95 compiler.
Original-Maintainer: Debian GCC Maintainers <debian-gcc#lists.debian.org>
I have also been unsuccessful trying to use the CxxFlags() and LdFlags() functions. Any suggestions are greatly appreciated.
Running sourceCpp() produces the following:
R> rcppSim <- sourceCpp("~/Dropbox/Rcpp/rcppSim.cpp")
rcppSim.cpp: In function ‘SEXPREC* file167d1a7cd1ad(SEXP, SEXP)’:
rcppSim.cpp:31:1: error: ‘arma’ has not been declared
rcppSim.cpp:31:11: error: expected ‘;’ before ‘coeff’
rcppSim.cpp:32:1: error: ‘arma’ has not been declared
rcppSim.cpp:32:11: error: expected ‘;’ before ‘errors’
rcppSim.cpp:33:9: error: ‘errors’ was not declared in this scope
rcppSim.cpp:35:1: error: ‘arma’ has not been declared
rcppSim.cpp:35:11: error: expected ‘;’ before ‘simdata’
rcppSim.cpp:36:1: error: ‘simdata’ was not declared in this scope
rcppSim.cpp:36:18: error: ‘arma’ has not been declared
rcppSim.cpp:36:30: error: ‘arma’ has not been declared
rcppSim.cpp:38:47: error: ‘coeff’ was not declared in this scope
rcppSim.cpp:38:52: error: ‘trans’ was not declared in this scope
make: *** [rcppSim.o] Error 1
g++ -I/usr/share/R/include -DNDEBUG -I"/usr/lib/R/library/Rcpp/include"
-I"/usr/lib/R/library/RcppArmadillo/include" -I"/usr/lib/R/library/Rcpp/include"
-fpic -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security
-Werror=format-security -D_FORTIFY_SOURCE=2 -g -c rcppSim.cpp -o rcppSim.o
Error in sourceCpp("~/Dropbox/Rcpp/rcppSim.cpp") :
Error 1 occurred building shared library.
Running the dpkg command gives:
~$ dpkg -l | grep libgfortran | cut -c-75
ii libgfortran-4.7-dev 4.7.3-2ubuntu1~12.04
ii libgfortran-4.8-dev 4.8.1-2ubuntu1~12.04
ii libgfortran3 4.8.1-2ubuntu1~12.04
~$
Can you run the following command, please:
edd#max:~$ dpkg -l | grep libgfortran | cut -c-75
ii libgfortran-4.7-dev:amd64 4.7.3-7ubuntu3
ii libgfortran-4.8-dev:amd64 4.8.1-10ubuntu9
ii libgfortran3:amd64 4.8.1-10ubuntu9
edd#max:~$
You need the libgfortrain-$VERSION-dev package for your machine. At some point having gfortran implied this via r-base-dev and its dependence on build-essentials.
Edit: Version numbers will of course be different on your 12.04 release; this was from a machine running 13.10.
Edit 2, based on your update: Your use of sourceCpp() is incorrect. You are not telling Rcpp that you need Armadillo, so Rcpp responds by saying it does not know Armadillo. You can either use a Rcpp::depends() in the cpp file, or use the plugin= argument.
So here is how I would write the code you have above. You can then just call sourceCpp() on the file which will create a function rcppSim() you can call:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::mat rcppSim(arma::mat coeff, arma::mat errors) {
int m = errors.n_rows;
int n = errors.n_cols;
arma::mat simdata(m,n);
simdata.row(0) = arma::zeros<arma::mat>(1, n);
for (int row=1; row < m; row++) {
simdata.row(row) = simdata.row(row-1)*trans(coeff) + errors.row(row);
}
return simdata;
}

using SHLIB to compile and load standalone Rcpp function

I am trying to compile the following function with SHLIB (saved as foo.cpp):
#include <Rcpp.h>
RcppExport SEXP foo( SEXP x, SEXP y){
Rcpp::NumericVector xx(x), yy(y) ;
int n = xx.size() ;
Rcpp::NumericVector res( n ) ;
double x_ = 0.0, y_ = 0.0 ;
for( int i=0; i<n; i++){
x_ = xx[i] ;
y_ = yy[i] ;
if( x_ < y_ ){
res[i] = x_ * x_ ;
} else {
res[i] = -( y_ * y_) ;
}
}
return res ;
}
I try
$ R CMD SHLIB foo.cpp
/opt/local/bin/g++-mp-4.4 -I/opt/local/lib/R/include -I/opt/local/lib/R/include/x86_64 -I/opt/local/include -fPIC -pipe -O2 -m64 -c foo.cpp -o foo.o
foo.cpp:1:18: error: Rcpp.h: No such file or directory
foo.cpp:3: error: 'RcppExport' does not name a type
make: *** [foo.o] Error 1
How do I include this file, and is this the right way to compile a standalone function with Rcpp? Of course, I have installed Rcpp with install.packages('Rcpp').
Update:
Trying to find the location of Rcpp.h in R I get:
> system.file("lib", "Rcpp.h", package="Rcpp")
[1] ""
>
However,
> Rcpp:::LdFlags()
/opt/local/lib/R/library/Rcpp/lib/x86_64/libRcpp.a>
Update 2:
Looking at http://www.mail-archive.com/r-help#r-project.org/msg79185.html, I tried
$ PKG_CPPFLAGS=`Rscript -e 'Rcpp:::CxxFlags()'` \
> PKG_LIBS=`Rscript -e 'Rcpp:::LdFlags()'` \
> R CMD SHLIB foo.cpp
/opt/local/bin/g++-mp-4.4 -I/opt/local/lib/R/include -I/opt/local/lib/R/include/x86_64 -I/opt/local/lib/R/library/Rcpp/include -I/opt/local/include -fPIC -pipe -O2 -m64 -c foo.cpp -o foo.o
/opt/local/bin/g++-mp-4.4 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/opt/local/lib -o foo.so foo.o /opt/local/lib/R/library/Rcpp/lib/x86_64/libRcpp.a -L/opt/local/lib/R/lib/x86_64 -lR
and it generated foo.o and foo.so. How do I import this in R now?
Update 3:
So it can be loaded from dyn.load as
> dyn.load("foo.so")
> is.loaded("foo")
[1] TRUE
It can be called successfully as as
> .Call("foo",x=as.numeric(c(1,2,3)),y=as.numeric(c(4,5,6)))
[1] 1 4 9
Although the function is not visible as such.
> foo
Error: object 'foo' not found
Your question is clearly addressed in Question 2.4. of the Rcpp-FAQ.
The answer I found is that SHLIB needs to be provided the location of the Rcpp files. This can be done as
$ PKG_CPPFLAGS=`Rscript -e 'Rcpp:::CxxFlags()'` \
> PKG_LIBS=`Rscript -e 'Rcpp:::LdFlags()'` \
> R CMD SHLIB foo.cpp
Then, the compiled file can be loaded in R as
> dyn.load("foo.so")
and it can be called in R as
> .Call("foo",c(1,2,3),c(4,5,6))

Resources