Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
76 lines (62 sloc) 2.84 KB
title: "jsPsych integration"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{jsPsych integration}
[jsPsych]( is a powerful library for constructing behavioral experiments in Javascript.
It is possible to embed jsPsych within psychTestR.
This is useful when you want to take advantage of jsPsych's timed stimulus presentation facilities.
psychTestR does not yet provide dedicated helper functions for embedding jsPsych,
partly because of recent changes to jsPsych's API.
However, we give an example below of how this integration can be achieved.
We intend to incorporate this functionality more explicitly into future releases of psychTestR.
If you have trouble getting the following to work, please contact Peter Harrison at -
we'd appreciate a chance to update this documentation.
#' @param timeline Character string providing the jsPsych timeline (
#' @param plugins Character vector of paths to scripts containing jsPsych plugins to be loaded (
#' @param jspsych File path of the jsPsych library file (see below)
#' @param jspsych_init File path to the jsPsych initialisation file (see below)
#' @param jquery File path to the jQuery library file (see below)
jspsych <- function(timeline, plugins, jspsych, jspsych_init, jquery) {
for (s in c(plugins, jspsych, jspsych_init, jquery))
if (!file.exists(s)) stop("couldn't find script ", s)
ui = shiny::tags$div(
shiny::tags$head(lapply(c(jspsych, jquery, plugins), shiny::includeScript)),
shiny::div(id = "js_psych")),
get_answer = function(input, ...) input$jspsych_results,
validate = function(answer, ...) nchar(answer > 0L),
save_answer = TRUE
## jsPsych library file
This core part of the jsPsych installation can be found here:
## jsPsych initialization file
This is the `jspsych_init` file of the `jspsych` function above.
This file should look something like this:
display_element: $('#js_psych'), // some versions of jsPsych
// display_element: 'js_psych', // other versions of jsPsych
fullscreen: false,
timeline: timeline,
on_finish: function() {
var json_data =;
Shiny.onInputChange("jspsych_results", json_data);
Depending on the version of jsPsych the definition of `display_element`
needs to change slightly, according to the comments above.
## jQuery
The jQuery library file can be downloaded from here:
Later versions of jsPsych may not need jQuery to be loaded.
You can’t perform that action at this time.