1

In short: How can I call, from within Rccp C++ code, the agrep C internal function that gets called when users use the regular agrep function from base R?

In long: I have found multiple questions here about how to invoke, from within Rcpp, a C or C++ function created for another package (e.g. using C function from other package in Rcpp and Rcpp: Call C function from a package within Rcpp).

The thing that I am trying to achieve, however, is at the same time simpler but also way less documented: it is to directly call, from within Rcpp, a .Internal C function that comes with base R rather than another package, without interfacing with R (that is, without doing what is said in Call R functions in Rcpp). How could I do that for the .Internal C function that lays underneath base R's agrep wrapper?

The specific function I am trying to call here is the agrep internal C function. And for context, what I am ultimately trying to achieve is to speed-up a call to agrep for when millions of patterns must be each checked against each of millions of x targets.

  • 2
    In short, you cannot eaily or directly as @Oliver details: R simply does not expose these internal functions. But see for example this [Rcpp Gallery article](https://gallery.rcpp.org/articles/boost-regular-expressions/) on using Boost regex with Rcpp -- you could find to any of the other regular expression libraries the same way. – Dirk Eddelbuettel Feb 17 '21 at 13:39
  • @DirkEddelbuettel I see, thanks! Quick follow-up: just as I said to @Oliver, what about using `do_agrep` or others that might be declared in `src/include/Internal.h`? –  太晚了 Feb 18 '21 at 23:56
  • 1
    Still no as R Core marks all those functions 'hidden'. If you look more closely at my (arguable large-ish) set of package on CRAN you will find two `RApi*` because one needs to copy the code out to "un-hide" it. And at that point you can also use a general reg.exp engine just as my previous comment suggested. Good luck! – Dirk Eddelbuettel Feb 19 '21 at 00:15

1 Answers1

4

Great question. The long and short of it is "You cant" (in many cases) unless the function is visible in one of the header files in "src/include/". At least not that easily.

Not long ago I had a similar fun challenge, where I tried to get access to the do_docall function (called by do.call), and it is not a simple task. First of all, it is not directly possible to just #include <agrep.c> (or something similar). That file simply isn't available for inclusion, as it is not a part of the "src/include". It is compiled and the uncompiled file is removed (not to mention that one should never "include" a .c file).

If one is willing to go the mile, then the next step one could look at is "copying" and "altering" the source code. Basically find the function in "src/main/agrep.c", copy it into your package and then fix any errors you find.

Problems with this approach:

  1. As documented in R-exts the internal structures of sexprec_info is not made public (this is the base structure for all objects in R). Many internal function use the fields within this structure, so one has to "copy" the structure into your source code, to make it public to your code specifically.
  2. If you ever #include <Rcpp.h> prior to this file, you will need to go through each and every call to internal functions and likely add either R_ or Rf_.
  3. The function may contain calls to other "internal" functions, that further needs to be copied and altered for it to work.
  4. You will also need to get a clear understanding of what CDR, CAR and similar does. The internal functions have a documented structure, where the first argument contains the full call passed to the function, and function like those 2 are used to access parts of the call. I did myself a solid and rewrote do_docall changing the input format, to avoid having to consider this. But this takes time. The alternative is to create a pairlist according to the documentation, set its type as a call-sexp (the exact name is lost to me at the moment) and pass the appropriate arguments for op, args and env.
  5. And lastly, if you go through the steps, and find that it is necessary to copy the internal structures of sexprec_info (as described later), then you will need to be very careful about when you include Rinternals and Rcpp, as any one of these causes your code to crash and burn in the most beautiful and silent way if you include your header and these in the wrong order! Note that this even goes for [[Rcpp::export]], which may indeed turn out to include them in the wrong arbitrary order!

If you are willing to go this far down the drainage, I would suggest carefully reading adv-R "R's C interface" and Chapter 2, 5 and 6 of R-ext and maybe even the R internal manual, and finally once that is done take a look at do_docall from src/main/coerce.c and compare it to the implementation in my repository cmdline.arguments/src/utils/{cmd_coerce.h, cmd_coerce.c}. In this version I have

  1. Added all the internal structures that are not public, so that I can access their unmodified form (unmodified by the current session).
    • This includes the table used to store the currently used SEXP's, that was used as a lookup. This caused a problem as I can't access the modified version, so my code is slightly altered with the old code blocked by the macro #if --- defined(CMDLINE_ARGUMENTS_MAYBE_IN_THE_FUTURE). Luckily the code causing a problem had a static answer, so I could work around this (but this might not always be the case).
  2. I added quite a few Rf_s as their macro version is not available (since I #include <Rcpp.h> at some point)
  3. The code has been split into smaller functions to make it more readable (for my own sake).
  4. The function has one additional argument (name), that is not used in the internal function, with some added errors (for my specific need).

This implementation will be frozen "for all time to come" as I've moved on to another branch (and this one is frozen for my own future benefit, if I ever want to walk down this path again).

I spent a few days scouring the internet for information on this and found 2 different posts, talking about how this could be achieved, and my approach basically copies this. Whether this is actually allowed in a cran package, is an whole other question (and not one that I will be testing out).

This approach goes again if you want to use not-public code from other packages. While often here it is as simple as "copy-paste" their files into your repository.

As a final side note, you mention the intend is to "speed up" your code for when you have to perform millions upon millions of calls to agrep. It seems that this is a time where one should consider performing the task in parallel. Even after going through the steps outlined above, creating N parallel sessions to take care of K evaluations each (say 100.000), would be the first step to reduce computing time. Of course each session should be given a batch and not a single call to agrep.

Oliver
  • 8,169
  • 3
  • 15
  • 37
  • Many thanks for this insightful answer! One last doubt that was left in my mind, is about what you said at the very beginning: "unless the function is visible in one of the header files in "src/include/"". Wouldn't it then be possible somehow to do what I asked through the `do_agrep` function (and FWIW what you wanted in the past with `do_docall`) since those functions are declared in the `src/include/Internal.h`? –  太晚了 Feb 18 '21 at 23:53
  • Just as Dirk said in the comment, it is a no. If you look through the source code for R, you will notice something reappearing again and again. `SEXP attribute_hidden ....`. That `attribute_hidden` is the big problem. Again you can go through the steps as I've explained. Looking at `do_agrep` it is not that complex. But I think a simpler method would be, again as Dirk suggest, using `std::regex` or `boost::regex`. My own code relies on `std::regex` for what it's worth. Although I don't perform any fuzzy-regex search. – Oliver Feb 19 '21 at 08:13