|
| 1 | +# Downloading data with BrAPI R package |
| 2 | + |
| 3 | +## Objective: To be able to demonstrate how to get all TERRA REF data available via BrAPI |
| 4 | + |
| 5 | +- Define BrAPI |
| 6 | +- Describe `brapi` R package for using API |
| 7 | + |
| 8 | +```{r, eval=FALSE} |
| 9 | +library(brapi) |
| 10 | +``` |
| 11 | + |
| 12 | +# Exploring available data |
| 13 | + |
| 14 | +The function `ba_calls` from `brapi` is supposed to show which functionalities are available for our data base. |
| 15 | + |
| 16 | +```{r, eval=FALSE} |
| 17 | +terraref <- ba_db()$terraref |
| 18 | +ba_calls(terraref) |
| 19 | +``` |
| 20 | +This returns no data and some warnings, as below: |
| 21 | + |
| 22 | +```{shell} |
| 23 | +Unknown or uninitialised column: 'datatypes'.Unknown or uninitialised column: 'methods'.Unknown or uninitialised column: 'versions'.No encoding supplied: defaulting to UTF-8. |
| 24 | +``` |
| 25 | + |
| 26 | +These errors come from two sources: |
| 27 | + |
| 28 | +1. The GET call using authentication when our server doesn't require any |
| 29 | +2. For showing the type of data, our server uses the current API version of the key (`dataTypes`) while the R package uses the older version (`datatypes`) |
| 30 | + |
| 31 | +This can be fixed in the R package code base by doing the following: |
| 32 | + |
| 33 | +**Remove authorization argument** |
| 34 | + |
| 35 | +This is in `brapiGET` function in the `httr::GET` line. |
| 36 | + |
| 37 | +*Current* |
| 38 | +```{r, eval=FALSE} |
| 39 | + res <- httr::GET(url = url, httr::timeout(25), |
| 40 | + httr::add_headers("Authorization" = |
| 41 | + paste("Bearer", con$token))) |
| 42 | +``` |
| 43 | + |
| 44 | +*New* |
| 45 | +```{r, eval=FALSE} |
| 46 | + res <- httr::GET(url = url, httr::timeout(25)) |
| 47 | +``` |
| 48 | + |
| 49 | +**Fix case** |
| 50 | + |
| 51 | +This is in `ba_calls` function in the `out$datatypes` line. |
| 52 | + |
| 53 | +*Current* |
| 54 | +```{r, eval=FALSE} |
| 55 | + out$datatypes <- vapply(X = out$datatypes, FUN = paste, FUN.VALUE = "", |
| 56 | + collapse = "; ") |
| 57 | +``` |
| 58 | + |
| 59 | +*New* |
| 60 | +```{r, eval=FALSE} |
| 61 | + out$datatypes <- vapply(X = out$dataTypes, FUN = paste, FUN.VALUE = "", |
| 62 | + collapse = "; ") |
| 63 | +``` |
| 64 | + |
| 65 | +Note: the latter does break for the example database used for BrAPI, sweetpotatobase, because the server still uses `datatypes`. |
| 66 | + |
| 67 | +Once these two changes are made, they can implemented locally by rebuilding the `brapi` R package by running `devtools::install(".")` from inside the `brapi` folder. |
0 commit comments