<div class="section-label"><span class="num">03</span> R</div>
<div class="section-title">The R package for this <em>API</em>.</div>
Install the client, set your credentials, call Grid, and read the forecast. The package name is <span class="mono">rbpengineapi</span>.
> [!note]
> Set <span class="mono">CSA_API_KEY</span> and <span class="mono">CSA_ACCESS_ID</span> in the environment. You can pass them to <span class="mono">rbp_engine_api()</span> instead.
Source: [rbp-engine-api-client/r](https://github.com/CambridgeSportsAnalytics/rbp-engine-api-client/tree/main/r).
## Install
```r
install.packages(c("httr2", "jsonlite", "remotes"))
remotes::install_github(
"CambridgeSportsAnalytics/rbp-engine-api-client",
subdir = "r"
)
```
## Grid
```r
library(rbpengineapi)
client <- rbp_engine_api()
envelope <- predict_grid(
client,
y = c(0.1, 0.2, 0.3),
X = matrix(c(1, 0, 0, 1, 1, 1), nrow = 3, byrow = TRUE)
)
yhat <- envelope$results$yhat
```
`predict_grid` submits the job and waits. The forecast is `envelope$results$yhat`. Fit and Grid insights sit on the same `results` object. What those numbers mean: [[Functions/Grid Prediction|Grid Prediction]] · [[Results/Grid Insights|Grid Insights]].
Example: [post_grid.R](https://github.com/CambridgeSportsAnalytics/rbp-engine-api-client/blob/main/r/examples/post_grid.R).
## MaxFit and PSR
```r
envelope <- predict_maxfit(client, y = y, X = X)
envelope <- predict_psr(client, y = y, X = X)
```
`predict_psr` is named so it does not mask `stats::predict`. Use MaxFit when the attribute set is fixed. Use PSR when the thresholds are fixed too. Examples: [post_maxfit.R](https://github.com/CambridgeSportsAnalytics/rbp-engine-api-client/blob/main/r/examples/post_maxfit.R) · [post_predict.R](https://github.com/CambridgeSportsAnalytics/rbp-engine-api-client/blob/main/r/examples/post_predict.R).
## Options
Omit `options` to use defaults. Pass a named list only for keys you want to change. Names match [[Settings/Options|Settings]]. For logistic outcomes, set `prediction_scale = "logistic"`.
## Several circumstances
Pass several rows of `theta`. The client waits for every row.
```r
envelope <- predict_grid(client, y = y, X = X, theta = rbind(c(0, 1), c(1, 0)))
```
Example: [post_predict_experiment.R](https://github.com/CambridgeSportsAnalytics/rbp-engine-api-client/blob/main/r/examples/post_predict_experiment.R).
## Submit without waiting
`post_grid`, `post_maxfit`, and `post_predict` return a receipt immediately. Call `wait_results(client, receipt)` when you want the forecast.
## Large X, one field, quota
Keep compact JSON `X` under **5 MiB**; this package does not upload yet. Upload from [[API/Clients/Python|Python]] and pass `list(s3_key = "...")`, or shrink the matrix. [[API/Limits/Payload Limits|Payload size]]
`get_results_field` and `get_quota` match the Python client. [[API/Limits/Throttle Limits|Quota]]
## If something goes wrong
Failures have class `rbp_engine_api_error` with `status_code` and `body`. 401 is a bad key, 413 is `X` too large, 429 is quota, 409 is a job that failed while running.
Calling HTTP yourself: [post_predict_http.R](https://github.com/CambridgeSportsAnalytics/rbp-engine-api-client/blob/main/r/examples/post_predict_http.R) · [[API/Endpoints/Grid|Endpoints]].
## Related
- [[API/Clients/Introduction|Clients]] · [[API/Clients/Python|Python]] · [[API/Clients/MATLAB|MATLAB]]
- [[API/Introduction|API]] · [[Functions/Grid Prediction|Grid Prediction]]
<div class="btn-row center">
<a class="btn-primary" href="/API/Clients/MATLAB">MATLAB</a>
<a class="btn-ghost" href="/API/Clients/Python">Python</a>
</div>