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

filters in sc_table_custom() #33

Open
GregorDeCillia opened this issue Oct 14, 2022 · 1 comment
Open

filters in sc_table_custom() #33

GregorDeCillia opened this issue Oct 14, 2022 · 1 comment
Assignees
Labels
API feature New feature or request
Milestone

Comments

@GregorDeCillia
Copy link
Contributor

There have now been several requests to support filtering in sc_table_custom(). Currently, the only way to do this is to generate the request.json by hand.

Example
library(STATcubeR)

schema <- sc_schema_db("detouextregsai")
region <- schema$`Other Classifications`$
  `Tourism commune [ABO]`$`Regionale Gliederung (Ebene +1)`

request <- list(
  database = schema$id,
  dimensions = list(I(region$id)),
  recodes = setNames(
    list(list(
      map = list(
        I(region$Bregenzerwald$id),
        I(region$`Vorarlberg Rest`$id),
        I(region$`Bodensee-Vorarlberg`$id)
      )
    )),
    region$id
  )
)

jsonlite::write_json(request, "request.json", pretty = TRUE, auto_unbox = TRUE)
readLines("request.json") %>% cat(sep = "\n")
x <- sc_table("request.json", add_totals = FALSE)
x$tabulate()

It might be sensible to extend the functionality of sc_table_custom() to support filters (or possibly other recodes) via additional parameters. The syntax might look like this

library(STATcubeR)

schema <- sc_schema_db("detouextregsai")
region <- schema$`Other Classifications`$
  `Tourism commune [ABO]`$`Regionale Gliederung (Ebene +1)`

sc_table_custom(
  schema,
  region,
  sc_recode(region, c(region$Bregenzerwald, 
      region$`Vorarlberg Rest`, region$`Bodensee-Vorarlberg`)) 
)
@GregorDeCillia GregorDeCillia added feature New feature or request API labels Oct 14, 2022
@GregorDeCillia GregorDeCillia self-assigned this Oct 14, 2022
GregorDeCillia added a commit that referenced this issue Nov 25, 2022
first attempt to resolve #33. Recodes
can now be defined with an additional
parameter. However, type-checking is
very minimal.

TODO:
- better error handling when the
  request is constructed. This way
  users get quick and useful error
  messages - at least for semantic
  errors such as invalid usage of
  parameters
- with this implementation, users will
  have to make sure that the parameters
  "recodes" and "dimensions" are
  consistent. Maybe simplify the usage
- The naming sc_recode is almost
  conflicting with the class
  sc_recoder. Possibly rename this
  function
- extend the custom tables article to
  showcase some usecases for recodes
  and add a short discussion about
  usage limits
- maybe add sc_filter which only allows
  filter-type recodes and performs
  stricter type-checks?
@GregorDeCillia GregorDeCillia linked a pull request Nov 25, 2022 that will close this issue
5 tasks
@GregorDeCillia
Copy link
Contributor Author

There is now a first implementation which allows to set filters

STATcubeR usage
schema <- sc_schema_db("detouextregsai")
region <- schema$`Other Classifications`$`Tourism commune [ABO]`$
  `Regionale Gliederung (Ebene +1)`
month <- schema$`Mandatory fields`$`Season/Tourism Month`

x <- sc_table_custom(
  schema,
  schema$Facts$Arrivals,
  list(month, region),
  recodes = c(
    sc_recode(region, total = FALSE, map = list(
      region$Achensee,
      list(region$Arlberg, region$`Ausseerland-Salzkammergut`)
    )),
    sc_recode(month, total = FALSE)
  )
)
x$tabulate()
resulting data.frame
# A STATcubeR tibble: 92 x 3
   `Season/Tourism Month` `Tourism commune [ABO]`           Arrivals
   <date>                 <fct>                                <dbl>
 1 2000-01-01             Achensee                             90947
 2 2000-01-01             Arlberg;Ausseerland-Salzkammergut   209356
 3 2000-06-01             Achensee                            133523
 4 2000-06-01             Arlberg;Ausseerland-Salzkammergut   109769
 5 2001-01-01             Achensee                             93178
 6 2001-01-01             Arlberg;Ausseerland-Salzkammergut   199256
 7 2001-06-01             Achensee                            137138
 8 2001-06-01             Arlberg;Ausseerland-Salzkammergut   112685
 9 2002-01-01             Achensee                            100464
10 2002-01-01             Arlberg;Ausseerland-Salzkammergut   212284
# … with 82 more rows
# ℹ Use `print(n = ...)` to see more rows
resulting API request
{
  "database": "str:database:detouextregsai",
  "measures": [
    "str:statfn:detouextregsai:F-DATA1:F-ANK:SUM",
    "str:measure:detouextregsai:F-DATA1:F-UEB"
  ],
  "dimensions": [
    [
      "str:field:detouextregsai:F-DATA1:C-SDB_TIT-0"
    ],
    [
      "str:valueset:detouextregsai:F-DATA1:C-C93-2:C-C93SUM-0"
    ]
  ],
  "recodes": {
    "str:field:detouextregsai:F-DATA1:C-SDB_TIT-0": {
      "total": true
    },
    "str:valueset:detouextregsai:F-DATA1:C-C93-2:C-C93SUM-0": {
      "total": true
    }
  }
}

This feature is currently only available in the development branch #32 . For anyone interested, the package can be instlled from the dev-branch. However, these new features are likely to be refactored before #32 is merged.

remotes::install_github("statistikat/STATcubeR", ref = "tibble_pkg")

GregorDeCillia added a commit that referenced this issue Dec 9, 2022
there are now several checks in place
that throw warnings if inputs in
sc_table_custom() or sc_recode() are
of the wrong schema-type or if other
inconsistencies are suspected. See
the section called "error handling"
in ?sc_table_custom for more details

some of those warnings might be
replaced with errors in the future

part of #33
@GregorDeCillia GregorDeCillia added this to the Version 1.0 milestone Dec 16, 2022
@GregorDeCillia GregorDeCillia pinned this issue Dec 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant