[Rd] bus error on calling nmmin

David Faden dfaden at gmail.com
Fri Apr 14 19:30:49 CEST 2006

2006/4/14, Prof Brian Ripley <ripley at stats.ox.ac.uk>:
> You appear to be trying to call nmmin from a standalone program.
> You linked against -lR, but failed to initialize R, hence the segfault.
> nmmin is not a part of R that is made available except to a running R
> process: it is documented in `Writing R Extensions' for use in R packages.

Thanks. Initializing R does seem to have fixed the problem. In this, I
have followed the code in tests/Embedding, in particular
embeddedRCall.c. I was led to this code by Section 7.1 of "Writing R

Below is the corrected code and an example session:

#include <R_ext/Applic.h>
#include <stdio.h>

double parabola(int n, double *par, void *ex) {
  double xm = par[0] - 1;
  return xm * xm + 13;

 * Copied from tests/Embedded/embeddedRCall.c:
extern int Rf_initEmbeddedR(int argc, char *argv[]);

int main()
  char *argv[]= {"nmminDemo", "--gui=none", "--silent"};
  const int argc = 3;

  double initial[1] = {1.5};
  double result[1];
  double value;
  int convergenceCode;
   * The following values are based on the help
   * page for optim.
  const double abstol = 1e-16;
  const double reltol = 1e-8;
  const double alpha = 1.0; /* reflection factor */
  const double beta = 0.5; /* contraction factor */
  const double gamm = 2.0; /* expansion factor */
  const int trace = 0; /* tracing on */
  int fncount;
  const int maxit = 10000;

  Rf_initEmbeddedR(argc, argv);

  nmmin(1, initial, result, &value, parabola,
	&convergenceCode, abstol, reltol,
	NULL, alpha, beta, gamm,
	trace, &fncount, maxit);
  printf("fncount: %d\n", fncount);
  printf("convergence code: %d\n", convergenceCode);
  printf("min of %f at x = %f\n", value, result[0]);
  return 0;


$ gcc nmminDemo.c -g
-L/Library/Frameworks/R.framework/Versions/2.1.1/Resources/lib -lR
$ R CMD ./a.out
fncount: 24
convergence code: 0
min of 13.000000 at x = 1.000195

David Faden, dfaden at iastate.edu
AIM: pitulx

More information about the R-devel mailing list