Convert stimulus dataframe to json object

stimulus_df_to_json(df, stimulus, data, pretty = TRUE)

Arguments

df

dataframe containins stimulus column with html for each stimulus, and additional columns for data codes

stimulus

character name of the stimulus column

data

character vector names of columns to include as data codes

pretty

logical true = pretty format, false=not pretty

Value

string a json object definition that can supply jsPsych with stimulus information

Examples

# create a stimulus dataframe library(dplyr) stroop_stim <- data.frame(stimulus = length(16), word = rep(c("red","green","blue","yellow"), each=4), color = rep(c("red","green","blue","yellow"), 4), response = rep(c("r","g","b","y"), 4), id = "stroop_stim", fontsize = "16pt") %>% mutate(stimulus = html_stimulus(df = ., html_content = "word", html_element = "p", column_names = c("color","fontsize"), css = c("color", "font-size"), id = "id")) head(stroop_stim)
#> stimulus #> 1 <p id = 'stroop_stim' style = 'color: red; font-size: 16pt;'>red</p> #> 2 <p id = 'stroop_stim' style = 'color: green; font-size: 16pt;'>red</p> #> 3 <p id = 'stroop_stim' style = 'color: blue; font-size: 16pt;'>red</p> #> 4 <p id = 'stroop_stim' style = 'color: yellow; font-size: 16pt;'>red</p> #> 5 <p id = 'stroop_stim' style = 'color: red; font-size: 16pt;'>green</p> #> 6 <p id = 'stroop_stim' style = 'color: green; font-size: 16pt;'>green</p> #> word color response id fontsize #> 1 red red r stroop_stim 16pt #> 2 red green g stroop_stim 16pt #> 3 red blue b stroop_stim 16pt #> 4 red yellow y stroop_stim 16pt #> 5 green red r stroop_stim 16pt #> 6 green green g stroop_stim 16pt
# convert to json stimulus_df_to_json(stroop_stim,"stimulus",c("word","color","response"))
#> [ #> { #> "stimulus": "<p id = 'stroop_stim' style = 'color: red; font-size: 16pt;'>red<\/p>", #> "data": { #> "word": "red", #> "color": "red", #> "response": "r" #> } #> }, #> { #> "stimulus": "<p id = 'stroop_stim' style = 'color: green; font-size: 16pt;'>red<\/p>", #> "data": { #> "word": "red", #> "color": "green", #> "response": "g" #> } #> }, #> { #> "stimulus": "<p id = 'stroop_stim' style = 'color: blue; font-size: 16pt;'>red<\/p>", #> "data": { #> "word": "red", #> "color": "blue", #> "response": "b" #> } #> }, #> { #> "stimulus": "<p id = 'stroop_stim' style = 'color: yellow; font-size: 16pt;'>red<\/p>", #> "data": { #> "word": "red", #> "color": "yellow", #> "response": "y" #> } #> }, #> { #> "stimulus": "<p id = 'stroop_stim' style = 'color: red; font-size: 16pt;'>green<\/p>", #> "data": { #> "word": "green", #> "color": "red", #> "response": "r" #> } #> }, #> { #> "stimulus": "<p id = 'stroop_stim' style = 'color: green; font-size: 16pt;'>green<\/p>", #> "data": { #> "word": "green", #> "color": "green", #> "response": "g" #> } #> }, #> { #> "stimulus": "<p id = 'stroop_stim' style = 'color: blue; font-size: 16pt;'>green<\/p>", #> "data": { #> "word": "green", #> "color": "blue", #> "response": "b" #> } #> }, #> { #> "stimulus": "<p id = 'stroop_stim' style = 'color: yellow; font-size: 16pt;'>green<\/p>", #> "data": { #> "word": "green", #> "color": "yellow", #> "response": "y" #> } #> }, #> { #> "stimulus": "<p id = 'stroop_stim' style = 'color: red; font-size: 16pt;'>blue<\/p>", #> "data": { #> "word": "blue", #> "color": "red", #> "response": "r" #> } #> }, #> { #> "stimulus": "<p id = 'stroop_stim' style = 'color: green; font-size: 16pt;'>blue<\/p>", #> "data": { #> "word": "blue", #> "color": "green", #> "response": "g" #> } #> }, #> { #> "stimulus": "<p id = 'stroop_stim' style = 'color: blue; font-size: 16pt;'>blue<\/p>", #> "data": { #> "word": "blue", #> "color": "blue", #> "response": "b" #> } #> }, #> { #> "stimulus": "<p id = 'stroop_stim' style = 'color: yellow; font-size: 16pt;'>blue<\/p>", #> "data": { #> "word": "blue", #> "color": "yellow", #> "response": "y" #> } #> }, #> { #> "stimulus": "<p id = 'stroop_stim' style = 'color: red; font-size: 16pt;'>yellow<\/p>", #> "data": { #> "word": "yellow", #> "color": "red", #> "response": "r" #> } #> }, #> { #> "stimulus": "<p id = 'stroop_stim' style = 'color: green; font-size: 16pt;'>yellow<\/p>", #> "data": { #> "word": "yellow", #> "color": "green", #> "response": "g" #> } #> }, #> { #> "stimulus": "<p id = 'stroop_stim' style = 'color: blue; font-size: 16pt;'>yellow<\/p>", #> "data": { #> "word": "yellow", #> "color": "blue", #> "response": "b" #> } #> }, #> { #> "stimulus": "<p id = 'stroop_stim' style = 'color: yellow; font-size: 16pt;'>yellow<\/p>", #> "data": { #> "word": "yellow", #> "color": "yellow", #> "response": "y" #> } #> } #> ]