Skip to content

[Rcpp-devel] [R] How to convert SEXP to double

1 message · Romain Francois

#
Le 28/09/10 08:43, Dmitrij Kudriavcev a ?crit :
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