camr_check_name_conventions
The following function is proposed for inclusion. Its purpose is to warn the user if a dataframe contains column names that do not adhere to our standards.
camr_check_name_conventions <- function(df_any) {
for (chr_name in names(df_any)) {
m_match <- str_match(chr_name, '(?<group>\\w{3})\\.(?<type>\\w{3})\\.(?<topic>\\w+)\\.?(?<tail>.+)?')
if (any(is.na(m_match[1, c('group', 'type', 'topic')]))) {
warning('Variable name is of invalid format: ', chr_name)
next
}
if (!(m_match[[1, 'group']] %in% c(
'IDX', 'IDS', 'SBJ', 'SSS', 'DRP',
'RES', 'BIO', 'INV', 'QCC', 'VST'
))) {
warning('Variable name contains invalid group: ', chr_name)
next
}
if (!switch(
m_match[[1, 'type']],
INT = is.integer,
DBL = is.numeric,
CHR = is.character,
MCQ = is.factor,
FCT = is.factor,
CKB = is.logical,
DAT = is.Date,
DTM = is.POSIXt,
DUR = is_duration,
TIM = is.period,
DUR = is.period,
LGL = is.logical,
{
warning('Variable name contains invalid type: ', chr_name)
next
}
)(df_any[[chr_name]])) {
warning(
'Variable name does not reflect type: ', chr_name,
' (', class(df_any[[chr_name]]), ': ', mode(df_any[[chr_name]]), ')'
)
}
}
df_any
}