radar2/anonymize.R

56 lines
1.5 KiB
R

require(tidyverse, quietly = T)
require(lubridate, quietly = T)
require(readr)
require(uuid, quietly = T)
pinWidth = 7
intTopin = function(x) {
return(formatC(x, width = 7, format = "d", flag = "0"))
}
# tPinMapping = tibble(origpin = intTopin(0),
# uupin = '78c3fe82-9343-4aa8-8b06-afc6da79a39a')
source('config.R', echo = F)
tPinMapping = read_rds(pinMappingDictFilename)
getUupin = function(pin) {
if (length(pin) == 1) {
if (class(pin) == 'character') {
if (pin %in% tPinMapping$origpin) {
return((tPinMapping %>% filter(origpin == pin) %>% pull(uupin)))
} else {
if (str_length(pin) == pinWidth) {
newUupin = UUIDgenerate()
tPinMapping <<- tPinMapping %>%
add_row(origpin = pin, uupin = newUupin)
tPinMapping %>% write_rds('data/pinMapping.rds', compress = 'gz')
return(newUupin)
} else {
stop(paste0('pin width <> ', pinWidth))
}
}
} else {
stop(paste0('class of pin should be character, but is ', class(pin)))
}
} else {
stop('Please supply exactly 1 pin')
}
}
getOrigpin = function(uuId) {
tPinMapping %>% filter(uupin == uuId) %>% pull(origpin)
}
anonymizeRds = function(x, ymdStartDate, ymdEndDate, suffix) {
y = x %>%
rowwise() %>%
mutate(patientid = getUupin(patientid)) %>%
ungroup() %>%
mutate(birth_date = ymd('1970-01-01'))
filename = paste0('data/bk_', ymdStartDate, '_', ymdEndDate, '_', suffix, '.rds')
if (!file.exists(filename)) {
write_rds(y, file = filename)
}
}