Saving button press text in randomized trials #2801
-
Hiya, I've gotten stuck trying to save which button participants press on. It tells me which physical button (1 or 0), but not the text that was on it (which I need to know). The button choices are dependent on the stimuli and randomized. So I'm trying to create a column in my dataset called "pronoun" which is the pronoun (choice1 or choice2) they click on. (my php file is missing from here, I wasn't sure if I should include how I save as well) <!DOCTYPE html>
<html>
<head>
<script src="jspsych/jspsych.js"></script>
<script src="jspsych/plugin-html-keyboard-response.js"></script>
<script src="jspsych/plugin-html-button-response.js"></script>
<link href="jspsych/jspsych.css" rel="stylesheet" type="text/css"></link>
</head>
<body></body>
<script>
let jsPsych=initJsPsych({
on_finish: function(){
let data=jsPsych.data.get();
}
}
);
function saveData(name, data_in){
var url = 'bmaze.php';
var data_to_send = {filename: name, filedata: data_in};
fetch(url, {
method: 'POST',
body: JSON.stringify(data_to_send),
headers: new Headers({
'Content-Type': 'application/json'
})
});
}
var timeline = [];
var practice1 = [
{
sentence: "Keith beat Phillip at a chess championship. ____ cleverly captured the queen early on.",
condition: "practice",
choice1: ["He"],
choice2: ["They"],
verb: "NA"
},
{
sentence: "Alexander lied to Frank about the broken vase. ____ really didn't want to get in trouble.",
condition: "practice",
choice1: ["He"],
choice2: ["She"],
verb: "NA"
}
]
var trial = {
type:jsPsychHtmlButtonResponse,
stimulus: jsPsych.timelineVariable('sentence'),
choices: jsPsych.randomization.shuffle([jsPsych.timelineVariable('choice1'), jsPsych.timelineVariable('choice2')]),
data:{
verb: jsPsych.timelineVariable('verb'),
condition: jsPsych.timelineVariable('condition')
}
}
var trials = {
timeline: [trial],
timeline_variables: practice1,
randomize_order: true,
on_finish: jsPsych.data.addProperties({
pronoun: jsPsych.data.get('choices').last(1) */<----- I have no idea what to write here*/
})
}
timeline.push(trials);
const save_page = {
type: jsPsychHtmlKeyboardResponse,
stimulus: '<p></p>',
trial_duration: 0,
choices: 'NO_KEYS',
data: {type: 'save'},
on_finish: function(){
var experiment_data = jsPsych.data.get();
saveData(/*participant_id+*/"_data.csv", experiment_data.csv());
}
}
timeline.push(save_page);
jsPsych.run(timeline);
</script>
</html> |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Great question! This is one reason why we added the To save the button response text as its own property in the trial data, I would add an var trial = {
type:jsPsychHtmlButtonResponse,
stimulus: jsPsych.timelineVariable('sentence'),
choices: jsPsych.randomization.shuffle([jsPsych.timelineVariable('choice1'), jsPsych.timelineVariable('choice2')]),
data:{
verb: jsPsych.timelineVariable('verb'),
condition: jsPsych.timelineVariable('condition')
},
save_trial_parameters: {
// save the randomized choices array to the data
// this will reflect the response options the order they were presented, e.g. ['he', 'they'] or ['they', 'he']
choices: true
},
on_finish: function(data) {
// response is the index of the button that was clicked and
// choices is the array of options (in the order they were presented),
// so indexing the choices array with the response value gives you the text from the response
data.pronoun = data.choices[data.response];
}
} |
Beta Was this translation helpful? Give feedback.
-
very informative |
Beta Was this translation helpful? Give feedback.
Great question! This is one reason why we added the
save_trial_parameters
parameter 😃 In your case, you want to save thechoices
parameter. There's an example in the documentation on plugin parameters that's similar to what you're trying to do.To save the button response text as its own property in the trial data, I would add an
on_finish
function to your trial (not the trial procedure). In this function, you can use the numericresponse
value indicating the index of the button that was clicked (which is what you normally get from a button response plugin) and thechoices
array (which you're adding to the trial data viasave_trial_parameters
) to retrieve the contents of the button that w…