Filters for entries up to a given entry-number or timestamp, then returns the latest entry per key.

Use rr_snapshot() for a snapshot of a whole register (both schema and data).

rr_records(entries, sequence = c("entry-number", "timestamp"),
  maximum = NULL, include_maximum = TRUE)

Arguments

entries

A data frame or tibble with at least the fields key and either entry-number or timestamp, depending on the value of sequence.

sequence

One of "entry-number" (default) or "timestamp". The snapshot is taken at the maximum value of this field of entries.

maximum

An integer if sequence is "entry-number" orPOSIXctifsequenceis"timestamp", giving the time at which to take the snapshot. Only the latest entry up to this value will be kept, perkey`. By default it is the maximum of all entries, to return the most recent state of the register.

include_maximum

Logical, whether to include entries whose entry-number or timestamp equals maximum.

Value

A tibble of records (latest entry per key).

Details

A record is the latest entry for a given key. 'Latest' can be either the maximum entry-number (recommended), or the maximum timestamp. An entry timestamp may be NA, and may not be unique. If the timestamp of any entry of a given key is NA, then no record for that key will be returned. If the timestamp is not unique, then the entry with the maximum entry-number will be chosen.

Examples

country <- rr_register("country") # There can be more than one entry per key country$data %>% dplyr::filter(key == "CZ") %>% dplyr::select(`entry-number`, timestamp, name, `official-name`)
#> # A tibble: 2 x 4 #> `entry-number` timestamp name `official-name` #> <int> <dttm> <chr> <chr> #> 1 64 2016-04-05 13:23:05 Czech Republic The Czech Republic #> 2 217 2016-11-11 16:25:07 Czechia The Czech Republic
# Snapshot by entry number country$data %>% rr_records(maximum = 64) %>% dplyr::filter(key == "CZ") %>% dplyr::select(`entry-number`, timestamp, name, `official-name`)
#> # A tibble: 1 x 4 #> `entry-number` timestamp name `official-name` #> <int> <dttm> <chr> <chr> #> 1 64 2016-04-05 13:23:05 Czech Republic The Czech Republic
country$data %>% rr_records(maximum = 217, include_maximum = FALSE) %>% dplyr::filter(key == "CZ") %>% dplyr::select(`entry-number`, timestamp, name, `official-name`)
#> # A tibble: 1 x 4 #> `entry-number` timestamp name `official-name` #> <int> <dttm> <chr> <chr> #> 1 64 2016-04-05 13:23:05 Czech Republic The Czech Republic
# Snapshot by entry timestamp country$data %>% rr_records(sequence = "timestamp", maximum = as.POSIXct("2016-04-05 13:23:05", tz = "UTC")) %>% dplyr::filter(key == "CZ") %>% dplyr::select(`entry-number`, timestamp, name, `official-name`)
#> # A tibble: 1 x 4 #> `entry-number` timestamp name `official-name` #> <int> <dttm> <chr> <chr> #> 1 64 2016-04-05 13:23:05 Czech Republic The Czech Republic
# When the timestamp of any entry of a key is missing, no entries are # returned. entries <- country$data entries$timestamp[entries$`entry-number` == 64] <- NA rr_records(entries, sequence = "timestamp") %>% dplyr::filter(key == "CZ")
#> # A tibble: 0 x 11 #> # ... with 11 variables: `entry-number` <int>, type <chr>, key <chr>, #> # timestamp <dttm>, hash <chr>, country <chr>, name <chr>, #> # `official-name` <chr>, `citizen-names` <chr>, `start-date` <chr>, #> # `end-date` <chr>
# Not all entries are data, some are schema country$schema$custodians %>% rr_records(maximum = 11) %>% dplyr::select(`entry-number`, timestamp, custodian)
#> # A tibble: 1 x 3 #> `entry-number` timestamp custodian #> <int> <dttm> <chr> #> 1 2 2017-07-17 10:59:47 Tony Worron