vignettes.Rmd
This R package rdbhapi provides tools to access DBH-API.
You can install the released version of rdbhapi from CRAN with:
install.packages("rdbhapi")
And the development version from GitHub with:
# install.packages("devtools")
devtools::install_github("makinin/rdbhapi")
library(rdbhapi)
Package consists of the following functions
dbh_cache Cache DBH tables
dbh_clean_cache Clean DBH cache
dbh_data Get data from API as R dataframe
dbh_metadata Get metadata for DBH datasets
dbh_toc Table of contents for DBH-API
DBH-API contents are in table
library(rdbhapi)
dbh_data(1)
#> # A tibble: 109 x 6
#> Emne `Tabell id` Tabellnavn Gdpr `Bulk tabell` Variabelliste
#> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 Ikke t~ 1 API innhold false true Emne,Tabell id,Tab~
#> 2 Ikke t~ 2 API metadata false true Tabell id,Tabellna~
#> 3 Studen~ 60 Studenter fordel~ true false Institusjonskode,A~
#> 4 Studen~ 62 Utvekslingsavtal~ false false Utvekslingsavtale,~
#> 5 Studen~ 66 Desentralisering~ true false Årstall,Institusjo~
#> 6 Studen~ 88 Etterutdanning true false Institusjonskode,A~
#> 7 Studen~ 93 Finansieringskil~ false false Finansieringskilde~
#> 8 Studen~ 98 Kandidater med f~ false true Institusjonskode,Å~
#> 9 Doktor~ 100 Samarbeid om dok~ false true Årstall,Institusjo~
#> 10 Doktor~ 101 Avlagte doktorgr~ false true Institusjonskode,A~
#> # ... with 99 more rows
The package supports two methods for downloading data from DBH-API: downloading of bulk data and sending query to DBH-API. Both methods are available in package main function dbh_tabell()
. If function has only tabell_id
, than the whole table is downloaded from the bulk download data. If we use filters
, group_by
, sort_by
than query will be sent in JSON format to DBH-API.
Get the whole table in R format:
institusjoner <- dbh_data(211)
Or you can get dataset using query by defining filters
, group_by
, sort_by
, exclude
argument. Filter should be named list, where names corresponds to variable names in DBH-API and values are vectors corresponding to filter type and filter values. Filter type are the same as in DBH-API: item, all, between, lessthan, greaterthan. For aggregate datasets we need to define group_by
variables.
library(knitr)
erasmus_plus <- dbh_data(142, filters = list("Årstall" = c("top","5"),Utvekslingsavtale = "ERASMUS+",
Type = "NORSK", "Nivåkode" = "*"),exclude = c("Nivåkode" = "FU"), group_by = "Årstall")
kable(head(erasmus_plus))
Årstall | Antall totalt | Antall kvinner | Antall menn |
---|---|---|---|
2020 | 1774 | 1056 | 718 |
2019 | 2902 | 1716 | 1186 |
2018 | 2707 | 1640 | 1067 |
2017 | 2368 | 1464 | 904 |
2016 | 2206 | 1352 | 854 |
Token can be defined by placing login credentials in the environment variables dbhapi_sso_id
and dbhapi_sso_secret
in the .Renviron
file before starting R or by using Sys.setenv
.
This vignette was created with
sessionInfo()
#> R version 3.6.3 (2020-02-29)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19042)
#>
#> Matrix products: default
#>
#> locale:
#> [1] LC_COLLATE=Norwegian Bokmål_Norway.1252
#> [2] LC_CTYPE=Norwegian Bokmål_Norway.1252
#> [3] LC_MONETARY=Norwegian Bokmål_Norway.1252
#> [4] LC_NUMERIC=C
#> [5] LC_TIME=Norwegian Bokmål_Norway.1252
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] knitr_1.32 rdbhapi_2.1.0
#>
#> loaded via a namespace (and not attached):
#> [1] highr_0.9 bslib_0.2.4 compiler_3.6.3 pillar_1.6.0
#> [5] jquerylib_0.1.3 tools_3.6.3 digest_0.6.27 jsonlite_1.7.2
#> [9] evaluate_0.14 memoise_2.0.0 lifecycle_1.0.0 tibble_3.1.1
#> [13] pkgconfig_2.0.3 rlang_0.4.10 rstudioapi_0.13 cli_2.4.0
#> [17] curl_4.3 yaml_2.2.1 pkgdown_1.6.1 xfun_0.22
#> [21] fastmap_1.1.0 httr_1.4.2 stringr_1.4.0 desc_1.3.0
#> [25] fs_1.5.0 sass_0.3.1 vctrs_0.3.6 systemfonts_1.0.1
#> [29] hms_1.0.0 rprojroot_2.0.2 glue_1.4.2 R6_2.5.0
#> [33] textshaping_0.3.3 fansi_0.4.2 rmarkdown_2.7 readr_1.4.0
#> [37] magrittr_2.0.1 ps_1.6.0 htmltools_0.5.1.1 ellipsis_0.3.1
#> [41] mime_0.10 ragg_1.1.2 utf8_1.2.1 stringi_1.5.3
#> [45] cachem_1.0.4 crayon_1.4.1 rjson_0.2.20