Hi Christopher/ Dirk,

Thank you very much for your replys. I think the idea of using inline as you
suggest is the best way to start off with using c++ with R. I went through
your examples and also plenty I found on the net. I have been trying to run
some of these over the past few days, but have consistently been getting
this error message as soon as I run the 'cfunction' or 'cxxfunction'. For
example, in the syntax that Dirk sent,
R> library(inline) #Runs perfectly
 R> src <- 'std::cout << "Hello C++_From_R World" << std::endl;
return(Rcpp::wrap(42));' #Runs perfectly
 R> rohit <- cxxfunction(signature(), src, plugin="Rcpp")
Now, as soon as I run this line, R spills a whole lot of text out and gives
an error and a warning:
ERROR(s) during compilation: source code errors or compiler configuration
errors!
Program source:
  1:
  2: // includes from the plugin
  3:
  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 file59046688( ) ;
 24: }
 25:
 26: // definition
 27:
 28: SEXP file59046688(  ){
 29: BEGIN_RCPP
 30: std::cout << "Hello C++_From_R World" << std::endl;
return(Rcpp::wrap(42));
 31: END_RCPP
 32: }
 33:
 34:
Error in compileCode(f, code, language = language, verbose = verbose) :
  Compilation ERROR, function(s)/method(s) not created!
In addition: Warning message:
running command 'C:\PROGRA~1\R\R-212~1.1/bin/i386/R CMD SHLIB
file59046688.cpp 2> file59046688.cpp.err.txt' had status 1

The "file59046688.cpp 2"  changes every time I run a different function, but
the problem seems to be the same.

I installed and loaded the inline package (0.3.8) and then the Rcpp package
(0.9.0). I also tried reversing the order in which I load these, but still
no luck. I think if I can get just one of these programs to work, I will be
on my way. Can any of you tell me what I might be doing wrong?

For your question on what exacly I require, Christopher - I just need to use
a while loop. I have always been able to substitute for loops with some of
the apply functions in R, but can't seem to be able to replace the while
with a more efficient function. But the things that are required inside the
while loop, I have already implemented in R efficiently. So, I thought of
transfering just the while loop to a language that is faster with loops.

Thanks in advance.
On Mon, Feb 7, 2011 at 6:21 AM, Wray, Christopher <
christopher.wray...@ucl.ac.uk> wrote:

> As Dirk says, using "inline" makes it real simple to start and to prototype
> code.
>
> You mention you have R functions you wish to "call" via Rcpp. Im not
> certain I fully understand what you require here, but it is pretty simple to
> pass R-side functions to C++ via Rcpp, and similarly its simple to send
> compiled functions back to R-side as external pointers (and reuse them
> elsewhere). Here is a toy example using a simple "user" function defined on
> the R-side (user_F).
>
> This is evaluated in R, passed as a "function" parameter to compiled C++
> function and evaluated there, and then a compiled version of the function is
> passed back to R as an external pointer, which you can send back to the C
> side and evaluate:
>
> <R> user_F=function(v){sum((1-v)*exp(-0.5*v))}
>
> cpp <- '
> NumericVector numvec(xvec);
> NumericVector RetVec;
> Function userR_f(fun);
> List ATR;
> typedef SEXP (*h_ptr)(SEXP);
> RetVec = userR_f(numvec);
> ATR["Fn_ptr"]=XPtr<h_ptr> (new h_ptr(&fme));
> ATR["Fn_VAL"]=RetVec;
> return ATR;
> '
> inc<-'
> using namespace Rcpp;
> SEXP fme(SEXP x){
> NumericVector xx(x);
> NumericVector yy=(1-xx)*exp(-0.5*xx);
> double Ans=sum(yy);
> return wrap(Ans);
> }
> '
> <R> FN_CSide <- cxxfunction(signature(xvec = "numeric", fun = "function"
> ),cpp, , plugin = "Rcpp", includes = inc)
>
> cppP <- '
> typedef SEXP (*h_ptr)(SEXP);
> XPtr<h_ptr>p(x);
> NumericVector yy(y);
> yy=(*p)(yy);
> return yy;
> '
> <R> px<-cxxfunction(signature(x = "externalptr", y = "numeric" ),cppP, ,
> plugin = "Rcpp", includes = "using namespace Rcpp; ")
>
> <R> R_side=FN_CSide(seq(1,5),user_F)
> <R> user_F(seq(1,5))
> [1] -1.548486
> <R> R_side
> $Fn_ptr
> <pointer: 0x0964c608>
> $Fn_VAL
> [1] -1.548486
> <R> px(R_side$Fn_ptr,seq(1,5))
> [1] -1.548486
>
> I've broken it out to make the logic explicit. The above is sloppy - and
> makes no attempt to treat memory de/allocation/gc/protection issues (which
> you should consider) especially if you pass objects around, but it does try
> to show you how the mechanics work.
>
> Aside from looking up the Rcpp::sugar functionality used in places above,
> you might also want to look at package "RcppArmadillo", if functions and
> calculations involve linear algebra calcs/matrix operations. Hopefully this
> gives you some food-for-thought to delve further into the
> archives/documentation to find what you need.
> chris
>
>
>
> ________________________________________
> From: rcpp-devel-boun...@r-forge.wu-wien.ac.at [
> rcpp-devel-boun...@r-forge.wu-wien.ac.at] on behalf of Dirk Eddelbuettel [
> e...@debian.org]
> Sent: 06 February 2011 18:18
> To: Dirk Eddelbuettel
> Cc: Rohit Pandey; rcpp-devel
> Subject: Re: [Rcpp-devel] [R] Help with integrating R and c/c++
>
> [ Now resending to rcpp-devel as I had said below I would --Dirk ]
>
> On 6 February 2011 at 20:58, Rohit Pandey wrote:
> | Hi,
> |
> | I have been using R for close to two years now and have grown quite
> | comfortable with the language. I am presently trying to implement an
> | optimization routine in R (Newton Rhapson). I have some R functions that
> | calculate the gradient and hessian (pre requisite matrices) fairly
> | efficiently. Now, I have to call this function iteratively until some
> | convergance criterion is reached. I think the standard method of doing
> this
> | in most programming languages is a while loop. However, I know R can get
> | pretty slow when you use loops. In order to make this efficient, I want
> to
> | transfer this part of my code to a more efficient programming language
> like
> | c++ or c. However, I have been trying to learn this all day without any
> | luck. I found a package called Rcpp that makes this easier. However, it
> | seems some functional knowledge of writing R packages is a pre requisite.
> I
>
> What gave you that impression?
>
> Here is a counter-example, using the packages inline (for cxxfunction) and
> Rcpp:
>
>  R> library(inline)
>  R> src <- 'std::cout << "Hello C++_From_R World" << std::endl;
> return(Rcpp::wrap(42));'
>  R> rohit <- cxxfunction(signature(), src, plugin="Rcpp")
>  R> rohit()
>  Hello C++_From_R World
>  [1] 42
>  R>
>
> This compiled, linked and loaded a C++ routine built from the two-statement
> program submitted as character variable.
>
> The Rcpp documentation, including its eight vignettes, is full of other
> examples. Start with Rcpp-introduction and maybe the Rcpp-FAQ.
>
> | tried to follow the standard manual for doing this, but could not find a
> | simple example to get me started. I know I am supposed to make a cpp file
> | and put it some where before it can be called from R, but I'm confused as
> to
> | how this can be done.
> |
> | My requirement is to start with a parameter vector, update it according
> to
> | the gradient and hessian, check if the parameter satisfies some
> convergance
> | criterion and continue doing this until it does. Is there a way to
> | efficiently do this through an R function (replicate?). The problem is
> that
> | the number of iterations is not fixed. If there is no function in R, is
> | there a way I can quickly use Rcpp or some thing to have this last part
> of
> | my code in a C or C++ program which repeatedly calls my R functions for
> | updating the parameters?
>
> Give the example above a first try, and then read some more. The archives
> of
> the rcpp-devel (CC'ed; post there for follow-ups after subscribing) list
> are
> full of examples, and the CRAN page for Rcpp lists almost two dozen other
> packages using Rcpp giving you plenty of examples should you want to write
> a
> package using Rcpp. Several of these packages do optimization giving you
> examples of you to pass parameters etc pp.
>
> Hope this helps, Dirk
>
> |
> | --
> | Thanks in advance,
> | Rohit
> | Mob: 91 9819926213
> |
> |       [[alternative HTML version deleted]]
> |
> | ______________________________________________
> | R-help@r-project.org mailing list
> | https://stat.ethz.ch/mailman/listinfo/r-help
> | PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> | and provide commented, minimal, self-contained, reproducible code.
>
> --
> Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com
> _______________________________________________
> Rcpp-devel mailing list
> rcpp-de...@lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel




-- 
Thanks,
Rohit
Mob: 91 9819926213

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to