From 7b8eaa7d8e299b085b162c031337c3d5aba9d066 Mon Sep 17 00:00:00 2001 From: alexkowa Date: Thu, 18 Jul 2024 09:28:55 +0200 Subject: [PATCH 1/6] v 1.0 -> CRAN --- DESCRIPTION | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 58e9ba1b..cd0a075f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,9 +1,9 @@ Type: Package Package: STATcubeR Title: R interface for the STATcube REST API and Open Government Data -Version: 0.5.2 +Version: 1.0.0 Authors@R: c( - person("Gregor", "de Cillia", , "Gregor.deCillia@statistik.gv.at", role = "aut"), + person("Gregor", "de Cillia", , "", role = "aut"), person("Bernhard", "Meindl", , "Bernhard.Meindl@statistik.gv.at", role = "ctb"), person("Alexander", "Kowarik", , "Alexander.Kowarik@statistik.gv.at", role = c("ctb","cre"),comment=c(ORCID="0000-0001-8598-4130")) @@ -35,4 +35,4 @@ Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.3 -Language: en-US \ No newline at end of file +Language: en-US From 9839a9bcda9e943172a1d087d0ad48ac456015d2 Mon Sep 17 00:00:00 2001 From: alexkowa Date: Thu, 18 Jul 2024 11:13:28 +0200 Subject: [PATCH 2/6] reducing dependencies --- DESCRIPTION | 6 ++-- NAMESPACE | 10 +++--- R/cache.R | 10 +++--- R/error.R | 5 ++- R/od_cache.R | 22 ++++++++------ R/od_list.R | 31 ++++++++++--------- R/od_resource.R | 30 +++++++++--------- R/od_revisions.R | 10 +++--- R/od_table.R | 3 +- R/od_table_save.R | 8 ++--- R/od_tabulate.R | 4 +-- R/od_utils.R | 17 +++++------ R/other_endpoints.R | 26 ++++++++-------- R/print.R | 8 ++--- R/sc_data.R | 2 +- R/schema.R | 8 ++--- R/schema_db.R | 6 ++-- R/schema_uri.R | 12 +++----- R/sdmx_table.R | 64 +++++++++++++++++++-------------------- R/table.R | 9 +++--- R/table_as_data_frame.R | 16 ++++------ R/table_field.R | 5 ++- R/table_json.R | 12 ++++---- R/table_meta.R | 22 +++++++------- R/table_saved.R | 9 +++--- R/tabulate.R | 6 ++-- R/utils.R | 14 +-------- man/od_catalogue.Rd | 6 ++-- man/other_endpoints.Rd | 3 +- man/pipe.Rd | 12 -------- man/sc_json_get_server.Rd | 2 +- man/sc_last_error.Rd | 2 +- man/sc_schema.Rd | 6 ++-- man/sc_tabulate.Rd | 6 ++-- man/sdmx_table.Rd | 4 +-- 35 files changed, 194 insertions(+), 222 deletions(-) delete mode 100644 man/pipe.Rd diff --git a/DESCRIPTION b/DESCRIPTION index cd0a075f..c44050d8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,23 +16,23 @@ License: GPL (>= 2) URL: https://statistikat.github.io/STATcubeR, https://github.com/statistikat/STATcubeR BugReports: https://github.com/statistikat/STATcubeR/issues +Depends: R (>= 3.5.0) Imports: cli (>= 3.4.1), httr, jsonlite, - magrittr, pillar (>= 1.5.0), vctrs (>= 0.5.2) Suggests: + magrittr, spelling, data.tree, rappdirs, xml2, reactable, - tidyverse, markdown Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.2 Language: en-US diff --git a/NAMESPACE b/NAMESPACE index 8336ba67..d81af6d7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,10 +4,16 @@ S3method(as.character,od_json) S3method(as.character,sc_json) S3method(as.character,sc_schema_uri) S3method(as.data.frame,sc_data) +S3method(format,od_json) +S3method(format,od_table) S3method(format,pillar_shaft_ogd_file) +S3method(format,pillar_shaft_ogd_id) +S3method(format,sc_rate_limit_table) S3method(format,sc_schema_uri) +S3method(format,sc_table) S3method(format,sdmx_table) S3method(pillar_shaft,ogd_file) +S3method(pillar_shaft,ogd_id) S3method(pillar_shaft,sc_dttm) S3method(pillar_shaft,sc_schema_type) S3method(pillar_shaft,sc_schema_uri) @@ -25,7 +31,6 @@ S3method(print,sdmx_description) S3method(tbl_format_footer,sc_meta) S3method(tbl_sum,sc_meta) S3method(tbl_sum,sc_tibble) -export("%>%") export(od_cache_clear) export(od_cache_dir) export(od_cache_file) @@ -77,9 +82,6 @@ export(sc_table_saved) export(sc_table_saved_list) export(sc_tabulate) export(sdmx_table) -importFrom(magrittr,"%<>%") -importFrom(magrittr,"%>%") -importFrom(magrittr,"%T>%") importFrom(pillar,pillar_shaft) importFrom(pillar,tbl_format_footer) importFrom(pillar,tbl_sum) diff --git a/R/cache.R b/R/cache.R index 0f9860e9..7216cf5a 100644 --- a/R/cache.R +++ b/R/cache.R @@ -74,8 +74,7 @@ sc_cache_dir_get <- function() { } sc_checksum <- function(x) { - httr::sha1_hash(NULL, x) %>% - gsub("/", "-", .) + gsub("/", "-", httr::sha1_hash(NULL, x)) } #' @describeIn sc_cache get the cache file associated with an object @@ -83,15 +82,14 @@ sc_checksum <- function(x) { #' @export sc_cache_files <- function(x) { if (inherits(x, "sc_table")) - return(x$response %>% attr("sc_cache_file")) + return(attr(x$response, "sc_cache_file")) if (inherits(x, "sc_schema")) - return(x %>% attr("response") %>% attr("sc_cache_file")) + return( attr(attr(x, "response"), "sc_cache_file")) stop("sc_cache_file() can only be used with sc_table and sc_schema objects") } sc_cache_file <- function(params, ext = ".rds") { - sc_checksum(params) %>% - paste0(sc_cache_dir(), "/", ., ext) + paste0(sc_cache_dir(), "/", sc_checksum(params), ext) } #' @describeIn sc_cache removes all files from the cache diff --git a/R/error.R b/R/error.R index 47595ec7..df1b3c34 100644 --- a/R/error.R +++ b/R/error.R @@ -15,7 +15,7 @@ #' try(sc_table_saved("invalid_id")) #' last_error <- sc_last_error() #' httr::content(last_error) -#' sc_last_error_parsed() %>% str() +#' str(sc_last_error_parsed()) #' @export sc_last_error <- function() { sc_env$last_error @@ -61,8 +61,7 @@ sc_check_response <- function(response) { sc_env$last_error <- response message <- paste0(httr::http_status(response)$message, "\n") if (httr::http_type(response) == "application/json") - message <- httr::content(response, as = "text") %>% - jsonlite::prettify(indent = 2) %>% paste0(message, .) + message <- paste0(message, jsonlite::prettify(httr::content(response, as = "text"),indent = 2)) message <- paste0(message, message_sc_last_error()) stop(message, call. = FALSE) } diff --git a/R/od_cache.R b/R/od_cache.R index 8f7dbf1f..aa4f5978 100644 --- a/R/od_cache.R +++ b/R/od_cache.R @@ -40,7 +40,9 @@ od_cache_summary <- function(server = "ext") { is_field <- pos_underscore != -1 field <- substr(files[is_field], 1 + pos_underscore[is_field], nchar(files[is_field]) - 4) id <- substr(files[is_field], 1, pos_underscore[is_field] - 1) - sizes_fields <- file.size(file.path(od_cache_dir(), files[is_field])) %>% split(id) %>% sapply(sum) + sizes_fields <- sapply( + split(file.size(file.path(od_cache_dir(), files[is_field])),id), + sum) fields <- list(id = id, field = field) files <- files[!is_field] @@ -51,14 +53,13 @@ od_cache_summary <- function(server = "ext") { id_data <- substr(files, 1, nchar(files) - 4) all_ids <- unique(c(id_data, id_header, fields$id)) res <- data_frame( - id = all_ids %>% `class<-`(c("ogd_id", "character")), + id = `class<-`(all_ids,c("ogd_id", "character")), updated = file.mtime(paste0(cache_dir, all_ids, ".json")), json = file.size(paste0(cache_dir, all_ids, ".json")), data = file.size(paste0(cache_dir, all_ids, ".csv")), header = file.size(paste0(cache_dir, all_ids, "_HEADER.csv")), fields = sizes_fields[match(unique(fields$id), all_ids)], - n_fields = match(fields$id, all_ids) %>% factor(seq_along(all_ids)) %>% - table() %>% as.integer() + n_fields = as.integer(table(factor(match(fields$id, all_ids),seq_along(all_ids)))) ) class(res$updated) <- c("sc_dttm", class(res$updated)) res @@ -66,12 +67,15 @@ od_cache_summary <- function(server = "ext") { #' @rdname od_cache -#' @importFrom magrittr %T>% #' @export od_downloads <- function(server = "ext") { - x <- od_cache_path(server, "downloads.log") %T>% - (function(x) {if (!file.exists(x)) stop("No file 'downloads.log' in cache")}) %>% - utils::read.csv(header = FALSE) %>% `names<-`(c("time", "file", "downloaded")) + x <- od_cache_path(server, "downloads.log") + if (!file.exists(x)) + stop("No file 'downloads.log' in cache") + x <- utils::read.csv(x,header = FALSE) + names(x) <- c("time", "file", "downloaded") x$time <- as.POSIXct(x$time) - x %>% .[rev(seq_len(nrow(.))), ] %>% `class<-`(c("tbl", "data.frame")) + x[rev(seq_len(nrow(x))), ] + class(x) <- c("tbl", "data.frame") + return(x) } diff --git a/R/od_list.R b/R/od_list.R index 937ed554..c7876fb3 100644 --- a/R/od_list.R +++ b/R/od_list.R @@ -33,20 +33,20 @@ od_list <- function(unique = TRUE, server = c("ext", "red")) { html <- httr::content(r, encoding = "UTF-8") # main-groups - grp <- html %>% - xml2::xml_find_all('//*[@class="panel-heading"]') %>% - xml2::xml_find_all(".//a") %>% + grp <- html |> + xml2::xml_find_all('//*[@class="panel-heading"]') |> + xml2::xml_find_all(".//a") |> xml2::xml_text() - el <- html %>% - xml2::xml_find_all(".//h4") %>% + el <- html |> + xml2::xml_find_all(".//h4") |> xml2::xml_find_all(".//a") # ids df <- data_frame( category = rep("NA", length(el)), - id = el %>% xml2::xml_attr("aria-label"), - label = el %>% xml2::xml_text() + id = el |> xml2::xml_attr("aria-label"), + label = el |> xml2::xml_text() ) ignored_labels <- c("[Alle \u00f6ffnen]", "[Alle schlie\u00dfen]", @@ -106,12 +106,12 @@ od_list <- function(unique = TRUE, server = c("ext", "red")) { #' @examples #' catalogue <- od_catalogue() #' catalogue -#' catalogue$update_frequency %>% table() -#' catalogue$categorization %>% table() +#' table(catalogue$update_frequency) +#' table(catalogue$categorization) #' catalogue[catalogue$categorization == "Gesundheit", 1:4] #' catalogue[catalogue$measures >= 70, 1:3] #' catalogue$json[[1]] -#' catalogue$database %>% head() +#' head(catalogue$database) #' @export od_catalogue <- function(server = "ext", local = TRUE) { if (local) { @@ -138,16 +138,17 @@ as_df_jsons <- function(jsons) { as.POSIXct(x, format = "%Y-%m-%dT%H:%M:%OS") } - descs <- sapply(jsons, function(x) x$extras$attribute_description) %>% paste0(";", .) + descs <- paste0(";", sapply(jsons, function(x) x$extras$attribute_description)) + tmpfn <- function(x) {x[!grepl("statcube", x)] <- NA_character_; x} out <- data_frame( title = sapply(jsons, function(x) x$title), - measures = gregexpr(";F-", descs) %>% sapply(length), - fields = gregexpr(";C-", descs) %>% sapply(length), + measures = sapply(gregexpr(";F-", descs),length), + fields = sapply(gregexpr(";C-", descs),length), modified = sapply(jsons, function(x) x$extras$metadata_modified), created = sapply(jsons, function(x) x$resources[[1]]$created), id = sapply(jsons, function(x) x$resources[[1]]$name), - database = sapply(jsons, function(x) x$extras$metadata_linkage[[1]]) %>% - (function(x) {x[!grepl("statcube", x)] <- NA_character_; x}) %>% strsplit("?id=") %>% + database = sapply(jsons, function(x) x$extras$metadata_linkage[[1]]) |> + tmpfn() |> strsplit("?id=") |> sapply(function(x) x[2]), title_en = sapply(jsons, function(x) x$extras$en_title_and_desc), notes = sapply(jsons, function(x) x$notes), diff --git a/R/od_resource.R b/R/od_resource.R index 70da1ff8..4cc3bec5 100644 --- a/R/od_resource.R +++ b/R/od_resource.R @@ -7,7 +7,6 @@ #' contain a hidden attribute `attr(., "od")` about the time used for #' downloading and parsing the resource. [od_resource_all()] converts these #' hidden attribute into columns. -#' @importFrom magrittr %<>% NULL od_resource_blacklist <- c( @@ -55,7 +54,7 @@ od_cache_path <- function(server = "ext", ...) { #' @export od_cache_clear <- function(id, server = "ext") { od_resource_check_id(id) - files <- od_cache_path(server) %>% dir(id, full.names = TRUE) + files <- dir(od_cache_path(server),id, full.names = TRUE) file.remove(files) message("deleted ", length(files), " files from ", shQuote(od_cache_path(server))) } @@ -94,7 +93,7 @@ od_cache_file <- function(id, suffix = NULL, timestamp = NULL, ..., server = "ex ext <- match.arg(list(...)$ext, c("csv", "json")) stopifnot(is.character(id) && length(id) > 0) od_resource_check_id(id) - filename <- c(id, suffix) %>% paste(collapse = "_") %>% paste0(".", ext) + filename <- paste0(paste(c(id, suffix), collapse = "_"), ".", ext) cache_file <- od_cache_path(server, filename) download <- NA_real_ if (!file.exists(cache_file) || !is.null(timestamp) && @@ -127,7 +126,7 @@ od_resource <- function(id, suffix = NULL, timestamp = NULL, server = "ext") { cache_file <- od_cache_file(id, suffix, timestamp, ext = "csv", server = server) t <- Sys.time() x <- utils::read.csv2(cache_file, na.strings = c("", ":"), - check.names = FALSE, stringsAsFactors = FALSE) %>% + check.names = FALSE, stringsAsFactors = FALSE) |> od_normalize_columns(suffix) t <- Sys.time() - t t <- 1000 * as.numeric(t) @@ -136,7 +135,7 @@ od_resource <- function(id, suffix = NULL, timestamp = NULL, server = "ext") { } od_resource_parse_all <- function(resources, server = "ext") { - parsed <- resources %>% lapply(function(x) { + parsed <- lapply(resources, function(x) { last_modified <- as.POSIXct(x$last_modified, format = "%Y-%m-%dT%H:%M:%OS") od_resource(x$name, timestamp = last_modified, server = server) }) @@ -144,12 +143,12 @@ od_resource_parse_all <- function(resources, server = "ext") { data_frame( name = sapply(resources, function(x) x$name), - last_modified = lapply(od, function(x) x$last_modified) %>% do.call(c, .), - cached = lapply(od, function(x) x$cached) %>% do.call(c, .), + last_modified = do.call(c, lapply(od, function(x) x$last_modified)), + cached = do.call(c, lapply(od, function(x) x$cached)), size = sapply(od, function(x) x$size), download = vapply(od, function(x) x$download, 1.0), parsed = sapply(od, function(x) x$parsed), - data = I(parsed %>% lapply(`attr<-`, "od", NULL)) + data = I(lapply(parsed, `attr<-`, "od", NULL)) ) } @@ -159,7 +158,7 @@ od_resources_check <- function(json) { id <- resources[[1]]$name stopifnot(resources[[2]]$name == paste0(id, "_HEADER")) stopifnot(all(sapply(resources, function(x) { x$format == "csv" }))) - fc_res <- resources %>% .[-c(1, 2)] %>% sapply(function(x) x$name) + fc_res <- sapply(resources[-c(1, 2)], function(x) x$name) fc_att <- att$code[substr(att$code, 1, 2) == "C-"] stopifnot(setequal(fc_res, paste0(id, "_", fc_att))) function(header) { @@ -174,7 +173,8 @@ od_normalize_columns <- function(x, suffix) { col_indices <- c(1, 2, 2, switch(suffix, HEADER = 3, c(4, 3)), 5, 7) col_names <- c("code", "label", "label_de", "label_en", switch(suffix, HEADER = NULL, "parent"), "de_desc", "en_desc") - x <- x[, col_indices] %>% `names<-`(col_names) + x <- x[, col_indices] + names(x) <- col_names x$label <- NA_character_ x$label_en <- as.character(x$label_en) x$label_de <- as.character(x$label_de) @@ -207,8 +207,7 @@ od_json <- function(id, timestamp = Sys.time() - 3600, server = "ext") { #' @export as.character.od_json <- function(x, ...) { - jsonlite::toJSON(x, pretty = TRUE, auto_unbox = TRUE) %>% - paste(...) + paste(jsonlite::toJSON(x, pretty = TRUE, auto_unbox = TRUE), ...) } #' @name od_resource @@ -222,8 +221,11 @@ od_resource_all <- function(id, json = od_json(id), server = "ext") { check_header <- od_resources_check(json) out <- od_resource_parse_all(json$resources, server = server) check_header(out$data[[2]]) - out$data[[2]] %<>% od_normalize_columns("HEADER") - out$data[seq(3, nrow(out))] %<>% lapply(od_normalize_columns, "FIELD") + + out$data[[2]] <- od_normalize_columns(out$data[[2]], "HEADER") + + out$data[seq(3, nrow(out))] <- lapply(out$data[seq(3, nrow(out))], + od_normalize_columns, "FIELD") class(out$name) <- c("ogd_file", "character") class(out$last_modified) <- c("sc_dttm", class(out$last_modified)) class(out$cached) <- c("sc_dttm", class(out$cached)) diff --git a/R/od_revisions.R b/R/od_revisions.R index 02d954e0..5328470d 100644 --- a/R/od_revisions.R +++ b/R/od_revisions.R @@ -41,15 +41,15 @@ print.od_revisions <- function(x, ...) { since <- attr(x, "since") response <- attr(x, "response") if (!is.null(since)) - cli::format_inline("{.strong {length(x)}} changes between + message(cli::format_inline("{.strong {length(x)}} changes between {.timestamp {attr(x, 'since')}} and - {.timestamp {response$date}}") %>% cat() + {.timestamp {response$date}}")) else - cli::format_inline("{.strong {length(x)}} datasets are available ", - "({.timestamp {response$date}})\n") %>% cat() + message(cli::format_inline("{.strong {length(x)}} datasets are available ", + "({.timestamp {response$date}})\n")) if (length(x) > 0) { y <- cli::cli_vec(x, list("vec-trunc" = 3)) - cli::format_inline("{.strong ids}: {.emph {y}}") %>% cat() + message(cli::format_inline("{.strong ids}: {.emph {y}}")) } invisible(x) } diff --git a/R/od_table.R b/R/od_table.R index 20c81252..390623e7 100644 --- a/R/od_table.R +++ b/R/od_table.R @@ -102,7 +102,7 @@ od_table_class <- R6::R6Class( #' #' Similar contents can be found in `$meta`. header = function() { - private$cache$header %>% sc_tibble_meta(c("label_de", "label_en")) + sc_tibble_meta(private$cache$header, c("label_de", "label_en")) }, #' @field resources #' lists all files downloaded from the server to construct this table @@ -132,6 +132,7 @@ print.od_table <- function(x, ...) { cat(format(x), sep = "\n") } +#' @export format.od_table <- function(x, ...) { c( cli::style_bold(strwrap(x$meta$source$label)), diff --git a/R/od_table_save.R b/R/od_table_save.R index 22dc9d52..3735b791 100644 --- a/R/od_table_save.R +++ b/R/od_table_save.R @@ -88,13 +88,11 @@ od_table_local_paths <- function() { id <- json$resources[[1]]$name stopifnot(is.character(id), length(id) == 1) if (json$extras$metadata_modified == "$PublDateTime$") { - readLines(json_file) %>% - gsub("\\$PublDateTime\\$", json$extras$begin_datetime, .) %>% - writeLines(json_file) + writeLines(gsub("\\$PublDateTime\\$", json$extras$begin_datetime, readLines(json_file)), json_file) json <- jsonlite::read_json(json_file) } - timestamps <- sapply(json$resources, function(x) x$last_modified) %>% - as.POSIXct(format = "%Y-%m-%dT%H:%M:%OS") + timestamps <- as.POSIXct(sapply(json$resources, function(x) x$last_modified), + format = "%Y-%m-%dT%H:%M:%OS") stopifnot(all(timestamps <= Sys.time())) paths <- list( classifications = dir(file.path(extracted, "classifications"), full.names = TRUE), diff --git a/R/od_tabulate.R b/R/od_tabulate.R index 820d69ee..897ca185 100644 --- a/R/od_tabulate.R +++ b/R/od_tabulate.R @@ -43,10 +43,10 @@ sc_data_tabulate <- function(table, ..., .list = NULL, raw = FALSE, parse_time = x <- x[, setdiff(names(x), setdiff(fields_to_aggregate, has_total))] if (length(aggregate_via_sum) > 0) { grouping_var <- do.call(paste, x[fields]) - x <- cbind( + x <- sc_tibble(cbind( subset(x, !duplicated(grouping_var), fields), rowsum(x[measures], group = grouping_var, reorder = FALSE) - ) %>% sc_tibble() + )) } # post process aggregated data for (field_code in fields) { diff --git a/R/od_utils.R b/R/od_utils.R index 7e432f5b..5338f350 100644 --- a/R/od_utils.R +++ b/R/od_utils.R @@ -16,11 +16,11 @@ od_get_total_code <- function(code, parent) { } od_attr <- function(json) { - desc <- json$extras$attribute_description %>% paste0(";", .) - index_c <- gregexpr(";C-", desc) %>% .[[1]] - index_f <- gregexpr(";F-", desc) %>% .[[1]] + desc <- paste0(";", json$extras$attribute_description) + index_c <- gregexpr(";C-", desc)[[1]] + index_f <- gregexpr(";F-", desc)[[1]] index_code <- sort(c(index_c, index_f)) - index_colon <- gregexpr(":", desc) %>% .[[1]] + index_colon <- gregexpr(":", desc)[[1]] index_end <- c(index_code[-1], 1000000L) - 1 code <- character(0) label <- character(0) @@ -75,7 +75,7 @@ od_create_data <- function(id, json = od_json(id), lang = NULL, resources$name <- paste0(resources$name, ".csv") od <- attr(json, "od") resources <- rbind(data_frame( - name = paste0(id, ".json"), last_modified = json$extras$metadata_modified %>% + name = paste0(id, ".json"), last_modified = json$extras$metadata_modified |> as.POSIXct(format = "%Y-%m-%dT%H:%M:%OS"), cached = od$cached, size = od$size, download = od$download, parsed = NA), resources[1:6] ) @@ -109,12 +109,12 @@ od_label_data <- function(table, x = table$data, parse_time = TRUE, language = N x } +#' @export format.od_json <- function(x, ...) { att <- od_attr(x) measures <- att$label[substr(att$code, 1, 1) == "F"] fields <- att$label[substr(att$code, 1, 1) == "C"] - last_modified <- x$extras$metadata_modified %>% - as.POSIXct(format = "%Y-%m-%dT%H:%M:%OS") %>% format() + last_modified <- format(as.POSIXct(x$extras$metadata_modified, format = "%Y-%m-%dT%H:%M:%OS")) notes <- "" if (x$title != x$notes) { notes <- c("", cli::style_italic(strwrap(x$notes)), "") @@ -144,8 +144,7 @@ cli_dl2 <- function(items, labels = names(items)) { if (length(item) > 10) item <- c(item[1:10], paste(cli::symbol$continue, cli::style_italic("(", length(item) - 10, " more)"))) - new <- paste0(labels[i], ": ", paste(unlist(item), collapse = ", ")) %>% - cli::ansi_strwrap(exdent = 2) + new <- cli::ansi_strwrap(paste0(labels[i], ": ", paste(unlist(item), collapse = ", ")), exdent = 2) out <- c(out, new) } out diff --git a/R/other_endpoints.R b/R/other_endpoints.R index 6cd40fe6..6188609a 100644 --- a/R/other_endpoints.R +++ b/R/other_endpoints.R @@ -7,8 +7,7 @@ #' sc_info() #' sc_rate_limit_table() #' sc_rate_limit_schema() -#' sc_schema("str:group:deake005:X_B1") %>% -#' sc_rate_limits() +#' sc_rate_limits(sc_schema("str:group:deake005:X_B1")) #' @name other_endpoints #' @inheritParams sc_key #' @inheritParams sc_schema @@ -21,12 +20,12 @@ sc_info <- function(language = c("en", "de"), key = NULL, server = "ext") { response <- httr::GET( url = paste0(base_url(server), "/info"), config = sc_headers(language, key, server) - ) %>% sc_check_response() + ) + response <- sc_check_response(response) info_content <- httr::content(response) - info_content$languages %>% - lapply(function(x) - data_frame(locale = x$locale, displayName = x$displayName)) %>% - do.call(rbind, .) + return(do.call(rbind,lapply(info_content$languages, + function(x) data_frame(locale = x$locale, + displayName = x$displayName)))) } #' @describeIn other_endpoints @@ -38,10 +37,10 @@ sc_info <- function(language = c("en", "de"), key = NULL, server = "ext") { #' Usually, this should be less than one hour `after the current time. #' @export sc_rate_limit_table <- function(language = c("en", "de"), key = NULL, server = "ext") { - response <- httr::GET( + response <- sc_check_response(httr::GET( url = paste0(base_url(server), "/rate_limit_table"), config = sc_headers(language, key, server) - ) %>% sc_check_response() + )) rate_limit <- httr::content(response) class(rate_limit) <- "sc_rate_limit_table" rate_limit @@ -50,10 +49,10 @@ sc_rate_limit_table <- function(language = c("en", "de"), key = NULL, server = " #' @rdname other_endpoints #' @export sc_rate_limit_schema <- function(language = c("en", "de"), key = NULL, server = "ext") { - response <- httr::GET( + response <- sc_check_response(httr::GET( url = paste0(base_url(server), "/rate_limit_schema"), config = sc_headers(language, key, server) - ) %>% sc_check_response() + )) rate_limit <- httr::content(response) class(rate_limit) <- "sc_rate_limit_table" rate_limit @@ -66,12 +65,12 @@ extract_rate_limits <- function(response) { limit = header[["x-ratelimit-schema"]], remaining = header[["x-ratelimit-remaining-schema"]], reset = header[["x-ratelimit-reset-schema"]] - ) %>% `class<-`("sc_rate_limit_table"), + ) |> `class<-`("sc_rate_limit_table"), table = list( limit = header[["x-ratelimit-table"]], remaining = header[["x-ratelimit-remaining-table"]], reset = header[["x-ratelimit-reset-table"]] - ) %>% `class<-`("sc_rate_limit_table") + ) |> `class<-`("sc_rate_limit_table") ) } @@ -96,6 +95,7 @@ print.sc_rate_limit_table <- function(x, ...) { cat(format(x), sep = "\n") } +#' @export format.sc_rate_limit_table <- function(x, ...) { cli::format_inline( "{.field {x$remaining}} / {.field {x$limit}} (Resets at {.timestamp ", diff --git a/R/print.R b/R/print.R index fc3c1b78..37d7c33f 100644 --- a/R/print.R +++ b/R/print.R @@ -96,18 +96,18 @@ format.pillar_shaft_ogd_file <- function(x, width, ...) { files <- cli::style_hyperlink( files, paste0("https://data.statistik.gv.at/data/", x$x)) } else { - files <- cli::style_hyperlink(files, paste0("file://", path.expand( - od_cache_dir()), x$x)) %>% as.character() + files <- as.character(cli::style_hyperlink(files, paste0("file://", path.expand( + od_cache_dir()), x$x))) } pillar::new_ornament(files, align = "left") } - +#' @export pillar_shaft.ogd_id <- function(x, ...) { pillar::new_pillar_shaft(list(x = x), width = pillar::get_max_extent(x), min_width = 20, class = "pillar_shaft_ogd_id", type_sum = "chr") } - +#' @export format.pillar_shaft_ogd_id <- function(x, width, ...) { id <- x$x too_long <- nchar(id) > width diff --git a/R/sc_data.R b/R/sc_data.R index 0845e78c..1a7dbe28 100644 --- a/R/sc_data.R +++ b/R/sc_data.R @@ -81,7 +81,7 @@ sc_data <- R6::R6Class( total_codes = function(...) { args <- list(...) if (length(args) == 0) - return(private$p_meta$fields[, c("code", "total_code")] %>% + return(private$p_meta$fields[, c("code", "total_code")] |> `class<-`(c("tbl", "data.frame"))) keys <- od_match_codes(private$p_meta$fields, names(args), single = FALSE) values <- unlist(args) diff --git a/R/schema.R b/R/schema.R index aac8801c..552dc3ad 100644 --- a/R/schema.R +++ b/R/schema.R @@ -38,7 +38,7 @@ sc_schema <- function(id = NULL, depth = NULL, ifelse(is.null(depth), "", paste0("?depth=", depth)) ), config = sc_headers(language, key, server) - ) %>% sc_check_response() + ) |> sc_check_response() }) content <- httr::content(response) x <- sc_as_nested_list(content) @@ -48,7 +48,7 @@ sc_schema <- function(id = NULL, depth = NULL, print_schema_with_tree <- function(x, ...) { stopifnot(requireNamespace("data.tree", quietly = TRUE)) - x <- unclass(x) %>% data.tree::as.Node(nodeName = x$label, check = "no-check") + x <- data.tree::as.Node(unclass(x), nodeName = x$label, check = "no-check") print(x, ..., "type") invisible(x) } @@ -152,8 +152,8 @@ sc_schema_flatten_impl <- function(resp, type) { label <- character() if (!is.null(resp$children)) { ret <- lapply(resp$children, sc_schema_flatten_impl, type) - id <- lapply(ret, function(x) x$id) %>% unlist() - label <- lapply(ret, function(x) x$label) %>% unlist() + id <- unlist(lapply(ret, function(x) x$id)) + label <- unlist(lapply(ret, function(x) x$label)) } if (resp$type == type) { id <- c(resp$id, id) diff --git a/R/schema_db.R b/R/schema_db.R index 223b5988..7f9ee6d4 100644 --- a/R/schema_db.R +++ b/R/schema_db.R @@ -25,9 +25,9 @@ #' my_content$label #' #' # print with data.tree -#' "str:group:deake005:X_B1" %>% -#' sc_schema(depth = "valueset") %>% -#' print(tree = TRUE) +#' +#' treeX_B1 <- sc_schema("str:group:deake005:X_B1", depth = "valueset") +#' print(treeX_B1, tree = TRUE) #' @describeIn sc_schema is similar to the #' [table view](`r sc_browse_database('deake005', open = TRUE)`) #' of the STATcube GUI and gives information about all measures and diff --git a/R/schema_uri.R b/R/schema_uri.R index ccd8741e..34e9a391 100644 --- a/R/schema_uri.R +++ b/R/schema_uri.R @@ -20,16 +20,14 @@ sc_schema_url <- function(uri) { url <- rep(NA_character_, length(uri)) is_database <- grep("^str:database", uri) if (length(is_database) > 0) - url[is_database] <- uri[is_database] %>% - sub("^str:database:", "", .) %>% - sc_browse_database(server = "ext") %>% + url[is_database] <- sub("^str:database:", "", uri[is_database]) |> + sc_browse_database(server = "ext") |> as.character() is_table <- grepl("^str:table", uri) & !grepl("^([0-9a-f-])+$", sub("str:table:", "", uri)) if (length(is_table) > 0) - url[is_table] <- uri[is_table] %>% - sub("^str:table:", "", .) %>% - sc_browse_table(server = "ext") %>% + url[is_table] <- sub("^str:table:", "", uri[is_table]) |> + sc_browse_table(server = "ext") |> as.character() url } @@ -43,7 +41,7 @@ pillar_shaft.sc_schema_uri <- function(x, ...) { uri <- vctrs::field(x, "uri") if (cli::ansi_hyperlink_types()$run) { run <- sc_schema_run(uri) - template <- cli::format_inline("{.run [%s](%s)}") %>% cli::style_underline() + template <- cli::format_inline("{.run [%s](%s)}") |> cli::style_underline() formatted <- sprintf(template, run, formatted) short_formatted <- sprintf(template, run, short_formatted) } else if (cli::ansi_has_hyperlink_support()) { diff --git a/R/sdmx_table.R b/R/sdmx_table.R index bd476a99..0f72377b 100644 --- a/R/sdmx_table.R +++ b/R/sdmx_table.R @@ -8,7 +8,7 @@ #' @return An object of class `sc_data` #' @keywords experimental #' @examples -#' x <- "sdmx/dedemo.zip" %>% system.file(package = "STATcubeR") %>% sdmx_table() +#' x <- sdmx_table(system.file("sdmx/dedemo.zip", package = "STATcubeR")) #' # print and tabulate #' x #' x$tabulate() @@ -17,7 +17,7 @@ #' data.frame(label = nuts2$label, #' parent = nuts2$label[match(nuts2$parent, nuts2$code)]) #' # extract more data from the raw xml -#' x$xml$meta %>% xml2::xml_find_first(".//Name") +#' xml2::xml_find_first(x$xml$meta, ".//Name") #' @export sdmx_table <- function(file) { sdmx_table_class$new(file) @@ -25,10 +25,8 @@ sdmx_table <- function(file) { sdmx_read <- function(folder = ".") { list( - meta = folder %>% sprintf('%s/structure.xml', .) %>% xml2::read_xml() %>% - xml2::xml_ns_strip(), - data = folder %>% sprintf('%s/dataset.xml', .) %>% xml2::read_xml() %>% - xml2::xml_ns_strip() + meta = xml2::xml_ns_strip(xml2::read_xml(sprintf('%s/structure.xml', folder))), + data = xml2::xml_ns_strip(xml2::read_xml(sprintf('%s/dataset.xml', folder))) ) } @@ -36,18 +34,18 @@ sdmx_read_zip <- function(zip_file) { exdir <- tempfile() dir.create(exdir) on.exit(unlink(exdir, recursive = TRUE)) - unzip(zipfile = zip_file, exdir = exdir) + utils::unzip(zipfile = zip_file, exdir = exdir) sdmx_read(exdir) } sdmx_as_raw_df <- function(x) { - obs <- x$data %>% xml2::xml_find_all(".//ObsValue") %>% - xml2::xml_attr("value") %>% as.numeric() - val <- x$data %>% xml2::xml_find_all(".//SeriesKey//Value") %>% + obs <- x$data |> xml2::xml_find_all(".//ObsValue") |> + xml2::xml_attr("value") |> as.numeric() + val <- x$data |> xml2::xml_find_all(".//SeriesKey//Value") |> xml2::xml_attr("value") # assume that entries of SeriesKey always use the same order - val_lab <- x$data %>% xml2::xml_find_first(".//SeriesKey") %>% - xml2::xml_find_all(".//Value") %>% xml2::xml_attr("concept") + val_lab <- x$data |> xml2::xml_find_first(".//SeriesKey") |> + xml2::xml_find_all(".//Value") |> xml2::xml_attr("concept") val_split <- split(val, rep( seq_len(length(val)/length(obs)), length(obs) )) @@ -61,23 +59,23 @@ sdmx_as_raw_df <- function(x) { factor(u, unique(u), sdmx_codes(unique(u))) }), obs_split - ) %>% vctrs::new_data_frame() - names(res) <- gsub("F-DATA_", "", names(res)) %>% sdmx_unescape_codes() + ) |> vctrs::new_data_frame() + names(res) <- gsub("F-DATA_", "", names(res)) |> sdmx_unescape_codes() res } sdmx_fields <- function(x) { - fields <- x$meta %>% + fields <- x$meta |> xml2::xml_find_all(".//CodeList[(@id and starts-with(@id, 'C-'))]") lapply(fields, function(field) { - names <- xml2::xml_find_all(field, ".//Name") %>% + names <- xml2::xml_find_all(field, ".//Name") |> xml2::xml_text() codes <- xml2::xml_find_all(field, "Code") values <- xml2::xml_attr(codes, "value") - desc <- codes %>% xml2::xml_find_all("Description") %>% + desc <- codes |> xml2::xml_find_all("Description") |> xml2::xml_text() - ann_list <- codes %>% - xml2::xml_find_all(".//common:AnnotationText", flatten = FALSE) %>% + ann_list <- codes |> + xml2::xml_find_all(".//common:AnnotationText", flatten = FALSE) |> lapply(xml2::xml_text) has_ann <- vapply(ann_list, length, 0) > 0 ann_de <- ann_en <- rep(NA_character_, length(ann_list)) @@ -87,7 +85,7 @@ sdmx_fields <- function(x) { ind_en <- seq(2, length(desc), 2) parent <- xml2::xml_attr(codes, "parentCode") list( - id = field %>% xml2::xml_attr("id"), + id = field |> xml2::xml_attr("id"), label_en = names[2], label_de = names[1], elements = vctrs::new_data_frame(list( @@ -105,19 +103,19 @@ sdmx_fields <- function(x) { } sdmx_meta <- function(x) { - code_measure <- x$meta %>% xml2::xml_find_all( - ".//*[(@id = 'CL_MEASURES_DIMENSION')]/Code") %>% xml2::xml_attr("value") - label_measure <- x$meta %>% xml2::xml_find_all( - ".//*[(@id = 'CL_MEASURES_DIMENSION')]/Code/Description") %>% + code_measure <- x$meta |> xml2::xml_find_all( + ".//*[(@id = 'CL_MEASURES_DIMENSION')]/Code") |> xml2::xml_attr("value") + label_measure <- x$meta |> xml2::xml_find_all( + ".//*[(@id = 'CL_MEASURES_DIMENSION')]/Code/Description") |> xml2::xml_text() - code_db <- xml2::xml_find_all(x$meta, ".//ConceptScheme") %>% - xml2::xml_attr("id") %>% sdmx_unescape_codes() - label_dataset <- x$meta %>% xml2::xml_find_all(".//ConceptScheme/Name") %>% + code_db <- xml2::xml_find_all(x$meta, ".//ConceptScheme") |> + xml2::xml_attr("id") |> sdmx_unescape_codes() + label_dataset <- x$meta |> xml2::xml_find_all(".//ConceptScheme/Name") |> xml2::xml_text() ind_de <- seq(1, length(label_measure), 2) ind_en <- seq(2, length(label_measure), 2) - prepared <- x$meta %>% xml2::xml_find_all(".//message:Prepared") %>% - xml2::xml_text() %>% as.POSIXct(format = "%FT%T") + prepared <- x$meta |> xml2::xml_find_all(".//message:Prepared") |> + xml2::xml_text() |> as.POSIXct(format = "%FT%T") list( source = data_frame(label = label_dataset[2], code = code_db, lang = "en", label_de = label_dataset[1], @@ -168,8 +166,8 @@ sdmx_esc <- function(codes, char) { } sdmx_unescape_codes <- function(codes) { - codes %>% sdmx_esc("\u5f") %>% sdmx_esc("\u7c") %>% sdmx_esc("\u2b") %>% - sdmx_esc("\u2e") %>% sdmx_esc("\u23") %>% sdmx_esc("\u40") + codes |> sdmx_esc("\u5f") |> sdmx_esc("\u7c") |> sdmx_esc("\u2b") |> + sdmx_esc("\u2e") |> sdmx_esc("\u23") |> sdmx_esc("\u40") } sdmx_table_class <- R6::R6Class( @@ -201,9 +199,9 @@ sdmx_table_class <- R6::R6Class( list( xml = function() { private$p_xml }, description = function() { - self$xml$meta %>% xml2::xml_find_first( + self$xml$meta |> xml2::xml_find_first( sprintf(".//ConceptScheme/Description[(@xml:lang='%s')]", self$language) - ) %>% xml2::xml_text() %>% `class<-`("sdmx_description") + ) |> xml2::xml_text() |> `class<-`("sdmx_description") } ) ) diff --git a/R/table.R b/R/table.R index f1e23f8f..57f9d22a 100644 --- a/R/table.R +++ b/R/table.R @@ -46,7 +46,7 @@ sc_table_class <- R6::R6Class( if (is.null(json) && is.null(file)) json <- jsonlite::toJSON( - content$query, auto_unbox = TRUE, pretty = TRUE) %>% toString() + content$query, auto_unbox = TRUE, pretty = TRUE) |> toString() private$json_content <- sc_json_class$new(json, file, add_totals) meta <- sc_meta(content) @@ -99,8 +99,8 @@ sc_table_class <- R6::R6Class( }, #' @description open the dataset in a browser browse = function() { - sc_json_get_server(self$json$content) %>% sc_url_gui() %>% - paste0("openinfopage?id=", self$meta$source$code) %>% sc_url() + sc_json_get_server(self$json$content) |> sc_url_gui() |> + paste0("openinfopage?id=", self$meta$source$code) |> sc_url() }, #' @description add a second language to the dataset #' @param language a language to add. `"en"` or `"de"`. @@ -216,7 +216,7 @@ sc_table <- function(json, language = NULL, add_totals = TRUE, key = NULL, both <- language == "both" if (both) language <- "de" - res <- sc_table_json_post(json$string, language, add_totals, key) %>% + res <- sc_table_json_post(json$string, language, add_totals, key) |> sc_table_class$new(json$string, json$file, add_totals) if (both) res$add_language("en", key) @@ -256,6 +256,7 @@ normalize_json <- function(json, json_file) { list(file = file, string = json) } +#' @export format.sc_table <- function(x, ...) { c( cli::style_bold(strwrap(x$meta$source$label)), diff --git a/R/table_as_data_frame.R b/R/table_as_data_frame.R index 31a9414d..7329fc90 100644 --- a/R/table_as_data_frame.R +++ b/R/table_as_data_frame.R @@ -10,14 +10,11 @@ unlist_n <- function(x, times) { get_annotations <- function(content, i = 1) { cube <- content$cubes[[i]]$annotations if (is.null(cube)) { - n_values <- content$cubes[[i]]$values %>% unlist() %>% length() + n_values <- length(unlist(content$cubes[[i]]$values)) return(rep(list(NULL), n_values)) } - dims <- content$fields %>% - lapply(function(x) x$items) %>% - sapply(length) - unlist_n(cube, length(dims) - 1) %>% - sapply(unlist) + dims <- sapply(lapply(content$fields,function(x) x$items), length) + return(sapply(unlist_n(cube, length(dims) - 1), unlist)) } sc_model_matrix <- function(dims) { @@ -26,16 +23,15 @@ sc_model_matrix <- function(dims) { out <- list() for (i in seq_along(dims)) { each <- each / dims[i] - out[[paste0("FIELD_", i)]] <- seq_len(dims[i]) %>% - rep(times = times, each = each) + out[[paste0("FIELD_", i)]] <- rep(seq_len(dims[i]), + times = times, each = each) times <- times * dims[i] } vctrs::new_data_frame(out) } sc_table_create_data <- function(content) { - dims_fields <- content$fields %>% - lapply(function(x) x$items) %>% + dims_fields <- lapply(content$fields, function(x) x$items) |> sapply(length) df <- sc_model_matrix(dims_fields) # labeling of fields diff --git a/R/table_field.R b/R/table_field.R index f0a72faa..0d6a1270 100644 --- a/R/table_field.R +++ b/R/table_field.R @@ -6,8 +6,7 @@ sc_field_parse <- function(field, type = sc_field_type(field)) { } sc_field_codes <- function(field, split_minus = TRUE) { - res <- field$items %>% - sapply(function(x) x$uris[[1]] %>% get_var_code(split_minus)) + res <- sapply(field$items, function(x) get_var_code(x$uris[[1]], split_minus)) res[res == ""] <- NA res } @@ -42,7 +41,7 @@ sc_field_type <- function(field) { } sc_field_parse_category <- function(field) { - field$items %>% sapply(function(x) x$labels[[1]]) + sapply(field$items, function(x) x$labels[[1]]) } sc_as_time <- function(year, month, ind) { diff --git a/R/table_json.R b/R/table_json.R index 52a2309c..ed730cba 100644 --- a/R/table_json.R +++ b/R/table_json.R @@ -39,7 +39,7 @@ sc_json_class <- R6::R6Class( #' @export as.character.sc_json <- function(x, ..., collapse = "\n") { - x$content %>% paste(..., collapse = collapse) + paste(x$content,..., collapse = collapse) } sc_json_add_totals <- function(json_content) { @@ -57,7 +57,7 @@ sc_json_add_totals <- function(json_content) { #' json request #' @param json path to a request json #' @examples -#' sc_example('accomodation') %>% sc_json_get_server() +#' sc_json_get_server(sc_example('accomodation')) #' @export sc_json_get_server <- function(json) { parsed <- jsonlite::fromJSON(json, simplifyVector = FALSE) @@ -80,15 +80,15 @@ sc_table_json_post <- function(json, language = NULL, language <- sc_language(language) server <- sc_json_get_server(json) if (add_totals) - json <- json %>% - jsonlite::fromJSON(simplifyVector = FALSE) %>% - sc_json_add_totals() %>% + json <- json |> + jsonlite::fromJSON(simplifyVector = FALSE) |> + sc_json_add_totals() |> jsonlite::toJSON(pretty = TRUE, auto_unbox = TRUE) sc_with_cache(c("sc_table_json_post", json, language, add_totals), function() { httr::POST( url = paste0(base_url(server), "/table"), body = json, config = sc_headers(language, key, server) - ) %>% sc_check_response() + ) |> sc_check_response() }) } diff --git a/R/table_meta.R b/R/table_meta.R index d9f17224..a6d0c205 100644 --- a/R/table_meta.R +++ b/R/table_meta.R @@ -14,12 +14,12 @@ get_item_code <- function(item, split_minus = FALSE) { return("SC_TOTAL") stopifnot(item$type == "RecodeItem") uris <- item$uris - codes <- as.character(uris) %>% - strsplit(":") %>% + codes <- as.character(uris) |> + strsplit(":") |> lapply(utils::tail, 1) if (split_minus) - codes <- as.character(codes) %>% - strsplit("-") %>% + codes <- as.character(codes) |> + strsplit("-") |> lapply(utils::tail(1)) paste(codes, collapse = ";") } @@ -30,11 +30,11 @@ summarize_annotations <- function(content, i) { return("") freq <- table(unlist(annotations)) sapply(seq_along(freq), function(i) { paste0(names(freq)[i], "(", as.numeric(freq)[i], - ")")}) %>% paste(collapse = ", ") + ")")}) |> paste(collapse = ", ") } sc_meta <- function(content) { - measure_info <- lapply(seq_along(content$measures), function(i) { + measure_info <- do.call(rbind,lapply(seq_along(content$measures), function(i) { measure <- content$measures[[i]] data_frame( label = measure$label, @@ -44,9 +44,9 @@ sc_meta <- function(content) { annotations = summarize_annotations(content, i), NAs = sum(unlist(content$cubes[[i]]$values) == 0) ) - }) %>% do.call(rbind, .) + })) - field_info <- lapply(content$fields, function(field) { + field_info <- do.call(rbind,lapply(content$fields, function(field) { has_total <- field$items[[length(field$items)]]$type == "Total" data_frame( label = field$label, @@ -55,7 +55,7 @@ sc_meta <- function(content) { type = sc_field_type(field), total_code = ifelse(has_total, "SC_TOTAL", NA_character_) ) - }) %>% do.call(rbind, .) + })) db_info <- data_frame( label = content$database$label, code = content$database$id @@ -64,12 +64,12 @@ sc_meta <- function(content) { } sc_meta_field <- function(field) { - res <- lapply(field$items, function(item) { + res <- do.call(rbind,lapply(field$items, function(item) { data_frame( label = paste(item$labels, collapse = ";"), code = get_item_code(item) ) - }) %>% do.call(rbind, .) + })) res$parsed <- sc_field_parse(field) res } diff --git a/R/table_saved.R b/R/table_saved.R index 9475c81f..a3840133 100644 --- a/R/table_saved.R +++ b/R/table_saved.R @@ -4,10 +4,11 @@ sc_table_saved_list <- function(key = NULL, server = "ext") { if (is.null(key)) key <- sc_key(server) schema <- sc_schema(key = key, server = server) - schema <- schema %>% attr("response") %>% httr::content() + schema <- attr(schema,"response") + schema <- httr::content(schema) schema <- schema$children - tables <- schema %>% sapply(function(x) x$type == "TABLE") + tables <- sapply(schema, function(x) x$type == "TABLE") saved_tables <- schema[tables] vctrs::new_data_frame(list( label = sapply(saved_tables, function(x) x$label), @@ -31,6 +32,6 @@ sc_table_saved <- function(table_uri, language = NULL, key = NULL, server = "ext httr::GET( url = paste0(base_url(server), "/table/saved/", table_uri), config = sc_headers(language, key, server) - ) %>% sc_check_response() - }) %>% sc_table_class$new() + ) |> sc_check_response() + }) |> sc_table_class$new() } diff --git a/R/tabulate.R b/R/tabulate.R index 5a2858d8..5b0a4841 100644 --- a/R/tabulate.R +++ b/R/tabulate.R @@ -94,7 +94,7 @@ #' ######################### STATcube REST API ################################# #' #' @examplesIf sc_key_exists() -#' table_tourism <- sc_example("accomodation.json") %>% sc_table("de") +#' table_tourism <- sc_table(sc_example("accomodation.json"), "de") #' #' table_tourism$tabulate() #' table_tourism$tabulate("Saison/Tourismusmonat") @@ -103,11 +103,11 @@ #' #' ## TODO: param annotations does not work currently #' if (FALSE) { -#' table_trade <- sc_example("foreign_trade.json") %>% sc_table("de") +#' table_trade <- sc_table(sc_example("foreign_trade.json"), "de") #' tt <- sc_tabulate(table_trade, "Berichtsjahr", "Import, Wert in Euro", #' annotations = TRUE) #' tt -#' tt[['Import, Wert in Euro_a']] %>% str() +#' str(tt[['Import, Wert in Euro_a']]) #' } #' @export sc_tabulate <- function(table, ..., .list = NULL, raw = FALSE, diff --git a/R/utils.R b/R/utils.R index d0d2a380..b6651b53 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,17 +1,5 @@ -#' Pipe operator -#' -#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. -#' -#' @name %>% -#' @rdname pipe -#' @keywords internal -#' @export -#' @importFrom magrittr %>% -#' @usage lhs \%>\% rhs -NULL - sc_parse_time <- function(timestamp) { - (as.numeric(timestamp) / 1000) %>% as.POSIXct(origin = "1970-01-01") + as.POSIXct((as.numeric(timestamp) / 1000), origin = "1970-01-01") } sc_user_agent <- function(){ diff --git a/man/od_catalogue.Rd b/man/od_catalogue.Rd index 17d1dd93..cba794a0 100644 --- a/man/od_catalogue.Rd +++ b/man/od_catalogue.Rd @@ -45,10 +45,10 @@ The last column \code{"json"} contains the full json metadata as returned by \examples{ catalogue <- od_catalogue() catalogue -catalogue$update_frequency \%>\% table() -catalogue$categorization \%>\% table() +table(catalogue$update_frequency) +table(catalogue$categorization) catalogue[catalogue$categorization == "Gesundheit", 1:4] catalogue[catalogue$measures >= 70, 1:3] catalogue$json[[1]] -catalogue$database \%>\% head() +head(catalogue$database) } diff --git a/man/other_endpoints.Rd b/man/other_endpoints.Rd index e7e5bbc6..13e5b914 100644 --- a/man/other_endpoints.Rd +++ b/man/other_endpoints.Rd @@ -55,7 +55,6 @@ Usually, this should be less than one hour `after the current time. sc_info() sc_rate_limit_table() sc_rate_limit_schema() -sc_schema("str:group:deake005:X_B1") \%>\% - sc_rate_limits() +sc_rate_limits(sc_schema("str:group:deake005:X_B1")) \dontshow{\}) # examplesIf} } diff --git a/man/pipe.Rd b/man/pipe.Rd deleted file mode 100644 index 0106fbdb..00000000 --- a/man/pipe.Rd +++ /dev/null @@ -1,12 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils.R -\name{\%>\%} -\alias{\%>\%} -\title{Pipe operator} -\usage{ -lhs \%>\% rhs -} -\description{ -See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. -} -\keyword{internal} diff --git a/man/sc_json_get_server.Rd b/man/sc_json_get_server.Rd index 103c56a2..b30cee41 100644 --- a/man/sc_json_get_server.Rd +++ b/man/sc_json_get_server.Rd @@ -18,5 +18,5 @@ parses a json request and returns a short string representing the corresponding STATcube server } \examples{ -sc_example('accomodation') \%>\% sc_json_get_server() +sc_json_get_server(sc_example('accomodation')) } diff --git a/man/sc_last_error.Rd b/man/sc_last_error.Rd index de058bf0..1ef6e8b6 100644 --- a/man/sc_last_error.Rd +++ b/man/sc_last_error.Rd @@ -36,6 +36,6 @@ the response content and the response status try(sc_table_saved("invalid_id")) last_error <- sc_last_error() httr::content(last_error) -sc_last_error_parsed() \%>\% str() +str(sc_last_error_parsed()) \dontshow{\}) # examplesIf} } diff --git a/man/sc_schema.Rd b/man/sc_schema.Rd index b19b1126..e5172f02 100644 --- a/man/sc_schema.Rd +++ b/man/sc_schema.Rd @@ -115,9 +115,9 @@ my_content <- httr::content(my_response) my_content$label # print with data.tree -"str:group:deake005:X_B1" \%>\% - sc_schema(depth = "valueset") \%>\% - print(tree = TRUE) + + treeX_B1 <- sc_schema("str:group:deake005:X_B1", depth = "valueset") + print(treeX_B1, tree = TRUE) \dontshow{\}) # examplesIf} } \concept{functions for /schema} diff --git a/man/sc_tabulate.Rd b/man/sc_tabulate.Rd index 2b2ec328..ef7636ac 100644 --- a/man/sc_tabulate.Rd +++ b/man/sc_tabulate.Rd @@ -121,7 +121,7 @@ sc_tabulate(table, "C-A11-0") ######################### STATcube REST API ################################# \dontshow{if (sc_key_exists()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -table_tourism <- sc_example("accomodation.json") \%>\% sc_table("de") +table_tourism <- sc_table(sc_example("accomodation.json"), "de") table_tourism$tabulate() table_tourism$tabulate("Saison/Tourismusmonat") @@ -130,11 +130,11 @@ table_tourism$tabulate("Ankünfte") ## TODO: param annotations does not work currently if (FALSE) { - table_trade <- sc_example("foreign_trade.json") \%>\% sc_table("de") + table_trade <- sc_table(sc_example("foreign_trade.json"), "de") tt <- sc_tabulate(table_trade, "Berichtsjahr", "Import, Wert in Euro", annotations = TRUE) tt - tt[['Import, Wert in Euro_a']] \%>\% str() + str(tt[['Import, Wert in Euro_a']]) } \dontshow{\}) # examplesIf} } diff --git a/man/sdmx_table.Rd b/man/sdmx_table.Rd index 4cadeb94..0bf81b25 100644 --- a/man/sdmx_table.Rd +++ b/man/sdmx_table.Rd @@ -18,7 +18,7 @@ consisting of \code{structure.xml} with metadata and \code{dataset.xml} for the values. } \examples{ -x <- "sdmx/dedemo.zip" \%>\% system.file(package = "STATcubeR") \%>\% sdmx_table() +x <- sdmx_table(system.file("sdmx/dedemo.zip", package = "STATcubeR")) # print and tabulate x x$tabulate() @@ -27,6 +27,6 @@ nuts2 <- x$field("C-B00-0") data.frame(label = nuts2$label, parent = nuts2$label[match(nuts2$parent, nuts2$code)]) # extract more data from the raw xml -x$xml$meta \%>\% xml2::xml_find_first(".//Name") +xml2::xml_find_first(x$xml$meta, ".//Name") } \keyword{experimental} From 8c9e73fcd2dd82e847305193ee6e2726333a6d88 Mon Sep 17 00:00:00 2001 From: alexkowa Date: Fri, 19 Jul 2024 11:52:54 +0200 Subject: [PATCH 3/6] fix DESCRIPTION --- DESCRIPTION | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c44050d8..766a7f6a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Type: Package Package: STATcubeR -Title: R interface for the STATcube REST API and Open Government Data +Title: R Interface for the STATcube REST API and Open Government Data Version: 1.0.0 Authors@R: c( person("Gregor", "de Cillia", , "", role = "aut"), @@ -13,7 +13,7 @@ Description: Import data from the STATcube REST API or from the open data requests as well as parsing utilities for data which originates from STATcube. License: GPL (>= 2) -URL: https://statistikat.github.io/STATcubeR, +URL: https://statistikat.github.io/STATcubeR/, https://github.com/statistikat/STATcubeR BugReports: https://github.com/statistikat/STATcubeR/issues Depends: R (>= 3.5.0) From f20f039708e4d292f1afa31447152e733efaec88 Mon Sep 17 00:00:00 2001 From: alexkowa Date: Fri, 19 Jul 2024 11:53:05 +0200 Subject: [PATCH 4/6] remove defunct link --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index b57e4d00..ef976412 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,6 @@ bytes](https://img.shields.io/github/languages/code-size/statistikat/STATcubeR?logo=github)](https://github.com/statistikat/STATcubeR) [![GitHub last commit](https://img.shields.io/github/last-commit/statistikat/STATcubeR.svg?logo=github)](https://github.com/statistikat/STATcubeR/commits/master) -[![Lifecycle: -experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental) R client for all things [STATcube](https://statcube.at). From e5a583b21606d5e09c5adfbf17d3d3778d84eb59 Mon Sep 17 00:00:00 2001 From: alexkowa Date: Fri, 19 Jul 2024 11:53:19 +0200 Subject: [PATCH 5/6] rm wine.json in main folder --- wine.json | 57 ------------------------------------------------------- 1 file changed, 57 deletions(-) delete mode 100644 wine.json diff --git a/wine.json b/wine.json deleted file mode 100644 index 73a98f1d..00000000 --- a/wine.json +++ /dev/null @@ -1,57 +0,0 @@ -{ - "database": "str:database:deerwobs17", - "measures": [ - "str:statfn:deerwobs17:F-BETRIEB:F-BETRANZ:SUM", - "str:statfn:deerwobs17:F-OBST:F-FLAECHE:SUM" - ], - "recodes": { - "str:field:deerwobs17:F-OBST:C-PFLOBSOR0-0": { - "map": [ - [ - "str:value:deerwobs17:F-OBST:C-PFLOBSOR0-0:C-PFLOBSOR1-0:PFLOBSOR1-1" - ] - ], - "total": false - }, - "str:field:deerwobs17:F-BETRIEB:C-PFLC41-0": { - "map": [ - [ - "str:value:deerwobs17:F-BETRIEB:C-PFLC41-0:C-B00-0:B00-1" - ], - [ - "str:value:deerwobs17:F-BETRIEB:C-PFLC41-0:C-B00-0:B00-2" - ], - [ - "str:value:deerwobs17:F-BETRIEB:C-PFLC41-0:C-B00-0:B00-3" - ], - [ - "str:value:deerwobs17:F-BETRIEB:C-PFLC41-0:C-B00-0:B00-4" - ], - [ - "str:value:deerwobs17:F-BETRIEB:C-PFLC41-0:C-B00-0:B00-5" - ], - [ - "str:value:deerwobs17:F-BETRIEB:C-PFLC41-0:C-B00-0:B00-6" - ], - [ - "str:value:deerwobs17:F-BETRIEB:C-PFLC41-0:C-B00-0:B00-7" - ], - [ - "str:value:deerwobs17:F-BETRIEB:C-PFLC41-0:C-B00-0:B00-8" - ], - [ - "str:value:deerwobs17:F-BETRIEB:C-PFLC41-0:C-B00-0:B00-9" - ] - ], - "total": false - } - }, - "dimensions": [ - [ - "str:field:deerwobs17:F-OBST:C-PFLOBSOR0-0" - ], - [ - "str:field:deerwobs17:F-BETRIEB:C-PFLC41-0" - ] - ] -} From a688416f77c1722ab9030c1647a6ba68400b9730 Mon Sep 17 00:00:00 2001 From: alexkowa Date: Fri, 19 Jul 2024 11:53:34 +0200 Subject: [PATCH 6/6] remove blocked links in DESCRIPTION --- R/schema.R | 4 ++-- man/sc_schema.Rd | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/schema.R b/R/schema.R index 552dc3ad..4b2f0de5 100644 --- a/R/schema.R +++ b/R/schema.R @@ -7,8 +7,8 @@ #' The main function `sc_schema()` can be used with any resource id. #' [sc_schema_catalogue()] and [sc_schema_db()] are very simple #' wrapper functions around [`sc_schema()`] and are comparable to the -#' [catalogue explorer](`r sc_browse_catalogue()`) or the -#' [table view](`r sc_browse_database('deake005', open = TRUE)`) of the STATcube GUI. +#' catalogue explorer or the +#' table view of the STATcube GUI. #' #' The responses of the API are tree-like data structures which #' are wrapped into a class called `sc_schema` to simplify the usage in R. diff --git a/man/sc_schema.Rd b/man/sc_schema.Rd index e5172f02..1aeff0e6 100644 --- a/man/sc_schema.Rd +++ b/man/sc_schema.Rd @@ -56,8 +56,8 @@ as well as metadata about specific databases. The main function \code{sc_schema()} can be used with any resource id. \code{\link[=sc_schema_catalogue]{sc_schema_catalogue()}} and \code{\link[=sc_schema_db]{sc_schema_db()}} are very simple wrapper functions around \code{\link[=sc_schema]{sc_schema()}} and are comparable to the -\href{https://portal.statistik.at/statistik.at/ext/statcube/jsf/dataCatalogueExplorer.xhtml}{catalogue explorer} or the -\href{https://portal.statistik.at/statistik.at/ext/statcube/opendatabase?id=deake005}{table view} of the STATcube GUI. +catalogue explorer or the +table view of the STATcube GUI. The responses of the API are tree-like data structures which are wrapped into a class called \code{sc_schema} to simplify the usage in R.