Le 28/09/10 08:43, Dmitrij Kudriavcev a écrit :

Hello All,

A simple question.

I get some return from the R in my C++ program (via Rcpp package). The
result come, as SEXP and it should be a simple numeric variable.

How to convert it to double?

The code, what i use:

stringstream ss;
ss<<  "p<- predict(fit_ar11, n.ahead = 2, doplot=FALSE);"
      <<  "p$pred[1]";

SEXP ans;
int iRet = R.parseEval(ss.str().c_str());
if (iRet == 0&&  ans != NULL)
{

     // ???
}

Cheers,
Dima

Hello,

Questions about Rcpp should go to the Rcpp-devel mailing list (cc'ed).

You probably meant to use thsi version of parseEval:

int parseEval(const std::string & line, SEXP &ans); // parse line, return in ans; error code rc

In your example "ans" is never used or initialized, so you have less than zero chances of it to work.


With recent versions of RInside, you may just do :

double res = R.parseEval( ss.str().c_str() ) ;

conversion will work itself out.


why:

parseEval implements the proxy pattern:

RInside::Proxy RInside::parseEval(const std::string & line) {
    SEXP ans;
    int rc = parseEval(line, ans);
    if (rc != 0) {
        throw std::runtime_error(std::string("Error evaluating: ") + line);
    }
    return Proxy( ans );
}

and Proxy has a templated implicit conversion operator:

        class Proxy {
        public:
            Proxy(SEXP xx): x(xx) { };
        
            template <typename T>
            operator T() {
                return ::Rcpp::as<T>(x);
            }
        private:
            Rcpp::RObject x;
        };

Please register to the Rcpp-devel mailing list of you have follow up questions.

Romain

--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/cCmbgg : Rcpp 0.8.6
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
`- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th

______________________________________________
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