Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to return all data from a table #26

Merged
merged 7 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
Type: Package
Package: dkstat
Title: API connection to the StatBank from Statistics Denmark.
Version: 0.08
Date: 2015-09-20
Version: 0.09
Date: 2024-11-15
Authors@R: c(
person("Kenneth", "Rose", , "[email protected]", role = c("aut", "cre")),
person("Thomas J.", "leeper", role = "ctb")
person("Thomas J.", "leeper", role = "ctb"),
person("Aleksander", "Bang-Larsen", "[email protected]", role = "ctb",
comment = c(ORCID = "0009-0007-7984-4650"))
)
Maintainer: Kenneth Rose <[email protected]>
Description: This package provides a few simple functions that can be used
Expand All @@ -26,6 +28,6 @@ Remotes:
sebastianbarfort/mapDK
Encoding: UTF-8
Roxygen: list(wrap = FALSE)
RoxygenNote: 5.0.1
RoxygenNote: 7.3.2
X-schema.org-isPartOf: http://ropengov.org/
X-schema.org-keywords: ropengov
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export(dst_get_data)
export(dst_get_all_data)
export(dst_meta)
export(dst_search)
export(dst_get_tables)
55 changes: 55 additions & 0 deletions R/dst_get_all_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#' This function returns the entire table requested data from the StatBank.
#' (http://www.statistikbanken.dk/statbank5a/ or http://www.dst.dk)
#'
#' @description Get all data from a table in the StatBank. This function
#' provides all the query parameters on the user's behalf. You, as a user, do
#' not need to specify anything other than a table name and you will be given
#' the entire contents of the table in a nice long format. This is useful for
#' you, if you would like to filter the table with e.g. `{dplyr}` functions or
#' save the entire table for archival.
#'
#' @export
#' @inheritParams dst_get_data
#' @family Data retrival functions
#' @author Aleksander Bang-Larsen <contact@@aleksanderbl.dk>
dst_get_all_data <- function(table, lang = "da") {

# Get metadata for table
metadata <- dst_meta(table)

# Extract variable names from metadata
variable_names <- get_vars(metadata)

# Get all options for each variable as a query-list
query <- get_var_options(metadata, variable_names)

# Request table with query params
data <- dst_get_data(
table = table,
query = query,
lang = lang
)

return(data)
}

#' This function extracts the variables from metadata
#' @noRd
get_vars <- function(metadata) {
vars <- metadata$variables[["id"]]
return(vars)
}

#' This function gets all the options for a provided list of variable names
#' given some metadata
#' @noRd
get_var_options <- function(metadata, variable_names) {
query <- list()

for (var in variable_names) {
query[var] <- var

query[var][var] <- metadata$values[[var]]["text"]
}
return(query)
}
1 change: 1 addition & 0 deletions R/dst_get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#' need to select a value for each of the parameters.
#' @param value_presentation For now, "value" or "default"
#' @export
#' @family Data retrival functions
dst_get_data <- function(table, ..., query = NULL, parse_dst_tid = TRUE, lang = "da",
meta_data = NULL, format = "CSV", value_presentation = "Value"){

Expand Down
1 change: 0 additions & 1 deletion man/dst_correct_url.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/dst_date_parse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/dst_find_val_id.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions man/dst_get_all_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions man/dst_get_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/dst_get_tables.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/dst_meta.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/dst_meta_parse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/dst_query_match.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions man/dst_search.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/dst_value_limit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading