nfVar: Access or set a member variable of a nimbleFunction

View source: R/nimbleFunction_Rexecution.R

nfVarR Documentation

Access or set a member variable of a nimbleFunction

Description

Access or set a member variable of a specialized nimbleFunction, i.e. a variable passed to or created during the setup function that is used in run code or preserved by setupOutputs. Works in R for any variable and in NIMBLE for numeric variables.

Usage

nfVar(nf, varName)

nfVar(nf, varName) <- value

Arguments

nf

a specialized nimbleFunction, i.e. a function returned by executing a function returned from nimbleFunction with setup arguments

varName

a character string naming a variable in the setup function.

value

value to set the variable to.

Details

Internal way to access or set a member variable of a nimbleFunction created during setup. Normally in NIMBLE code you would use nf$var instead of nfVar(nf, var).

When nimbleFunction is called and a setup function is provided, then nimbleFunction returns a function. That function is a generator that should be called with arguments to the setup function and returns another function with run and possibly other member functions. The member functions can use objects created or passed to setup. During internal processing, the NIMBLE compiler turns some cases of nf$var into nfVar(nf, var). These provide direct access to setup variables (member data). nfVar is not typically called by a NIMBLE user or programmer.

For internal access to methods of nf, see nfMethod.

For more information, see ?nimbleFunction and the NIMBLE User Manual.

Value

whatever varName is in the nimbleFunction nf.

Author(s)

NIMBLE development team

Examples

nfGen1 <- nimbleFunction(
    setup = function(A) {
    B <- matrix(rnorm(4), nrow = 2)
    setupOutputs(B) ## preserves B even though it is not used in run-code
   },
   run = function() {
      print('This is A', A, '\n')
})

nfGen2 <- nimbleFunction(
  setup = function() {
    nf1 <- nfGen1(1000)
  },
  run = function() {
      print('accessing A:', nfVar(nf1, 'A'))
      nfVar(nf1, 'B')[2,2] <<- -1000
      print('accessing B:', nfVar(nf1, 'B'))
   })
       
nf2 <- nfGen2()
nf2$run()

nimble documentation built on Sept. 11, 2024, 7:10 p.m.