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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:44:08+00:00 2026-06-15T23:44:08+00:00

How to pass a pointer to a function from C code to R (using

  • 0

How to pass a pointer to a function from C code to R (using External R) and after call that function from R??

Something like:

C:

typedef void (* FunctionPtr)();
SEXP ans;
PROTECT(ans = /* ?some code? */);
R_tryEval(ans, R_GlobalEnv, NULL);
UNPROTECT(1);

R:

callback_function()

EDIT:
@Romain Francois’s post was very helpful.
myapp code:

namespace
{
    void callback()
    {
        std::cout << "callback\n" << std::flush;
    }
}
class Worker
{
public:
/*...*/
    void initialize(argc, argv)
    {
       Rf_initEmbeddedR(argc, argv);

        SEXP ans, val;
        typedef void (* FunctionPtr)();

        PROTECT(ans = Rf_lang2(Rf_install("source"), Rf_mkString("script.R")));
        R_tryEval(ans, R_GlobalEnv, NULL);
        UNPROTECT(1);

        PROTECT(val = Rf_ScalarInteger((int)(&callback)));
        /* pass the address of the pointer to a function */
        PROTECT(ans = Rf_lang2(Rf_install("setCallback"), val));
        R_tryEval(ans, R_GlobalEnv, NULL);
        UNPROTECT(2);
    }
    void uninitialize()
    {
        Rf_endEmbeddedR(0);
    }
};

R and Rcpp
script.R

###################
sourceCpp("utils.cpp")
###################
callback <- function()
{
  callCallback()
}

utils.cpp

#include <Rcpp.h>

using namespace Rcpp;

typedef void (* Callback)();
static Callback spCallback = 0;

// [[Rcpp::export]]
void callCallback()
{
  if (spCallback) {
    spCallback();
  } else {
    Rprintf("ERROR: callback is not set");
  }
}
// [[Rcpp::export]]
void setCallback(const int address)
{
  spPlaceOrder = (Callback)address;
}
  • 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-15T23:44:08+00:00Added an answer on June 15, 2026 at 11:44 pm

    External pointers are what you are looking for. They ley you encapsulate pointers to arbitrary data strucures. The word data is important here and function pointers are a different beast. If you want to use C++ and Rcpp, what I would suggest is to create a small class that encapsulates the function pointer:

    typedef void (* FunctionPtr)();
    class FunctionPointer {
      FunctionPtr ptr;
    public:
        FunctionPointer( FunctionPtr ptr_) : ptr(ptr_){}
    } ;
    

    and then create an XPtr<FunctionPointer>:

    #include <Rcpp.h>
    using namespace Rcpp ;
    
    // your callback function
    void callback(){
        Rprintf( "hello from callback\n" ) ;
    }
    
    // The function that creates an external pointer to your 
    // callback
    // [[Rcpp::export]]
    XPtr<FunctionPointer> create_ptr(){
        return XPtr<FunctionPointer>( new FunctionPointer(callback) );
    }
    
    // The function that invokes the callback
    // [[Rcpp::export]]
    void invokeCallback( XPtr<FunctionPointer> callback){
        callback->ptr() ;
    }
    

    On the R side, you can use lexical scoping for example to wrap the external pointer into an R function:

    callback <- local( {
        ptr <- create_ptr()
        function(){
            invokeCallback( ptr )
            invisible(NULL)
        } 
    } )
    callback()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I pass a pointer P from function f1 to function f2, and modify
I want to call a native c++ method from java (android) code, and pass
I would like to pass C# reference to C/C++ code(pointer), and than get it
I want to pass an uninitialized object pointer to some method. Within that method
I am trying to pass a character pointer array as an input from C
I have a function that I pass an array into and an int into
Is it possible to pass a pointer to a QObject using QMimeData during drag-and-drop
Currently I'm using Cgo to call C functions from Go. I'm trying to recreate
I'm working on some legacy code and I ran into something that I'm not
I would like to pass some (dll or not) function pointers as arguments to

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.