Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8828403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:35:56+00:00 2026-06-14T07:35:56+00:00

I’m trying to use R within C++ via RInside. I’m having trouble passing armadillo

  • 0

I’m trying to use R within C++ via RInside. I’m having trouble passing armadillo matrices to R and returning a result. Below I am able to return a results from an R library function, however I get the wrong result. I’m using the skewness function from the moments package as an example which works as should in R. I checked the examples from RInside and i’m still unsure how to use RcppArmadillo. How do I properly pass an armadillo matrix in c++ to R?

    #include <RInside.h>                   
    #include <RcppArmadillo.h>

    using namespace std;
    using namespace arma;

    int main(int argc, char *argv[]) {
        RInside R(argc, argv);  


        string R_libs = "suppressMessages(library(moments));";

        R.parseEvalQ(R_libs);

        mat A = randu<mat>(5,5);

        R["A"] = A;

        string R_skewness = "B <- skewness(A);";
        //this fails
        mat B = Rcpp::as<mat>(R.parseEval(R_skewness)); //terminate called after throwing an instance of 'Rcpp::not_a_matrix'   

        //this works but wrong
        mat B = Rcpp::as<vec>(R.parseEval(R_skewness)); // returns only 1 number, should be 5 ( 1 for each columnn), same result if i change mat B to vec B
        exit(0);
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:35:58+00:00Added an answer on June 14, 2026 at 7:35 am

    The way we implemented as<mat> requires that the R object you pass is a matrix. And in your example B is a vector:

    > A <- matrix( runif(25), ncol = 5)
    > A
               [,1]      [,2]       [,3]       [,4]      [,5]
    [1,] 0.19215339 0.5857249 0.14345222 0.32154176 0.6162155
    [2,] 0.95753898 0.9618379 0.06239842 0.06200197 0.7044018
    [3,] 0.33575790 0.1372804 0.03027635 0.62662467 0.9778451
    [4,] 0.16504957 0.1919765 0.49176372 0.94841456 0.2914772
    [5,] 0.01570709 0.8055231 0.51218581 0.79562809 0.6939380
    > B <- skewness( A )
    > B
    [1]  1.15196587 -0.04547576  0.32186257 -0.30788111 -0.29251009
    

    For conversion to arma::vec I don’t reproduce the behavior you see. The arma::vec has 3 elements:

    require( RcppArmadillo )    ## and make sure you have Rcpp 0.10.0 or later
    
    sourceCpp( code = '
    // [[Rcpp::depends("RcppArmadillo")]]
    
    #include <RcppArmadillo.h>
    
    using namespace arma ; 
    using namespace Rcpp ;
    
    // [[Rcpp::export]]
    List foo( NumericVector x){
        vec B = Rcpp::as<vec>(x); 
    
        return List::create( 
            _["nrows"] = B.n_rows,
            _["ncols"] = B.n_cols
        ) ;
    
    }
    ')
    foo( c(1, 2, 3 ) )
    # $nrows
    # [1] 3
    # 
    # $ncols
    # [1] 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.