rdbhapi provides an R interface for accessing the open data available through DBH-API. It allows users to query, filter, and retrieve data directly into R for further analysis.
install.packages(“remotes”)
remotes::install_github(“mni-hkdir/rdbhapi”)
To retrieve full (non-rounded) data from the DBH-API, you need to authenticate using your SSO ID and SSO Secret credentials.
There are two options:
-
Using
.RenvironfileThe
.Renvironfile is a hidden file used by R to store environment variables.
You can create or edit this file in your home directory or in your R project folder.Open the
.Renvironfile in a text editor:file.edit("~/.Renviron")Add the following lines to
.Renviron:dbhapi_sso_id = "your_sso_id" dbhapi_sso_secret = "your_sso_secret"After saving, restart your R session to load the updated
.Renvironfile.You can verify the environment variables are loaded using:
Sys.getenv("dbhapi_sso_id") Sys.getenv("dbhapi_sso_secret")
-
Setting credentials dynamically
Alternatively, you can set the token during your R session using:
Sys.setenv(dbhapi_sso_id = "your_sso_id") Sys.setenv(dbhapi_sso_secret = "your_sso_secret")
# Load the package
library(rdbhapi)dbh_data(1)institutions <- dbh_data(211)
head(institutions)students_abroad <- dbh_data(142)
head(students_abroad)top5_institutions <- dbh_data(
211,
filters = list(
"Institusjonskode" = c("top", "5")
)
)
head(top5_institutions)