| 1 | /* |
| 2 | * Example of a routine making use of the interface defined in |
| 3 | * init.c. The routine will iterate on both arguments passed from R, |
| 4 | * thereby making the R function vectorized. |
| 5 | * |
| 6 | * The code is derived from package actuar and base R. |
| 7 | * |
| 8 | * Copyright (C) 2016 Vincent Goulet |
| 9 | * Copyright (C) 1995--1997 Robert Gentleman and Ross Ihaka |
| 10 | * Copyright (C) 1998--2016 The R Core Team. |
| 11 | * Copyright (C) 2003--2016 The R Foundation |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License as |
| 15 | * published by the Free Software Foundation; either version 3 of the |
| 16 | * License, or (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | * General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 26 | * 02110-1301, USA. |
| 27 | * |
| 28 | * AUTHOR: Vincent Goulet <[email protected]> |
| 29 | */ |
| 30 | |
| 31 | #include <R.h> |
| 32 | #include <Rinternals.h> |
| 33 | #include "locale.h" |
| 34 | #include "pkg.h" |
| 35 | |
| 36 | SEXP pkg_do_foo(SEXP args) |
| 37 | { |
| 38 | SEXP sx, sy; |
| 39 | int i, nx; |
| 40 | double xi, *x, *y; |
| 41 | Rboolean naflag = FALSE; |
| 42 | |
| 43 | if (!isNumeric(CADR(args))) |
| 44 | error(_("invalid arguments")); |
| 45 | |
| 46 | nx = LENGTH(CADR(args)); |
| 47 | if (nx == 0) |
| 48 | return(allocVector(REALSXP, 0)); |
| 49 | |
| 50 | PROTECT(sx = coerceVector(CADR(args), REALSXP)); |
| 51 | PROTECT(sy = allocVector(REALSXP, nx)); |
| 52 | x = REAL(sx); |
| 53 | y = REAL(sy); |
| 54 | |
| 55 | for (i = 0; i < nx; i++) |
| 56 | { |
| 57 | xi = x[i]; |
| 58 | if (ISNA(xi)) |
| 59 | y[i] = NA_REAL; \ |
| 60 | else if (ISNAN(xi)) |
| 61 | y[i] = R_NaN; |
| 62 | else |
| 63 | { |
| 64 | /* this is where the expint routine is used */ |
| 65 | y[i] = pkg_expint_E1(xi, 0); |
| 66 | if (ISNAN(y[i])) naflag = TRUE; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (naflag) |
| 71 | warning(R_MSG_NA); |
| 72 | |
| 73 | SET_ATTRIB(sy, duplicate(ATTRIB(sx))); |
| 74 | SET_OBJECT(sy, OBJECT(sx)); |
| 75 | UNPROTECT(2); |
| 76 | |
| 77 | return sy; |
| 78 | } |