forked from djnavarro/jaysire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjaysire08.Rmd
162 lines (128 loc) · 4.52 KB
/
jaysire08.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
---
title: "8. A choice reaction time task"
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r setup}
library(jaysire)
set.seed(143)
```
In this example we'll build a relatively simple choice reaction time experiment. On any given trial, the participant will be presented with a blue circle or an orange circle, and will respond by pressing the "F" or "J" keys. It is almost identical to the [demonstration experiment](https://www.jspsych.org/tutorials/rt-task/) used in the jsPsych documentation.
## Creating the resource files
```{r resources, message = FALSE}
library(ggplot2)
# where should the images be stored initially?
resources <- temporary_folder()
blue_image <- file.path(resources, "blue.png")
orange_image <- file.path(resources, "orange.png")
# create the blue circle plot
blue_pic <- ggplot() + theme_void() +
annotate(geom = "point", x = 0, y = 0, colour = "blue", size = 60)
# create the orange circle plot
orange_pic <- ggplot() + theme_void() +
annotate(geom = "point", x = 0, y = 0, colour = "orange", size = 60)
# save the images to files in the resource folder
ggsave(filename = blue_image, plot = blue_pic, width = 2, height = 2)
ggsave(filename = orange_image, plot = orange_pic, width = 2, height = 2)
# check that it worked
list.files(resources)
```
## Defining instructions
```{r instructions}
welcome <- trial_html_keyboard_response(
stimulus = "Welcome to the experiment! Press any key to begin",
data = insert_property(stage = "start")
)
finish <- trial_html_keyboard_response(
stimulus = "Thank you!",
choices = respond_no_key(),
trial_duration = 2000,
data = insert_property(stage = "end")
)
page1 <- paste0(
"To navigate these instructions, use the arrow keys on your keyboard. ",
"The right arrow will move you forward one page, and the left arrow will ",
"move you back one page. Press the right arrow key to continue."
)
page2 <- paste0(
"In this experiment, a circle will appear in the centre of the screen. ",
"If the circle is <b>blue</b>, press the letter F on the keyboard as fast ",
"as you can. If the circle is <b>orange</b>, press the letter J as fast as",
"you can.<br>"
)
page3 <- paste0(
"If you see this blue circle, you should press F. <br>",
"<img src = '", insert_resource("blue.png"), "' width = 300px>"
)
page4 <- paste0(
"If you see this orange circle, you should press J. <br>",
"<img src = '", insert_resource("orange.png"), "' width = 300px>"
)
page5 <- "When you are ready to begin, press the right arrow key."
instructions <- trial_instructions(
pages = c(page1, page2, page3, page4, page5),
show_clickable_nav = FALSE,
allow_keys = TRUE,
post_trial_gap = 2000,
data = insert_property(stage = "instruction")
)
```
## Defining the experiment trials
```{r}
intervals <- c(250, 500, 750, 1000, 1250, 1500, 1750, 2000)
fixation <- trial_html_keyboard_response(
stimulus = '<div style="font-size:60px;">+</div>',
choices = respond_no_key(),
trial_duration = fn_sample(intervals, 1),
data = insert_property(stage = "fixation")
)
test <- trial_image_keyboard_response(
stimulus = insert_variable("circle"),
stimulus_height = 300,
stimulus_width = 300,
choices = c("f", "j"),
data = insert_property(
stage = "choice",
colour = insert_variable("colour"),
correct_key = insert_variable("correct_key")
)
)
trials <- build_timeline(fixation, test) %>%
set_variables(
circle = insert_resource(c("orange.png", "blue.png")),
colour = c("orange", "blue"),
correct_key = keycode(c("f","j"))
) %>%
set_parameters(
repetitions = 5,
randomize_order = TRUE
)
```
## Building the experiment
```{r experiment, eval=FALSE}
build_experiment(
timeline = build_timeline(welcome, instructions, trials, finish),
resources = build_resources(resources),
columns = insert_property(experiment = "choice_rt"),
path = temporary_folder(),
on_finish = save_locally()
)
```
Working version of the experiment [here](../demos/example08/experiment/index.html).
```{r include=FALSE, message=FALSE, warning=FALSE}
finish <- trial_html_keyboard_response(
stimulus = "All done! Click <a href='../../../articles/experiment01.html'>here</a> to return to the vignette.",
choices = respond_no_key()
)
# write a working copy of the experiment into the docs folder
build_experiment(
timeline = build_timeline(welcome, instructions, trials, finish),
resources = build_resources(resources),
columns = insert_property(experiment = "choice_rt"),
path = here::here("docs", "demos", "example08")
)
```