Return a javascript function that samples from an array

fn_sample(x, size, replace = FALSE, weights = NULL)

Arguments

x

A vector specifying the possible values

size

The number of values to sample

replace

Sample with replacement? (default = FALSE)

weights

Probability of sampling each item (ignored if replace = FALSE)

Value

Returns a javascript function that samples from an array of values

Details

The fn_sample() is used to return a function that, when called from within a jsPsych experiment, will mirror the behaviour of the sample() function from the base package using th jsPsych randomisation functions to at runtime. The input argument x specifies the set of values from which samples should be drawn, and the size argument specifies the number of samples to be drawn. When replace = TRUE items are sampled with replacement, and when replace = FALSE items are sampled without replacement. When sampling with replacement, the weights argument can be used to specify unequal sampling probabilities.

The current implementation is limited. It does not work when x is a character vector, for example. Note also that the value returned within the jsPsych experiment is always an array (not a scalar), even when size = 1.