statistikat / STATcubeR

R interface for the STATcube REST API and data.statistik.gv.at
https://statistikat.github.io/STATcubeR/
GNU General Public License v2.0
14 stars 2 forks source link

Allow recoding of `sc_data` objects #17

Closed GregorDeCillia closed 3 years ago

GregorDeCillia commented 3 years ago

Add functionalities that allow modifications of labels and similar. Define a new R6 class and put an instance into x$recode.

changes to public methods

x <- od_table()

# set labels for fields
x$recode$label(code_field, new_label, language)
# set labels for measures
x$recode$label(code_measure, new_label, language)
# set labels of levels
x$recode$level(code_field, code_level, new_label, language)

# set total codes similar to x$total_codes() but for programmatic usage
x$recode$total_code(code_field, code_level)
# codes_levels is a permutation of x$field(code_field)$code
x$recode$order(code_field, codes_levels)
# define which levels are included in $tabulate(). 
x$recode$visible(code_field, code_level, visible = TRUE)

# undo all recodes
x$recode$reset()

Implementation

All modifications should directly overwrite x$meta and x$fields(i). The functionality should be bilingual, i.e. it should be possible to define german and english labels.

# in initialize()
private$recoder <- recoder_class$new(self, private)
# in active bindings
recode = function(value) {
  private$recoder
}

It would probably be useful to add extra columns visible and order in x$field(i) to store this part of the "recode state". We could also just store them in $private$p_fields[[i]] and omit them in the active field

active = list(field = function(i) {
  privae$p_fields[[i]][, -c("order", "visible)]
})

pluralization

We could also add "pluralized" versions that implement the recodes such as

x$recode$labels(code_measures, new_labels, language)
x$recode$levels(code_field, code_levels, new_labels, language)
# ...
GregorDeCillia commented 3 years ago

Some further options that are more specific to our internal needs

## set the color used for a level in visualizations
x$recode$color(code_field, code_level, hex_color)
## modify how time values are parsed ???
x$recode$parser(code_field, time_format = "YYYY-Q")