Manipulate S-PLUS Objects from C

DESCRIPTION:

The .Call interface provides a convenient method for manipulating S-PLUS objects from within C.

USAGE:

.Call(...)

REQUIRED ARGUMENTS:

...
Arguments similar to those for the .C function, including the following:

NAME the name of the C function being called.

... the names of the arguments to the C function.

COPY a logical vector specifying which arguments to protect by copying. If omitted, all arguments are copied before modification.

VALUE:

Returns a S-PLUS object, whatever the C function returns.

SIDE EFFECTS:

Side effects of .Call are those of the called function.

DETAILS:

C functions callable by .Call are required to have all arguments and the return value be objects of C type s_object *. They must also include the standard S-PLUS header file, S.h. Any function dealing with the S-PLUS evaluator must declare the evaluator by use of the S_EVALUATOR macro.

REFERENCES:

J.M. Chambers (1998). Programming with Data. Springer-Verlag, New York.

SEE ALSO:

EXAMPLES:

# /* C code example
# Remove leftmost pound sign to create compilable code below
# Then compile and attach the associate S-PLUS chapter */
#
##include "S.h"
# s_object *makeseq(s_object *sobjX)
#{
#  S_EVALUATOR
#  long i, n, xmax, *seq, *x
#  s_object *sobjSeq
#  /* Convert the s_objects into C data types: */
#  sobjX = AS_INTEGER(sobjX)
#  x = INTEGER_POINTER(sobjX)
#  n = GET_LENGTH(sobjX)
#  /* Compute max value: */
#  xmax = x[0]
#  if(n > 1) {
#    for(i=1; i<n; i++) {
#      if(xmax < x[i]) xmax = x[i]
#    }
#  }
#  if(xmax < 0)
#    PROBLEM "The maximum value (%ld) is
#      negative.", xmax ERROR
#  /* Create a new s_object, set its length and get a C integer
#     pointer to it */
#  sobjSeq = NEW_INTEGER(0)
#  SET_LENGTH(sobjSeq, xmax)
#  seq = INTEGER_POINTER(sobjSeq)
#  for(i=0; i<xmax; i++) {
#    seq[i] = i + 1
#  }
#  return(sobjSeq)
#}

makeseq <- function(x){
  x <- as.integer(x)
  .Call("makeseq", x)
}
makeseq(5)