program: Run a function, either once or for each subject

Description Usage Arguments Details Value See Also Examples

Description

Run a function, either once or for each subject

Usage

1
program(run, fn, name = "No name")

Arguments

run

"first", "last", or "all".

fn

a function which should take two arguments, id and period.

name

optional name of the stage.

Details

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.

Value

A Stage object of class Program

See Also

Other stages: NEXT, WAIT, stage; checkpoint; form_stage; period; text_stage; timed

Examples

 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)

hughjonesd/betr documentation built on May 17, 2019, 9:11 p.m.