Learn R Programming

MplusAutomation (version 0.2-5)

processConditionalTags:

Usage

processConditionalTags(syntaxFile, toProcess, perl = T)

Arguments

syntaxFile
toProcess
perl

Value

    Examples

    Run this code
    ##---- Should be DIRECTLY executable !! ----
    ##-- ==>  Define data, use random,
    ##--	or do  help(data=index)  for the standard data sets.
    
    ## The function is currently defined as
    function (syntaxFile, toProcess, perl = T) 
    {
        regexp <- paste("\[\[\s*(?:", paste(toProcess$iterators, 
            collapse = "|"), ")\s*[!><=]+\s*\d+\s*\]\]", sep = "")
        tagStarts <- grep(regexp, syntaxFile, perl = T)
        if (length(tagStarts) == 0) {
            return(syntaxFile)
        }
        linesToDrop <- c()
        for (i in 1:length(tagStarts)) {
            regexp <- paste("\[\[\s*((?:", paste(toProcess$iterators, 
                collapse = "|"), ")\s*[!><=]+\s*\d+)\s*\]\]", 
                sep = "")
            tagPart <- unlist(strapply(syntaxFile[tagStarts][i], 
                regexp, c, perl = T, backref = -1))
            for (j in 1:length(tagPart)) {
                endregexp <- paste("\[\[\s*\/", tagPart[j], "\s*\]\]", 
                    sep = "")
                sameLineEnd <- regexpr(endregexp, syntaxFile[tagStarts][i], 
                    perl = T)
                if (sameLineEnd > 0) {
                    startPos <- regexpr(regexp, syntaxFile[tagStarts][i], 
                      perl = TRUE)
                    if (evaluateConditional(tagPart[j], toProcess)) {
                      syntaxFile[tagStarts][i] <- paste(substring(syntaxFile[tagStarts][i], 
                        c(1, (startPos + attr(startPos, "match.length")), 
                          (sameLineEnd + attr(sameLineEnd, "match.length"))), 
                        c((startPos - 1), (sameLineEnd - 1), nchar(syntaxFile[tagStarts][i]))), 
                        collapse = "")
                    }
                    else {
                      syntaxFile[tagStarts][i] <- paste(substring(syntaxFile[tagStarts][i], 
                        c(1, (sameLineEnd + attr(sameLineEnd, "match.length"))), 
                        c((startPos - 1), nchar(syntaxFile[tagStarts][i]))), 
                        collapse = "")
                    }
                }
                else {
                    tagEnd <- grep(endregexp, syntaxFile[-1:-tagStarts[i]], 
                      perl = T)
                    if (length(tagEnd) >= 1) {
                      tagEnd <- tagEnd[1] + tagStarts[i]
                    }
                    else if (length(tagEnd) == 0) {
                      stop("Could not find matching close tag for conditional tag: ", 
                        tagPart)
                    }
                    if (evaluateConditional(tagPart[j], toProcess)) {
                      linesToDrop <- c(linesToDrop, tagStarts[i], 
                        tagEnd)
                    }
                    else {
                      linesToDrop <- c(linesToDrop, tagStarts[i]:tagEnd)
                    }
                }
            }
        }
        syntaxFile <- syntaxFile[!1:length(syntaxFile) %in% linesToDrop]
        return(syntaxFile)
      }

    Run the code above in your browser using DataLab