Including a personal.h library in C code callable from R - c

I've been struggling to find proper information on the web to solve this problem, in case it is an easy task please guide me through.
My final goal is to write some R functions that call C subroutines with the .Call function. In general there are no problems in doing this when R.h and Rinternals.h are sufficient.
My problem is: I would need to use in the C code some functions that are in a "personal.h" C library. I already compiled this library with gcc, but if I just try to add
#include "personal.h"
at the beginning after
#include <R.h>
#include <Rinternals.h>
like I would do if it was a standalone C file, when I then call any function from that package in the code, while compiling with R CMD SHLIB I get an error message telling me that it was not possible to find that function.
What should I do in order to include a C library in a C routine callable from R?

Read the "Writing R Extensions" manual which came with your copy of R.
Here, you need PKG_CPPFLAGS to tell R about your include files / headers. Later, you will need to tell it about your library.
Look at other small packages using C code as eg my digest package. And yes, there are in fact numerous tutorials on the Web for this too.

Related

Unit testing using testthat for R packages with compiled code

I am currently developing an R package where most of the computational work is done in C code. In particular, I have several complex C routines for which I then wrap with an R function. These I know I can test using testthat because the namespace includes the C function called (I am using roxygen comments to generate the namespace).
The difficulty I am having is that some of these C routines have a few complicated subroutines, the output of which is non-trivial to check. These functions are never called from R directly, so there is no code in my R/ directory that has a roxygen comment that results in the namespace making these functions available. What I want to do is write an R routine that performs the same computation and then use testthat to compare against the output of the C function. I have done this and it works great when I load the entire shared library (using dyn.load), but I don't think this is the proper approach for tests in a package that I intend to submit to CRAN.
My current workaround is to create a dummy function in a file in my R/ directory that then has a roxygen comment for each of the functions I wish to load. That way when I run devtools::document(), these functions are included in the namespace.
Is this the best approach, or is there another method that is better/preferred?

How to call dpois_raw C stats routine from R

I'm trying to figure out a way to call stats package's "dpois_raw" routine rather than the "dpois" wrapper using .Call .External or whatever means.
"dpois_raw" is not listed in the package environment (stats:::C_*) nor when I do getDLLRegisteredRoutines("stats") so I am probably out of luck, but I wonder whether someone R/C expert knows of a workaround.
The dpois_raw routine is provided by the Rmath.h header, and it doesn't appear to actually be exposed as part of the stats package (or otherwise); however, it is made available to C / C++ code through the Rmath.h header.
The simplest way to expose it would be with your own C / C++ code exposing that code, e.g. (code stub)
#include <R.h>
#include <Rmath.h>
SEXP my_dpois_raw(<...>) {
double result = dpois_raw(<...>);
return result;
}
This routine would then be callable from R with something like
.Call("my_dpois_raw", <...>)
See Hadley's r-pkgs section on using compiled code in R packages for some more information on including C / C++ code in an R package.

Turn C code intended to be compiled into R function

I'm trying to learn how to leverage C code in R functions, specifically because I want to be able to use word2vec in R. word2vec is available as C code and, on a linux box, I can use make to compile it and then ./word2vec -options to build the model and do fun things with it. I would like to take the C code, as is, and essentially wrap it into an R function so I could call, word2vec(words) and have it return the calculated matrix.
So, is there a way to include C code which is written to be compiled into an executable or alternatively, just call the compiled executable?

Where is the source code for the GNU C library?

How do I go about finding the source code behind standard C functions (under Linux/Ubuntu 13)?
Case in point, chdir(). I know I have to #include <unistd.h> but then I encounter a bug, and I suppose the source code would help me figure out this bug.
Thanks if you point me out to the correct source - but real thanks if you give me a method for finding the correct source file every time I need one.
The project is online at GNU C Library.
Yes, the source code to the GNU libc is available at https://sourceware.org/git/?p=glibc.git;a=tree

Compiling C code for ODE Model into .so file for R package

I am organizing some R code I have written into a package. This code contains MCMC algorithms for inference on parameters in ordinary differential equation models, so I will be solving the ODE thousands of times. Thus it is necessary to pass the model into the ode function of the deSolve package using compiled code instead of an R function. Normally, I use the commands
system('R CMD SHLIB mymodel.c')
dyn.load(mymodel)
to use the compiled versions. Instead, I would like R to automatically generate the .so files when I install the package. I cannot find a way to do this because these C functions are not for use as an R function. I just need the path to the valid DLL to pass into the ode function. It doesn't seem to make sense to make a wrapper for the ODE model since I can't use the function inside of R, but maybe I am confused. I cannot find a package on CRAN that uses C code in this manner, so maybe it is not possible.

Resources