Skip to content

Commit aac1a4b

Browse files
Merge pull request #26 from aleksanderbl29/dst_get_all_data
Add function to return all data from a table
2 parents f8865d3 + e916a36 commit aac1a4b

15 files changed

+109
-18
lines changed

DESCRIPTION

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
Type: Package
22
Package: dkstat
33
Title: API connection to the StatBank from Statistics Denmark.
4-
Version: 0.08
5-
Date: 2015-09-20
4+
Version: 0.09
5+
Date: 2024-11-15
66
Authors@R: c(
77
person("Kenneth", "Rose", , "[email protected]", role = c("aut", "cre")),
8-
person("Thomas J.", "leeper", role = "ctb")
8+
person("Thomas J.", "leeper", role = "ctb"),
9+
person("Aleksander", "Bang-Larsen", "[email protected]", role = "ctb",
10+
comment = c(ORCID = "0009-0007-7984-4650"))
911
)
1012
Maintainer: Kenneth Rose <[email protected]>
1113
Description: This package provides a few simple functions that can be used
@@ -26,6 +28,6 @@ Remotes:
2628
sebastianbarfort/mapDK
2729
Encoding: UTF-8
2830
Roxygen: list(wrap = FALSE)
29-
RoxygenNote: 5.0.1
31+
RoxygenNote: 7.3.2
3032
X-schema.org-isPartOf: http://ropengov.org/
3133
X-schema.org-keywords: ropengov

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export(dst_get_data)
2+
export(dst_get_all_data)
23
export(dst_meta)
34
export(dst_search)
45
export(dst_get_tables)

R/dst_get_all_data.R

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#' This function returns the entire table requested data from the StatBank.
2+
#' (http://www.statistikbanken.dk/statbank5a/ or http://www.dst.dk)
3+
#'
4+
#' @description Get all data from a table in the StatBank. This function
5+
#' provides all the query parameters on the user's behalf. You, as a user, do
6+
#' not need to specify anything other than a table name and you will be given
7+
#' the entire contents of the table in a nice long format. This is useful for
8+
#' you, if you would like to filter the table with e.g. `{dplyr}` functions or
9+
#' save the entire table for archival.
10+
#'
11+
#' @export
12+
#' @inheritParams dst_get_data
13+
#' @family Data retrival functions
14+
#' @author Aleksander Bang-Larsen <contact@@aleksanderbl.dk>
15+
dst_get_all_data <- function(table, lang = "da") {
16+
17+
# Get metadata for table
18+
metadata <- dst_meta(table)
19+
20+
# Extract variable names from metadata
21+
variable_names <- get_vars(metadata)
22+
23+
# Get all options for each variable as a query-list
24+
query <- get_var_options(metadata, variable_names)
25+
26+
# Request table with query params
27+
data <- dst_get_data(
28+
table = table,
29+
query = query,
30+
lang = lang
31+
)
32+
33+
return(data)
34+
}
35+
36+
#' This function extracts the variables from metadata
37+
#' @noRd
38+
get_vars <- function(metadata) {
39+
vars <- metadata$variables[["id"]]
40+
return(vars)
41+
}
42+
43+
#' This function gets all the options for a provided list of variable names
44+
#' given some metadata
45+
#' @noRd
46+
get_var_options <- function(metadata, variable_names) {
47+
query <- list()
48+
49+
for (var in variable_names) {
50+
query[var] <- var
51+
52+
query[var][var] <- metadata$values[[var]]["text"]
53+
}
54+
return(query)
55+
}

R/dst_get_data.R

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#' need to select a value for each of the parameters.
1515
#' @param value_presentation For now, "value" or "default"
1616
#' @export
17+
#' @family Data retrival functions
1718
dst_get_data <- function(table, ..., query = NULL, parse_dst_tid = TRUE, lang = "da",
1819
meta_data = NULL, format = "CSV", value_presentation = "Value"){
1920

man/dst_correct_url.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dst_date_parse.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dst_find_val_id.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dst_get_all_data.Rd

+30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dst_get_data.Rd

+15-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dst_get_tables.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dst_meta.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dst_meta_parse.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dst_query_match.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dst_search.Rd

+1-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dst_value_limit.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)