View source: R/externalCalls.R
nimbleRcall | R Documentation |
Normally compiled nimbleFunctions call other compiled nimbleFunctions. nimbleRcall enables any R function (with viable argument types and return values) to be called (and evaluated in R) from compiled nimbleFunctions.
nimbleRcall(
prototype,
returnType,
Rfun,
where = getNimbleFunctionEnvironment()
)
prototype |
Argument type information for Rfun. This can be provided as an R function using |
returnType |
Return object type information. This can be provided similarly to |
Rfun |
The name of an R function to be called from compiled nimbleFunctions. |
where |
An optional |
The nimbleFunction
returned by nimbleRcall
can be used in other nimbleFunction
s. When called from a compiled nimbleFunction
(including from a model), arguments will be copied according to the declared types, the function named by Rfun
will be called, and the returned object will be copied if necessary. The example below shows use of an R function in a compiled nimbleModel
.
A nimbleFunction
returned by nimbleRcall
can only be used in a compiled nimbleFunction
. Rfun
itself should work in an uncompiled nimbleFunction
.
A nimbleFunction
that wraps a call to Rfun
with type-declared arguments and return object.
Perry de Valpine
nimbleExternalCall
for calling externally provided C (or other) compiled code.
## Not run:
## Say we want an R function that adds 2 to every value in a vector
add2 <- function(x) {
x + 2
}
Radd2 <- nimbleRcall(function(x = double(1)){}, Rfun = 'add2',
returnType = double(1))
demoCode <- nimbleCode({
for(i in 1:4) {x[i] ~ dnorm(0,1)}
z[1:4] <- Radd2(x[1:4])
})
demoModel <- nimbleModel(demoCode, inits = list(x = rnorm(4)),
check = FALSE, calculate = FALSE)
CdemoModel <- compileNimble(demoModel)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.