5 Calling an R function from C

#include <R.h>
#include <Rinternals.h>

SEXP myRnorm (SEXP n) {
  R_len_t nProtected = 0, *m = INTEGER (n);
  SEXP y = PROTECT (allocVector (REALSXP, *m)); nProtected++;
  SEXP rnorm = install("rnorm");
  y = eval (lang2 (rnorm, ScalarInteger(*m)), R_GlobalEnv);
  UNPROTECT (nProtected);
  return (y);
}
.Call("myRnorm", as.integer(5))
## [1] -1.0409069  0.3787785  2.7300672 -1.0206140 -1.6873518
#include <R.h>
#include <Rinternals.h>

SEXP myFirstN (SEXP n) {
  R_len_t nProtected = 0, *m = INTEGER (n);
  SEXP y = PROTECT (allocVector (REALSXP, *m)); nProtected++;
  SEXP firstN = install("firstN");
  y = eval (lang2 (firstN, ScalarInteger(*m)), R_GlobalEnv);
  UNPROTECT (nProtected);
  return (y);
}
firstN <- function (n) 1:n

.Call("myFirstN", as.integer(5))
## [1] 1 2 3 4 5