Skip to content

Commit

Permalink
changes related to subsamble table functionality; fix #33
Browse files Browse the repository at this point in the history
  • Loading branch information
stolarczyk committed Jan 9, 2020
1 parent 4fc9293 commit 4bd18ea
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
7 changes: 1 addition & 6 deletions R/loadConfig.R
Expand Up @@ -159,7 +159,7 @@
return(path)
}

#' Format a string like python's format function
#' Format a string like python's format method
#'
#' Given a string with environment variables (encoded like \code{${VAR}} or \code{$VAR}), and
#' other variables (encoded like \code{{VAR}}) this function will substitute
Expand Down Expand Up @@ -189,11 +189,6 @@
argsLengths = lapply(argsUnlisted, length)
if (any(argsLengths > 1)) {
pluralID = which(argsLengths > 1)
# Remove the previously interpolated,
# thus plural elements from another round of interpolation
if (any(names(pluralID) %in% exclude)) {
pluralID = pluralID[-which(names(pluralID) %in% exclude)]
}
attrCount = sapply(argsUnlisted, length)[pluralID]
nrows = unique(attrCount)
if(length(nrows) > 1) {
Expand Down
5 changes: 3 additions & 2 deletions R/models.R
Expand Up @@ -403,8 +403,8 @@ setMethod(
}
regex = cfg$data_sources[[sampDataSource]]
if (!is.null(regex)) {
a = .strformat(regex, as.list(samp), exclude, parentDir)
samp[[column]] = list(system(sprintf("echo %s",a), intern = TRUE))
formatted = .strformat(regex, as.list(samp), exclude, parentDir)
samp[[column]] = .matchesAndRegexes(formatted)
}
listOfSamples[[iSamp]] = samp
}
Expand All @@ -416,6 +416,7 @@ setMethod(
.Object
}


.implyAttributes = function(.Object) {
if (is.null(.Object@config$implied_attributes)) {
# Backwards compatibility
Expand Down
20 changes: 19 additions & 1 deletion R/utils.R
Expand Up @@ -159,6 +159,7 @@ fetchSamples = function(samples, attr=NULL, func=NULL, action="include") {
#' A designated place for any config transformations/sanity checks, like keys deprecation handling
#'
#' @param cfg config or a section of one to sanitize
#'
#' @return sanitized config
#' @export
.sanitizeConfig = function(cfg) {
Expand All @@ -170,4 +171,21 @@ fetchSamples = function(samples, attr=NULL, func=NULL, action="include") {
}
}
return(cfg)
}
}

#' Create a list of matched files in the system and unmatched regular expessions
#'
#' @param rgx string to expand in the system
#'
#' @return a list of all the elements after possible expansion
.matchesAndRegexes = function(rgx) {
res = c()
for(i in rgx){
matched = Sys.glob(i)
if(length(matched) < 1) {
matched = i
}
res = c(res, matched)
}
return(list(res))
}
4 changes: 2 additions & 2 deletions man/dot-strformat.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions tests/testthat/test_utils.R
Expand Up @@ -153,10 +153,18 @@ test_that(".expandPath work with both curly braced and non-curly braced
.expandPath("${HOME}/my/path/string.txt"))
})

test_that(".strformat works",{
expect_null(.strformat("~/{VAR1}{VAR2}_file", list(VAR1=c("hi","a"), VAR2="hello"), exclude = "VAR1"))
expect_is(.strformat("~/{VAR1}{VAR2}_file", list(VAR1="hi", VAR2="hello"), exclude = "test"),"character")
expect_is(.strformat("~/{VAR1}{VAR2}_file", list(VAR1="hi", VAR2="hello")),"character")
test_that(".strformat returns an object of correct class", {
expect_is(.strformat("~/{VAR1}{VAR2}_file", list(VAR1="var1a", VAR2="var2a")), "character")
})

test_that(".strformat works with multiple values", {
expect_equal(length(.strformat("{VAR1}_{VAR2}", list(VAR1=c("var1a", "var1b"), VAR2="var2a"))), 2)
expect_equal(length(.strformat("{VAR1}_{VAR2}", list(VAR1=c("var1a", "var1b"), VAR2=c("var2a", "var2b")))), 2)
expect_equal(length(.strformat("{VAR1}_{VAR2}", list(VAR1=c("var1a", "var1b"), VAR2="var2a"))), 2)
})

test_that(".strformat fails when multiple multi-replacemenets of different lengths are used", {
expect_error(.strformat("{VAR1}_{VAR2}", list(VAR1=c("var1a", "var1b"), VAR2=c("var2a", "var2b","var2c"))))
})

test_that(".printNestedList produces an output", {
Expand Down

0 comments on commit 4bd18ea

Please sign in to comment.