global_local: Global and local variables

get_globalR Documentation

Global and local variables

Description

Global and local variables are useful when you want to compute some variable from the participant's responses and store this variable for later use.

Usage

get_global(key, state)

set_global(key, value, state, allow_dots = FALSE)

get_local(key, state)

set_local(key, value, state, allow_dots = FALSE)

assert_global_is_null(key, state)

Arguments

key

The variable's key (character scalar).

state

The participant's state object.

value

Value to set the variable to.

allow_dots

Set to TRUE to allow keys beginning with periods, which are typically reserved for internal use by psychTestR.

Details

You can set global or local variables within expressions that are run during the participant's testing session, for example in code blocks (code_block), or during on_complete functions (see page). The variable will be preserved for the duration of the participant's testing session until it is overwritten.

For tests without modules, global and local variables behave the same. The difference between local and global variables is that local variables are encapsulated within a given module (begin_module), and are wiped at the end of the module (end_module).

Use get_global and get_local to get variables. Use set_global and set_local to set them.

assert_global_is_null throws an error if the specified global variable is not NULL. This is useful to catch cases where the code would otherwise overwrite a pre-existing variable.

Value

get_global and get_local return the respective variables with key equal to key, or NULL if the variable has yet to be defined.


pmcharrison/psychTestR documentation built on Nov. 10, 2024, 9:03 p.m.