Rev 6475 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{Foreign}\title{Foreign Function Interface}\usage{.C(name, \dots, NAOK=FALSE, DUP=TRUE, PACKAGE).Fortran(name, \dots, NAOK=FALSE, DUP=TRUE, PACKAGE).External(name, \dots).Call(name, \dots)}\alias{.C}\alias{.Fortran}\alias{.External}\alias{.Call}\arguments{\item{name}{a character string giving the name of a C function orFortran subroutine.}\item{\dots}{arguments to be passed to the foreign function.}\item{NAOK}{if \code{TRUE} then any \code{NA} or \code{NaN} or\code{Inf} values in the arguments are passed on to the foreignfunction. If \code{FALSE}, the presence of \code{NA} or \code{NaN}or \code{Inf} values is regarded as an error.}\item{DUP}{if \code{TRUE} then arguments are ``duplicated'' beforetheir address is passed to C or Fortran.}\item{PACKAGE}{if supplied, confine the search for the \code{name} tothe DLL given by this argument (plus the conventional extension,\code{.so}, \code{.sl}, \code{.dll}, \dots). This is intended toadd safety for packages, which can ensure that no other package canoverride their external symbols by using this argument. Use\code{PACKAGE="base"} for symbols linked in to \R.}}\description{The functions \code{.C} and \code{.Fortran} can be used to make callsto C and Fortran code.\code{.External} can be used to call compiled code that uses \Robjects in the same way as internal \R functions.\code{.Call} can be used call compiled code which makes use ofinternal \R objects. The arguments are passed to the C code as asequence of \R objects. It is included to provide compatibility withS version 4.}\details{For details about how to write code to use with \code{.Call} and\code{.External} see the \file{R-external} manual which can be madefrom files in the \R source tree.}\value{The functions \code{.C} and \code{.Fortran} return a list similar tothe \code{\dots} list of arguments passed in, but reflecting anychanges made by the C or Fortran code.\code{.External} and \code{.Call} return an \R object.These calls are typically made in conjunction with\code{\link{dyn.load}} which links DLLs to \R.}%%-- This note based on BDR's understanding, edited TSL\section{Argument types}{The mapping of the types of \R arguments to C or Fortran argumentsin \code{.C} or \code{.Fortran} is\tabular{lll}{\ R \tab C \tab Fortran\crinteger \tab int * \tab integer\crnumeric \tab double * \tab double precision\cr-- or -- \tab float * \tab real\crcomplex \tab complex * \tab double complex\crlogical \tab int * \tab integer \crcharacter \tab char ** \tab [see below]\crlist \tab SEXP *\tab not allowed\crother \tab SEXP\tab not allowed\cr}Numeric vectors in \R will be passed as type \code{double *} to C(and as \code{double precision} to Fortran) unless (i) \code{.C} or\code{.Fortran} is used, (ii) \code{DUP} is false and (iii) the argumenthas attribute \code{Csingle} set to \code{TRUE} (use \code{\link{as.single}}or \code{\link{single}}). This mechanism is only intended to be use tofacilitate the interfacing of existing C and Fortran code.The C type \code{complex} is defined in \file{Complex.h} as a\code{typedef struct {double r; double i;}}. Fortran type\code{double complex} is an extension to the Fortran standard, andthe availibility of a mapping of \code{complex} to Fortran may becompiler dependent.\emph{Note:} The C types corresponding to \code{integer} and\code{logical} are \code{int}, not \code{long} as in S.The first character string of a character vector is passed as a Ccharacter array to Fortran: that string may be usableas \code{character*255} if its true length is passed separately. Only upto 255 characters of the string are passed back.Functions, expressions, environments and other languageelements are passed as the internal \R pointer type \code{SEXP}. This type isdefined in \code{Rinternals.h} or the arguments can be declaredas generic pointers, \code{void *}. Lists are passed as C arrays of\code{SEXP} and can be declared as \code{void *} or \code{SEXP *}.R functions can be invoked using \code{call_S} or \code{call_R} andcan be passed lists or the simple types as arguments.}%%-- This note by Thomas Lumley, (minimally edited by MM):\note{\emph{\code{DUP=FALSE} is dangerous.}There are three important dangers with \code{DUP=FALSE}. The first is thatgarbage collection may move the object, resulting in the pointerspointing nowhere useful and causing hard-to-reproduce bugs.The second is that if you pass a formal parameter of the callingfunction to \code{.C}/\code{.Fortran} with \code{DUP=FALSE}, it may notnecessarily be copied. You may be able to change not only the localvariable but the variable one level up. This will also be very hard totrace.The third is that lists are passed as a single \R \code{SEXP} with\code{DUP=FALSE}, not as an array of \code{SEXP}. This means theaccessor macros in \code{Rinternals.h} are needed to get at the listelements and the lists cannot be passed to \code{call_S}/\code{call_R}.1. If your C/Fortran routine calls back any \R function including\code{S_alloc}/\code{R_alloc} then do not use \code{DUP=FALSE}. Do noteven think about it. Calling almost any \R function could triggergarbage collection.2. If you don't trigger garbage collection it is safe and useful to set\code{DUP=FALSE} if you don't change any of the variables that might beaffected, e.g.,\code{.C("Cfunction", input=x, output=numeric(10))}.In this case the output variable didn't exist before the call so it can'tcause trouble. If the input variable is not changed in \code{Cfunction} you aresafe.}\section{Header files for external code}{Writing code for use with \code{.External} and \code{.Call} willuse internal \R structures. If possible use just those defined in\file{Rinternals.h} and/or the macros in \file{Rdefines.h},as other header files are not installed and are even morelikely to be changed.}\seealso{\code{\link{dyn.load}}.}\keyword{programming}