Created
January 22, 2021 01:48
-
-
Save jmcurran/e4ab8eb5cca84289b67868788fbbe04e to your computer and use it in GitHub Desktop.
An R function for matching a parameter pattern with a list of possible parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A function for matching a parameter pattern with a list of possible parameters | |
# matchParams("b0", c("b0", "b1", "b2", "sigma")) - should return 1 | |
# matchParams("b.", c("b0", "b1", "b2", "sigma")) - should return 1 2 3 | |
# matchParams("b[1-2]", c("b0", "b1", "b2", "sigma")) - should return 2 3 | |
# matchParams(c("b0", "b1"), c("b0", "b1", "b2", "sigma")) - should return 1 2 | |
# matchParams(c("b0", "b1", "b4"), c("b0", "b1", "b2", "sigma")) - should fail because b4 doesn't match | |
# matchParams(c("b0", "b."), c("b0", "b1", "b2", "sigma")) - should return 1 2 3 not 1 1 2 3 as it looks for unique matches | |
matchParams = function(params, paramList){ | |
whichMatch = function(r){ | |
## how many parameters does params match? | |
numMatchingParams = sum(r) | |
if(numMatchingParams == 0){ | |
return(-1) | |
}else{ | |
return(which(r)) ## these should be unique | |
} | |
} | |
numParams = length(paramList) | |
m = if(length(params) == 1){ | |
grepl(params, paramList) | |
}else{ | |
lapply(params, grepl, x = paramList) | |
} | |
if(is.list(m)){ | |
m1 = lapply(m, whichMatch) | |
## firstly check if there were any failures | |
fail = any(sapply(m1, function(results)any(results == -1))) | |
if(fail){ | |
failures = which(sapply(m1, function(results)any(results == -1))) | |
stop(paste0("Could not find pattern(s) \"", | |
paste0(params[failures], collapse = ", \""), | |
"\" in parameter list")) | |
} | |
## Assuming there are not, collapse the results (in case of non-unique requests) | |
return(unique(unlist(m1))) | |
}else{ | |
return(whichMatch(m)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment