diff --git a/NEWS.md b/NEWS.md index 96be491e..2b5ab577 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,7 @@ * Add filters and other recodes to `sc_table_custom()` (#33) * Add global option `STATcubeR.language` to override the default language * `od_table()`: Add descriptions to `x$header` and `x$field(i)` +* Allow json strings in `sc_table()` (@matmo, #36) # STATcubeR 0.5.0 diff --git a/R/table.R b/R/table.R index 809977af..3f51a54c 100644 --- a/R/table.R +++ b/R/table.R @@ -92,8 +92,10 @@ sc_table_class <- R6::R6Class( #' provided by the API. #' @param annotations Include separate annotation columns in the returned #' table. This parameter is currently broken and needs to be re-implemented - tabulate = function(..., round = TRUE, annotations = FALSE) { - sc_table_tabulate(self, ..., round = round, annotations = annotations) + #' @param recode_zeros interpret zero values as missings? + tabulate = function(..., round = FALSE, annotations = FALSE, recode_zeros = FALSE) { + sc_table_tabulate(self, ..., round = round, annotations = annotations, + recode_zeros = recode_zeros) }, #' @description open the dataset in a browser browse = function() { @@ -168,8 +170,9 @@ sc_table_class <- R6::R6Class( #' * [sc_table_saved()] uses a table uri of a saved table. #' #' Those three functions all return an object of class `"sc_table"`. -#' @param json_file path to a json file, which was downloaded via the STATcube -#' GUI ("Open Data API Abfrage") +#' @param json Path to a json file, which was downloaded via the STATcube +#' GUI ("Open Data API Request"). Alternatively, a json string which +#' passes [jsonlite::validate()]. #' @param add_totals Should totals be added for each classification field in #' the json request? #' @return An object of class `sc_table` which contains the return @@ -181,9 +184,10 @@ sc_table_class <- R6::R6Class( #' will use english. `"de"` uses german. #' The third option `"both"` will import both languages by sending two requests #' to the `/table` endpoint. +#' @param json_file Depricated. Use `json` instead #' @family functions for /table #' @examplesIf sc_key_exists() -#' my_table <- sc_table(json_file = sc_example("population_timeseries.json")) +#' my_table <- sc_table(json = sc_example("population_timeseries.json")) #' #' # print #' my_table @@ -206,14 +210,15 @@ sc_table_class <- R6::R6Class( #' my_response <- sc_table_saved(table_uri) #' as.data.frame(my_response) #' @export -sc_table <- function(json_file, language = NULL, add_totals = TRUE, - key = NULL) { +sc_table <- function(json, language = NULL, add_totals = TRUE, key = NULL, + json_file = NA) { + json <- normalize_json(json, json_file) language <- sc_language(language, c("en", "de", "both")) both <- language == "both" if (both) language <- "de" - res <- sc_table_json_post(readLines(json_file, warn = FALSE), language, add_totals, key) %>% - sc_table_class$new(file = json_file, add_totals = add_totals) + 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) res @@ -239,6 +244,19 @@ print.sc_table <- function(x, ...) { cat(format(x, ...), sep = "\n") } +normalize_json <- function(json, json_file) { + if (!is.na(json_file)) { + json <- json_file + warning("parameter `json_file` was renamed to `json`") + } + file <- NULL + if (length(json) == 1 && !jsonlite::validate(json)) { + file <- json + json <- readLines(file) + } + list(file = file, string = json) +} + format.sc_table <- function(x, ...) { c( cli::style_bold(strwrap(x$meta$source$label)), diff --git a/man/sc_table.Rd b/man/sc_table.Rd index dd937647..2c0f9b8b 100644 --- a/man/sc_table.Rd +++ b/man/sc_table.Rd @@ -8,7 +8,7 @@ \alias{sc_table_saved} \title{Create a request against the /table endpoint} \usage{ -sc_table(json_file, language = NULL, add_totals = TRUE, key = NULL) +sc_table(json, language = NULL, add_totals = TRUE, key = NULL, json_file = NA) sc_examples_list() @@ -19,8 +19,9 @@ sc_table_saved_list(key = NULL, server = "ext") sc_table_saved(table_uri, language = NULL, key = NULL, server = "ext") } \arguments{ -\item{json_file}{path to a json file, which was downloaded via the STATcube -GUI ("Open Data API Abfrage")} +\item{json}{path to a json file, which was downloaded via the STATcube +GUI ("Open Data API Request"). Alternatively, a json string which +passes \code{\link[jsonlite:validate]{jsonlite::validate()}}.} \item{language}{The language to be used for labeling. \code{"en"} (the default) will use english. \code{"de"} uses german. @@ -33,6 +34,8 @@ the json request?} \item{key}{(\code{string}) An API key. To display your key, call \code{\link[=sc_browse_preferences]{sc_browse_preferences()}}.} +\item{json_file}{Depricated. Use \code{json} instead} + \item{filename}{The name of an example json file.} \item{server}{A STATcube API server. Defaults to the external Server via @@ -61,7 +64,7 @@ Those three functions all return an object of class \code{"sc_table"}. } \examples{ \dontshow{if (sc_key_exists()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -my_table <- sc_table(json_file = sc_example("population_timeseries.json")) +my_table <- sc_table(json = sc_example("population_timeseries.json")) # print my_table