Description Usage Arguments Value Author(s) References See Also Examples
The code generation mechanism in this package allow for various
types of customization. A type map is a table
that identifies a native type (a target)
and optional operations for converting
between that type and R values.
This function collects the arguments into a map.
Each element applies to a particular native type,
e.g. an int, an unsigned long, a pointer to a structure,
a typedef.
Each element is a list.
The native type is identified in one of 2 ways.
The name of the map element can be the familiar
stringified declaration form of the native type,
e.g. int
, FILE *
.
Alternatively, the element can have a target
element which is either a string in the same form as above,
or an actual type object of class TypeDefinition
.
Having identified the target native type, an element in this map can
have
any of the following elements
coerceRValue,
convertRValue,
convertValueToR.
Each of these can be a string giving the name of an R function or C
routine
as appropriate. The generated code will be a simple call to this
function/routine with the parameter in the code.
That is, if we are trying to convert a C object held in the variable
ans
back to R, and the convertValueToR element is given
as "R_matrix"
, the generated code is R_matrix(ans)
.
If simply pasting the name of a function/routine and the variable name together is too simple, one can provide a function as the value of any of these elements. In that case, the function is called with three arguments: the name of the variable being processed, a list of all the variables being processed giving their names and types, and this type map which can be used for further conversions.
A function can return a simple string and the different functions
use this in their default manner. Alternatively, one can
return a string which is an RCode object and it will be
inserted as-is with no subsequent processing, e.g.
prefixed with "var = "
.
1 |
... |
a collection of map elements. Names can be specified in the call and these can be used to match against the different type objects being looked up in the map. Each element can have elements named target, coerceRValue, convertRValue and convertValueToR. These can be function/routine names or functions that take 3 arguments. |
A list with S3 class TypeMap
to help
identify it in computations..
Duncan Temple Lang
~put references to the literature/web site here ~
createMethodBinding
coerceRValue
convertRValue
convertValueToR
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | typeMap( "FILE *" = list(target = "FILE *",
# Can be a string, e.g. asFILE, but then couldn't get the mode 'r' in.
coerceRValue = function(name, ...) paste("asFILE(", name, ", 'r')"),
convertRValue = "R_openFile",
convertValueToR = NULL
))
# coercion in R is a function and returns an as-is RCode block.
typeMap( "FILE *" = list(target = "FILE *",
coerceRValue = function(name, ...) {
structure(paste("f = as(", name, ", 'FILERef')\n\ton.exit(.Call('R_closeFile', ", name, "))"),
class = "RCode")
}
))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.