Hi,
I am fairly familiar with R but a total newcomer to C++. Still, I can see the new possibilities that are opened up by the integration of R with C++. I am excited by the new developments in Rcpp and related packages in recent months. I
have decided to at least begin by wetting my feet in these topics.
If this is not the forum for the questions that I
am raising here, please let me know whom I should contact.
Right now, I am on a PC with win XP. I would like to have some help in knowing what tools I need to get started. I have Rtools 2.11 installed. Is it
sufficient to have this to access the C compilers? I even have a
seperate installation of MinGW. The following commands show the versions of gcc and g++ (and g77 too) :
system("gcc --version")
gcc.exe (GCC)
4.2.1-sjlj (mingw32-2)
system("g++ --version")
G__~1.EXE
(GCC) 4.2.1-sjlj (mingw32-2)
system("g77 --version")
GNU Fortran (GCC) 3.4.5 (mingw special)
Copyright (C) 2004 Free Software Foundation, Inc.
But I am confused by
the MingW installation procedure. I tried to install MinGW with the most recent
version (5.1.6) from SourceForge. But this led to a older version of gcc and g++ (v 3.4.5 if I remember correctly). And it was difficult to
locate the MSYS installation file. My Norton Internet security program
warned of security risks with the file that I located. I have now gone
back to the installation with MinGW 5.1.4 and MSYS 1.0.1.
I
tried to test my set of tools with the examples in the reference manual
of the inline package. Except for the fortran bit, I find that I am able to run the commands. I am reproducing here the results from the R
console. How do I solve the problem that I experience with Fortran code? What am I missing? Are the results OK for the other commands? How can I upgrade to the latest version of Mingw? I find some of the
documentation on the Mingw site a little too difficult to follow? Can I
use the mingw files on Rtools (and upgrade to Rtools 2.12)?
I first show the results with
sessionInfo() and then show the results from running the inline
examples.
########################################
sessionInfo()
R version 2.11.0 (2010-04-22)
i386-pc-mingw32
locale:
[1] LC_COLLATE=Swedish_Sweden.1252 LC_CTYPE=Swedish_Sweden.1252
LC_MONETARY=Swedish_Sweden.1252 LC_NUMERIC=C
[5]
LC_TIME=Swedish_Sweden.1252
attached base packages:
[1]
grDevices datasets splines graphics stats tcltk utils
methods base
other attached packages:
[1]
RcppArmadillo_0.2.3 Rcpp_0.8.2 inline_0.3.5
svSocket_0.9-48 TinnR_1.0.3 R2HTML_2.0.0
Hmisc_3.8-0
[8] survival_2.35-8 rcom_2.2-1
rscproxy_1.3-1
loaded via a namespace (and not attached):
[1] cluster_1.12.3 grid_2.11.0 lattice_0.18-5 svMisc_0.9-57
tools_2.11.0
###########################################
library(inline)
Warning message:
package 'inline' was built under R version 2.11.1
x <- as.numeric(1:10)
n <-
as.integer(10)
code <- "
+ integer i
+ do 1 i=1, n(1)
+ 1 x(i) = x(i)**3
+ "
cubefn <-
cfunction(signature(n="integer", x="numeric"), code,
convention=".Fortran")
ERROR(s) during compilation: source code
errors or compiler configuration errors!
Program source:
1:
2: SUBROUTINE file678418be ( n, x )
3: INTEGER n(*)
4: DOUBLE PRECISION x(*)
5: integer i
6: do 1 i=1, n(1)
7: 1 x(i) = x(i)**3
8:
9: RETURN
10: END
Error in compileCode(f, code, language, verbose) :
Compilation ERROR,
function(s)/method(s) not created!
cubefn(n, x)$x
Error:
could not find function "cubefn"
sigSq <-
signature(n="integer", x="numeric")
codeSq <- "
+ for (int i=0; i < *n; i++) {
+ x[i] = x[i]*x[i];
+ }"
setCMethod(c("squarefn", "quadfn"), list(sigSq, sigQd),
+
list(codeSq, codeQd), convention=".C")
Warning message:
In
getPackageName(environment(fdef)) :
Created a package name,
"2010-07-01 16:05:30", when none found
+ fx <- cxxfunction( signature(x = "integer", y = "numeric" ) , '
+ return wrap( as<int>(x) * as<double>(y) ) ;
+ ', plugin = "Rcpp" )
+ fx( 2L, 5 )
+ }
Loading required package: Rcpp
[1] 10
Warning message:
package 'Rcpp' was built under R version
2.11.1
fx(6L,7)
[1] 42
if( require( RcppArmadillo )
){
+ fx <- cxxfunction( signature(x = "integer", y = "numeric" ) , '
+ int dim = as<int>( x ) ;
+ arma::mat z =
as<double>(y) * arma::eye<arma::mat>( dim, dim ) ;
+
return wrap( arma::accu(z) ) ;
+ ', plugin = "RcppArmadillo" )
+
fx( 2L, 5 )
+ }
Loading required package: RcppArmadillo
[1] 10
Warning message:
package 'RcppArmadillo' was built under R version 2.11.1
###########################################
I would appreciate any help that I can get.
Thanking you,
Ravi
PS : Instead of sending a long stream of text, I attempted to get the
output in a file with the following commands :
setwd("M:\\CtoRf\\Rcpp")
sink("inlineExOut.txt") # send output to inlineExOut.txt
source("inlineExamples.r",echo=TRUE) #inlineExamples.r is the file with the script
sink()
This
gave the following error :
Error in compileCode(f, code, language,
verbose) :
Compilation ERROR, function(s)/method(s) not created!
Is there any workaround?