Skip to content

Commit

Permalink
fix merge of recodes & labels
Browse files Browse the repository at this point in the history
  • Loading branch information
cturbelin committed Dec 17, 2018
1 parent 3cd1b11 commit c605fae
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions R/platform.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,14 @@ create_survey_definition <- function( mapping, labels=NULL, codes=NULL, recodes=
labels[[name]] <<- recodes[[name]]
})
}

checks = NULL

if( !is.null(template) ) {
if( is.null(survey_templates[[template]]) ) {
stop(paste0("Unknown template '", template,'"'))
}

template = survey_templates[[template]]

checks = check_survey_template(template, mapping, recodes, only.errors = only.errors)
Expand All @@ -170,15 +173,18 @@ create_survey_definition <- function( mapping, labels=NULL, codes=NULL, recodes=

# Merge recodes
rr = list()
nn = unique(names(recodes), names(template$recodes))
nn = unique(c(names(recodes), names(template$recodes)))
for(name in nn) {
rr[[name]] = merge_by_value(recodes[[name]], template$recodes[[name]])
if( !check_unique(rr[name]) ) {
stop(paste0("Values are not unique for recode ", name))
}
}
recodes = rr
rm(rr)

# Merge labels
labels = merge.list(labels, template$labels)

}

# Check if any mapping to protected names
Expand Down Expand Up @@ -257,6 +263,10 @@ check_survey_template <- function(template, mapping, recodes, only.errors=TRUE)
#' @param errors list() errors generated by \code{\link{create_survey_definition}}
#' @export
print.survey_error = function(errors) {
if( length(errors)== 0 ) {
cat("No error")
return()
}
cat("Error merging survey with template\n")
rr = lapply(errors, function(e) {
n = ""
Expand Down

0 comments on commit c605fae

Please sign in to comment.