Description Usage Arguments Details Value See Also Examples
Run a function, either once or for each subject
1 |
run |
"first", "last", or "all". |
fn |
a function which should take two arguments, |
name |
optional name of the stage. |
If
run
is "all"
then the function fn
will be run every time
a subject reaches the stage. If run
is "first"
then fn
will be run when the first subject reaches the stage. If run
is
"last"
then fn
will be run when the last subject reaches
the stage.
A Stage object of class Program
Other stages: NEXT
, WAIT
,
stage
; checkpoint
;
form_stage
; period
;
text_stage
; timed
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 | expt <- experiment(N=16, on_ready=function() {
mpcr <<- 1.5
mydf <<- experiment_data_frame(expt)
mydf$contrib <<- NA
mydf$profit <<- NA
mydf$group <<- rep(rep(1:4, each=4), nperiods(expt))
})
s1 <- function(id, period, params) {
if (! is.null(params) && 'contrib' %in% names(params)) {
mydf$contrib[mydf$id==id & mydf$period==period] <<- params$contrib
return(NEXT)
} else {
return("<html><body><form action=''>
Enter a contribution:<input name='contrib' type='text'>
<input type='submit' value='Next'></form></body></html>")
}
}
s2 <- program("last", function(id, period) {
mydf$profit <<- with(mydf[mydf$period==period,]
ave(contrib, group, FUN=function(x) 50 - x + mpcr * mean(x)))
})
add_stage(expt, period(), s1, checkpoint(mydf$group), s2, s3, times=10)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.