What does the "install" function mean in C syntax? - c

I'm trying to understand the C code called by the R package ucminf. The following code is from the file interface.c found at https://cran.r-project.org/src/contrib/ucminf_1.1-4.tar.gz:
#include <R.h>
#include <Rinternals.h> //R internal structures
#include <R_ext/RS.h> //F77_CALL etc.
// Declare FORTRAN routine for use in C
extern void F77_NAME(ucminf)(int*, double[], double*, double[],
int*,double[],int*,int*,int*,double[],SEXP);
/*-------------------------------------------------------------------------------
Define C functions that calls user defined function in R
*/
void installPar(int nn, double x[], SEXP rho) {
int i;
SEXP PAR = findVarInFrame(rho, install(".x"));
double *xpt = REAL(PAR);
if (LENGTH(PAR) != nn)
error("Dimension mismatch, length(.x) = %d != n = $d", LENGTH(PAR), nn);
for (i = 0; i < nn; i++) xpt[i] = x[i] ;
}
rho is an environment created in R, and it contains the vector .x. My best guess is that the line SEXP PAR = findVarInFrame(rho, install(".x")); is setting PAR equal to .x, but what does the install() command do?
This is such a simple question that I was surprised I couldn't find the answer online - searching for "install c syntax" turned up lots of information about how to install compilers, but I couldn't find anything about the command. Any suggestions for keywords to make my searches more effective would be appreciated.

This code is part of an R extension I think, and therefore the use of install here is a function call to the R C API for Writing Extension. What this does is create the symbol .x in the current symbol table (or return a reference to the existing .x symbol). The linked document does indicate that using install is harmless if the symbol already exists, and is a good way of looking up the symbol if that's what you actually want to do.

Related

useDynLib() error and compileAttributes returns nothing: embedding a C library into an R package

I have a C package which builds a executable with several argument flags. One compiles the code with a Makefile (I know, this needs to change for the R package) and an executable is created to be run via
$ ./codeName -f path/inputfile -o path/outputfile -p ## -s "type"
My goal is to integrate several of the functions used in this C package to be used with an R library. I take a look at some examples on github.com/cran of R packages using C. In Writing R Extensions, it explains how I could use .Call() and Makevars to call the C functions from R. I would like to avoid that like the plague. However, it looks like this would require significant re-writing with SEXP object--so I turn to Rcpp (yeah!)
I create the package Rcpp.package.skeleton("packageName")
Great. Within R, I do the following:
$ R
> library(devtools)
> build() # works!
> install() # works!
> library(packageName)
> rcpp_hello_world()
## expected output
Everything works. Then I add my C package into /src. I then execute Rcpp::compileAttributes() in the package root directory via R--nothing happens and nothing is output, which is expected, as I haven't changed the C code at all yet.
I try installing with the commands above: devtools::build() and devtools::install(). Via the Makefile, it looks like the C code compiles perfectly! But then there's this issue:
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
Error in library.dynam(lib, package, package.lib) :
shared object ‘packageName.so’ not found
Error: loading failed
Execution halted'
ERROR: loading failed
Well, that's somewhat confusing, and I don't know why that has occurred, but the snag is the useDynLib("packageName") in the NAMESPACE. If I remove this, the C code appears to compile and the package installs via the build/install commands above. rcpp_hello_world() still works.
(1) Why does this error ‘packageName.so’ not found appear now, and can I get around it?
(This question has nothing to do with Rcpp.)
Then, I go to a .c file. I add
#include <Rcpp.h>
using namespace Rcpp;
to a *.c file and //[[Rcpp::export]] before a function I would like to import. (I'm not sure that's going to work in *.c, or in a C header file.)
Next, I go to the package root directory, open R and try this:
$ R
> library(Rcpp)
> compileAttributes()
That runs without error. However, no RcppExports.R and RcppExports.cpp were generated. Compiling the C code also results in the error that it cannot find #include <Rcpp.h>.
(2) Why would compileAttributes() not function in this environment? I must be incorrectly using Rcpp and //[[Rcpp::export]] in order to wrap these C functions into R-usable format.
What would you call this function? C code?
int fib(int n) {
if (n < 2) return n;
return fib(n-1) + fib(n-2);
}
It passes as both C and C++ code. So let's call it C code.
You can clearly interface this from R via Rcpp with the following caller:
// [[Rcpp::export]]
int callFib(int n) {
return fib(n);
}
Stick them all together into a C++ file so that Rcpp can operate on them (see comments) and you are all set.
R> library(Rcpp)
R> sourceCpp("/tmp/ex.cpp")
R> callFib(10)
[1] 55
R>
The complete file is below.
#include <Rcpp.h>
int fib(int n) {
if (n < 2) return n;
return fib(n-1) + fib(n-2);
}
// [[Rcpp::export]]
int callFib(int n) {
return fib(n);
}
/*** R
callFib(10)
*/

How to generate a R wrapper for a C function that returns an int*?

I would like to generate a R (programming language) wrapper for the following C function:
int *test(void)
{
int i;
i = 1024;
return (int *) i;
}
To create this wrapper I am using SWIG with the following interface:
%module X
%{
extern int *test(void);
%}
extern int *test(void);
I can successfully create and compile this wrapper. However, when I run the following R code (the wrapper was loaded beforehand):
print(test())
It will give the following error:
Error in getClass(Class, where = topenv(parent.frame())) :
“_p_int” is not a defined class
Calls: print -> test -> new -> getClass
My question is the following: how can I wrap the C test function (more precisely the int * returned by this function)?
SWIG is not so widely used around R. But the inline package can help you.
R> library(inline)
R> foo <- cfunction(signature(i="int"), body="*i = 1024;",
+ language="C", convention=".C")
R> foo(12)$i
[1] 1024
R>
That said, you probably want .Call() instead of .C(), and you should probably look into Rcpp even if you want to use plain C -- the tooling is pretty rich and useful.

R CMD SHLIB to create a DLL from a source .c file which needs an external library (on Windows x64)

First things first, I am very new to C programming and the whole idea of compilation, so I would really appreciate some very straightforward and step-by-step guidance on this.
Here is my problem: I am trying to write some C code that I can dyn.load into R to speed up my R task. My C code would involve some very complex matrix operation that is only available in an external library with the header file "matrix.h" and the static library file "matrix.lib". It would also make use of some basic R header files such as "Rdefines.h", etc. The files "matrix.h" and "matrix.lib" are located at C:\lcc\include and C:\lcc\lib, respectively. Here is a sample test C code:
#include <Rmath.h>
#include <R.h>
#include <Rdefines.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <matrix.h>
void foo(double *cdegree, double *fdegree, int *size) {
int i;
for(i=0; i<*size; i++) {
cdegree[i] = 5.0/9.0*(fdegree[i]-32.0);
}
}
As you can see, this is simple code converting Fahrenheit to Celsius. Although the test code does not make use of anything in the matrix library, the goal here is to be able to include both the R header files and matrix.h from the external library. If I try R CMD SHLIB this C code I get the "no such file or directory" error for trying to include "matrix.h". How can I tell R to compile this with the external library? Everything is done on a Windows 8.1 X64 system.
Honestly, you will find it much easier if you start exploring Rcpp. Here is a link to introduce you to Rcpp. There are many examples to be found throughout the documentation.
f2c.cpp
#include <Rcpp.h>
// [[Rcpp::export]]
void foo(Rcpp::NumericVector fdegree, Rcpp::NumericVector cdegree, int size){
int i;
for(i=0; i < size; i++){
cdegree[i] = 5.0/9.0*(fdegree[i]-32.0);
}
}
R code
library(Rcpp)
sourceCpp("f2c.cpp")
fdegree <- c(98.6, 212, 32)
cdegree <- c(0,0,0)
foo(fdegree, cdegree, length(fdegree))
cdegree
[1] 37 100 0
Naturally this makes some assumptions but it demonstrates how you can quickly use some C code and not fiddle with all the R headers and SHLIB.
Regarding your concern to use some external headers, just simply set the PKG_CXXFLAGS environmental variable to the location of your header(s).
Sys.setenv("PKG_CXXFLAGS" = '-I"path/to/headers"')
followed by the same compilation.
sourceCpp("f2c.cpp")
However, it should be noted that if you are doing more than a few of these functions you should build a package with Rcpp and provide an appropriate Makevars file. You can find further information on Rcpp package development here.

Error using GNU scientific library with Code::Blocks

I am having an extraordinary problem with my current Code::Blocks (GNU GCC compiler) setup. The compiler seems to selectively run some GSL functions, but seems for some reason to have great problems when commanded to execute other GSL functions.
For example, I have lifted the following code from the following destination:
https://www.gnu.org/software/gsl/manual/html_node/Example-programs-for-matrices.html
I assume that because the code is derived from the official GNU website, that the code is correct:
#include <math.h>
#include <stdio.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>
int
main (void)
{
size_t i,j;
gsl_matrix *m = gsl_matrix_alloc (10, 10);
for (i = 0; i < 10; i++)
for (j = 0; j < 10; j++)
gsl_matrix_set (m, i, j, sin (i) + cos (j));
for (j = 0; j < 10; j++)
{
gsl_vector_view column = gsl_matrix_column (m, j);
double d;
d = gsl_blas_dnrm2 (&column.vector);
printf ("matrix column %d, norm = %g\n", j, d);
}
gsl_matrix_free (m);
return 0;
}
From debugging, I have learned that the source of the error is the following line:
d = gsl_blas_dnrm2 (&column.vector);
The compiler crashes at this point and prints the following error message:
Process returned -1073741819 <0xC0000005>
I have spent a lot of time trying to discover the source of the bug but have sadly not had much success. I am generally not sure why there is a crash at all. The debugger prints no warnings or error messages.
I'm going to suggest that perhaps you have mismatched headers and libraries. Perhaps there is more than one version of GSL installed. You have the headers from one version referenced in your source, and the linker is referencing the libs from other version.
I went looking up the typedef of the gsl_vector_view and ended up here, and you may be even able to discern that this version doesn't even support the vector member of that struct type.
You will get this 0xC0000005 error, typically when you use some unitialised pointer. Its not that your pointer is uninitialised here though .. I'd say what's happening is the 'vector' bit of &column.vector is being interpreted as something other than what is intended.
In summary, I think this is some kind of environmental issue, perhaps with your linker settings? There are some details on how to configure these here: http://www.learncpp.com/cpp-tutorial/a3-using-libraries-with-codeblocks/
Hope this helps.

R from C -- Simplest Possible Helloworld

What is the simplest possible C function for starting the R interpreter, passing in a small expression (eg, 2+2), and getting out the result? I'm trying to compile with MingW on Windows.
You want to call R from C?
Look at section 8.1 in the Writing R Extensions manual. You should also look into the "tests" directory (download the source package extract it and you'll have the tests directory). A similar question was previously asked on R-Help and here was the example:
#include <Rinternals.h>
#include <Rembedded.h>
SEXP hello() {
return mkString("Hello, world!\n");
}
int main(int argc, char **argv) {
SEXP x;
Rf_initEmbeddedR(argc, argv);
x = hello();
return x == NULL; /* i.e. 0 on success */
}
The simple example from the R manual is like so:
#include <Rembedded.h>
int main(int ac, char **av)
{
/* do some setup */
Rf_initEmbeddedR(argc, argv);
/* do some more setup */
/* submit some code to R, which is done interactively via
run_Rmainloop();
A possible substitute for a pseudo-console is
R_ReplDLLinit();
while(R_ReplDLLdo1() > 0) {
add user actions here if desired
}
*/
Rf_endEmbeddedR(0);
/* final tidying up after R is shutdown */
return 0;
}
Incidentally, you might want to consider using Rinside instead: Dirk provides a nice "hello world" example on the project homepage.
In you're interested in calling C from R, here's my original answer:
This isn't exactly "hello world", but here are some good resources:
Jay Emerson recently gave a talk on R package development at the New York useR group, and he provided some very nice examples of using C from within R. Have a look at the paper from this discussion on his website, starting on page 9. All the related source code is here: http://www.stat.yale.edu/~jay/Rmeetup/MyToolkitWithC/.
The course taught at Harvard by Gopi Goswami in 2005: C-C++-R (in Statistics). This includes extensive examples and source code.
Here you go. It's the main function, but you should be able to adapt it to a more general purpose function. This example builds an R expression from C calls and also from a C string. You're on your own for the compiling on windows, but I've provided compile steps on linux:
/* simple.c */
#include <Rinternals.h>
#include <Rembedded.h>
#include <R_ext/Parse.h>
int
main(int argc, char *argv[])
{
char *localArgs[] = {"R", "--no-save","--silent"};
SEXP e, tmp, ret;
ParseStatus status;
int i;
Rf_initEmbeddedR(3, localArgs);
/* EXAMPLE #1 */
/* Create the R expressions "rnorm(10)" with the R API.*/
PROTECT(e = allocVector(LANGSXP, 2));
tmp = findFun(install("rnorm"), R_GlobalEnv);
SETCAR(e, tmp);
SETCADR(e, ScalarInteger(10));
/* Call it, and store the result in ret */
PROTECT(ret = R_tryEval(e, R_GlobalEnv, NULL));
/* Print out ret */
printf("EXAMPLE #1 Output: ");
for (i=0; i<length(ret); i++){
printf("%f ",REAL(ret)[i]);
}
printf("\n");
UNPROTECT(2);
/* EXAMPLE 2*/
/* Parse and eval the R expression "rnorm(10)" from a string */
PROTECT(tmp = mkString("rnorm(10)"));
PROTECT(e = R_ParseVector(tmp, -1, &status, R_NilValue));
PROTECT(ret = R_tryEval(VECTOR_ELT(e,0), R_GlobalEnv, NULL));
/* And print. */
printf("EXAMPLE #2 Output: ");
for (i=0; i<length(ret); i++){
printf("%f ",REAL(ret)[i]);
}
printf("\n");
UNPROTECT(3);
Rf_endEmbeddedR(0);
return(0);
}
Compile steps:
$ gcc -I/usr/share/R/include/ -c -ggdb simple.c
$ gcc -o simple simple.o -L/usr/lib/R/lib -lR
$ LD_LIBRARY_PATH=/usr/lib/R/lib R_HOME=/usr/lib/R ./simple
EXAMPLE #1 Output: 0.164351 -0.052308 -1.102335 -0.924609 -0.649887 0.605908 0.130604 0.243198 -2.489826 1.353731
EXAMPLE #2 Output: -1.532387 -1.126142 -0.330926 0.672688 -1.150783 -0.848974 1.617413 -0.086969 -1.334659 -0.313699
I don't think any of the above has answered the question - which was to evaluate 2 + 2 ;). To use a string expression would be something like:
#include <Rinternals.h>
#include <R_ext/Parse.h>
#include <Rembedded.h>
int main(int argc, char **argv) {
SEXP x;
ParseStatus status;
const char* expr = "2 + 2";
Rf_initEmbeddedR(argc, argv);
x = R_ParseVector(mkString(expr), 1, &status, R_NilValue);
if (TYPEOF(x) == EXPRSXP) { /* parse returns an expr vector, you want the first */
x = eval(VECTOR_ELT(x, 0), R_GlobalEnv);
PrintValue(x);
}
Rf_endEmbeddedR(0);
return 0;
}
This lacks error checking, obviously, but works:
Z:\>gcc -o e.exe e.c -IC:/PROGRA~1/R/R-213~1.0/include -LC:/PROGRA~1/R/R-213~1.0/bin/i386 -lR
Z:\>R CMD e.exe
[1] 4
(To get the proper commands for your R use R CMD SHLIB e.c which gives you the relevant compiler flags)
You can also construct the expression by hand if it's simple enough - e.g., for rnorm(10) you would use
SEXP rnorm = install("rnorm");
SEXP x = eval(lang2(rnorm, ScalarInteger(10)), R_GlobalEnv);
I think you can't do much better than the inline package (which supports C, C++ and Fortran):
library(inline)
fun <- cfunction(signature(x="ANY"),
body='printf("Hello, world\\n"); return R_NilValue;')
res <- fun(NULL)
which will print 'Hello, World' for you. And you don't even know where / how / when the compiler and linker are invoked. [ The R_NilValue is R's NULL version of a SEXP and the .Call() signature used here requires that you return a SEXP -- see the 'Writing R Extensions' manual which you can't really avoid here. ]
You will then take such code and wrap it in a package. We had great success with using
inline for the
Rcpp unit tests (over 200 and counting now) and some of the examples.
Oh, and this inline example will work on any OS. Even Windoze provided you have the R package building tool chain installed, in the PATH etc pp.
Edit: I misread the question. What you want is essentially what the littler front-end does (using pure C) and what the RInside classes factored-out for C++.
Jeff and I never bothered with porting littler to Windoze, but RInside did work there in most-recent release. So you should be able to poke around the build recipes and create a C-only variant of RInside so that you can feed expression to an embedded R process. I suspect that you still want something like Rcpp for the clue as it gets tedious otherwise.
Edit 2: And as Shane mentions, there are indeed a few examples in the R sources in tests/Embedding/ along with a Makefile.win. Maybe that is the simplest start if you're willing to learn about R internals.

Resources