-
Notifications
You must be signed in to change notification settings - Fork 7
Passing data from R to WebPPL
Michael Henry Tessler edited this page Jun 15, 2016
·
1 revision
Data can be passed directly from R to WebPPL as in:
my_model <- "
var model = function () {
var p = uniform(0, 1)
var scores = map( function(d) {
return Binomial({n:4, p:p}).score(d)
}, myDF)
return scores
}
model()
"
webppl(my_model,
data = df,
data_var = "myDF")
In this example, myDF
is not defined inside the WebPPL program, but is passed into it from R, using data = df
. The argument data_var
tells WebPPL what the data should be called.
The standard case we consider is passing an R data frame to WebPPL.
If myDF
looks like this in R:
Participant | Condition | Response |
---|---|---|
1 | A | 0.4 |
1 | B | 0.8 |
2 | A | 0.2 |
It will exist in WebPPL as a list of js objects e.g.
[
{ participant: 0, condition: "A", response: 0.4 },
{ participant: 0, condition: "B", response: 0.8 },
{ participant: 1, condition: "A", response: 0.2 },
...
]
Data gets passed via the jsonlite
package using toJSON
.
Other objects can be passed.
For example, if a list in R is passed, it will be appear as
> toJSON(list(a = 5, b= 3))
{"a":[5],"b":[3]}
Contrast that with passing a data.frame
> toJSON(data.frame(a = 5, b = 3))
[{"a":5,"b":3}]