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 9156635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:49:03+00:00 2026-06-17T12:49:03+00:00

Consider the following R code, ## ———– R version ———– caller <- function(x=1:3, fun

  • 0

Consider the following R code,

## ----------- R version -----------

caller <- function(x=1:3, fun = "identity", ...){

  ## do some other stuff
  ## ...
  ## then call the function
  eval(call(fun, x))

}

fun1 <- function(x, ...){
  x + x
}

fun2 <- function(x, a = 10) a * x

caller(fun = "fun1")
caller(fun = "fun2")

The user can pass a function name “fun”, that is used by caller. I wish to perform the same task with RcppArmadillo objects (as part of a more complex task, obviously). The function would be defined in C++, and the user selects it at the R level by referring to its name:

caller_cpp(1:3, "fun1_cpp")

or

caller_cpp(1:3, "fun2_cpp")

etc.

Here’s my naive attempt for the caller function, that even fails to compile:

## ----------- C++ version -----------

library(Rcpp)
require( RcppArmadillo )    

sourceCpp( code = '

       // [[Rcpp::depends("RcppArmadillo")]]

       #include <RcppArmadillo.h>

       using namespace arma ; 
       using namespace Rcpp ;


       colvec fun1_cpp(const colvec x)
      {
       colvec y ;
       y = x + x;
       return (y);
      }

       colvec fun2_cpp(const colvec x)
      {
       colvec y ;
       y = 10*x;
       return (y);
      }

     // mysterious pointer business in an attempt 
     // to select a compiled function by its name

      typedef double (*funcPtr)(SEXP);
      SEXP putFunPtrInXPtr(SEXP funname) {
            std::string fstr = Rcpp::as<std::string>(funname);
            if (fstr == "fun1")
                return(Rcpp::XPtr<funcPtr>(new funcPtr(&fun1_cpp)));
            else if (fstr == "fun2")
            return(Rcpp::XPtr<funcPtr>(new funcPtr(&fun2_cpp)));

       }

       // [[Rcpp::export]]
       colvec caller_cpp(const colvec x, character funname)
      {
       Rcpp::XPtr fun = putFunPtrInXPtr(funname);
       colvec y ;
       y = fun(x);
       return (y);
      }

   ')

Edit: adapted the example after following Dirk’s suggestion to look at RcppDE.

  • 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-17T12:49:04+00:00Added an answer on June 17, 2026 at 12:49 pm

    (Sometime you need to use svn log ... on files to see how dated they are…)

    I think a better use case is in my “port” of the C-based DEoptim to Rcpp / RcppArmadillo: RcppDE. In it, I allow the optimization routine to use either an R function (as DEoptim does) or a user-supplied compiled function — which is what you want here as I understand it.

    There is a tiny bit of C++ scaffolding, but you should have no problem following that.

    Edit on 2013-01-21 Below is a complete solution which I have also justed posted as this new post at the Rcpp Gallery — including some comments and sample usage.

    // [[Rcpp::depends(RcppArmadillo)]]
    #include <RcppArmadillo.h>
    
    using namespace arma; 
    using namespace Rcpp;
    
    vec fun1_cpp(const vec& x) {    // a first function 
        vec y = x + x;
        return (y);
    }
    
    vec fun2_cpp(const vec& x) {    // and a second function
        vec y = 10*x;
        return (y);
    }
    
    typedef vec (*funcPtr)(const vec& x);
    
    // [[Rcpp::export]]
    XPtr<funcPtr> putFunPtrInXPtr(std::string fstr) {
        if (fstr == "fun1")
            return(XPtr<funcPtr>(new funcPtr(&fun1_cpp)));
        else if (fstr == "fun2")
            return(XPtr<funcPtr>(new funcPtr(&fun2_cpp)));
        else
            return XPtr<funcPtr>(R_NilValue); // runtime error as NULL no XPtr
    }
    
    // [[Rcpp::export]]
    vec callViaString(const vec x, std::string funname) {
        XPtr<funcPtr> xpfun = putFunPtrInXPtr(funname);
        funcPtr fun = *xpfun;
        vec y = fun(x);
        return (y);
    }
    
    // [[Rcpp::export]]
    vec callViaXPtr(const vec x, SEXP xpsexp) {
        XPtr<funcPtr> xpfun(xpsexp);
        funcPtr fun = *xpfun;
        vec y = fun(x);
        return (y);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: $(document).ready(function() { $(body).append(<div class='outer'><span class='inner'>Click me</span></div>); $(html).click(function(event) { var targetClass
Consider the following code: function func() { var totalWidths = 0; for( var i
Consider the following code. <!DOCTYPE html> <script> console.log(a); function a() {} </script> Notice that
Consider the following code to demonstrate the question: let sequence = Seq.initInfinite (fun _
Consider following JavaScript code (tested in Firefox): function f(a) { if (a == undefined)
Consider the following code: $('.stores').click(function() { console.log($(this).data('latitude')); // -1754.26265626 console.log($(this).data('longitude')); // 65.262518 console.log(themap); //
Consider following code - namespace N1 { class A { //some implementation } class
Consider following code : <p style=margin: 30px 0; padding: 0;>Some text some text some
Consider the following code : #include <iostream> #include <vector> #include <type_traits> // Version A
Consider the following code: function dbTask(q) { mysql = new MySQL(); mysql.host = sqlHost;

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.