-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary.R
81 lines (68 loc) · 2.72 KB
/
dictionary.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#' ---
#' title: "Build Data Dictionary"
#' author:
#' - "Alex F. Bokov^[UT Health, Department of Epidemiology and Biostatistics]"
#' date: "09/14/2018"
#' ---
#'
#+ message=F,echo=F
# init ----
debug <- 0;
.projpackages <- c( 'purrr', 'dplyr' );
.globalpath <- c(list.files(patt='^global.R$',full=T)
,list.files(path='scripts',patt='^global.R$',full=T)
,list.files(rec=T,pattern='^global.R$',full=T)
,list.files(path='..',patt='^global.R$',full=T)
,list.files(path='..',rec=T,pattern='^global.R$',full=T))[1];
if(debug>0) source(.globalpath,chdir = TRUE, local=TRUE) else {
.junk<-capture.output(source(.globalpath,chdir=TRUE, echo=FALSE, local=TRUE))};
.currentscript <- current_scriptname('dictionary.R');
#' Setting default arguments
#' Saving original file-list so we don't keep exporting functions and
#' environment variables to other scripts
.origfiles <- ls();
#+ echo=FALSE
#### read_maps ####
#' Read the mapping files
#'
#' The data dictionary generated by DataFinisher for the current data pull
map0 <- try_import(inputdata['map0']);
#' Persistent data dictionary that (hopefully) will work over multiple data
#' pulls
map1 <- try_import(inputdata['map1']);
map2 <- try_import(inputdata['map2']);
#' Patient safety indicators
psi <- try_import(inputdata['psi']);
#' Do some cleanup
map0$c_info[is.na(map0$c_info)] <- FALSE;
#' Create code-groups (the info column only, because that's where actual codes
#' are stored)
for(ii in c('ICD9','ICD10','ICD9PCS','ICD10PCS','CPT','LOINC')){
map0[[paste0('c_',tolower(ii))]] <- map0$ddomain == ii & map0$c_info;
}
#' Create a column to bind by (unstable solution, might stop working)
map0$standard_code <- paste0(map0$ddomain,':',gsub(' - .*$','',map0$name));
#' Assign diagnosis columns to patient safety categories
for(ii in (psinames<-unique(psi$shortname))) {
map0[[paste0('c_',ii)]] <- select(map0,'standard_code') %>%
left_join(subset(psi,shortname==ii)[,c('icd10_ccode','shortname')]
,by=c('standard_code'='icd10_ccode')) %>%
select('shortname') %>% is.na %>% `|`(map0$c_info) %>% `!`;
};
#' Columns that participate in any PSI category
map0$c_psi <- map0[,paste0('c_',unique(psinames))] %>% rowSums > 0;
#' Join on map1 and add rows from map2
map3 <- left_join(map0,map1) %>% bind_rows(map2);
#' Insure no missing display names
map3$dispname_short <- with(map3,coalesce(dispname_short,dispname));
# QC ----
#' # QC warnings (if any)
#'
#' `.missing_from_response` should be empty.
.missing_from_response <- setdiff(v(c_mainresponse,dictionary=map3)
,v(c_response,dictionary=map3));
.missing_from_response;
#### write varmap.csv ####
export(map3,'varmap.csv');
#+ echo=F,eval=F
c()