Learn R Programming

MplusAutomation (version 0.2-5)

replaceTags:

Usage

replaceTags(regexp = NULL, syntaxFile, toProcess, perl = T, replaceType)

Arguments

regexp
syntaxFile
toProcess
perl
replaceType

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 (regexp = NULL, syntaxFile, toProcess, perl = T, replaceType) 
    {
        if (!is.list(toProcess)) {
            stop("Argument list passed to replaceTags is not a list")
        }
        if (length(syntaxFile) == 0) {
            return(NA)
        }
        if (replaceType == "simple" || replaceType == "final") {
            regexp <- paste("\[\[\s*(?!", paste(toProcess$iterators, 
                collapse = "|"), ")(\w+)\s*\]\]", sep = "")
            tags <- gsubfn(regexp, function(tag, simpleWord) {
                replaceText <- eval(parse(text = paste("toProcess$", 
                    simpleWord, sep = "")))
                if (is.null(replaceText)) {
                    stop("When replacing tag: ", tag, ", could not find corresponding value.")
                }
                replaceText
            }, syntaxFile, perl = perl, backref = 1)
        }
        else if (replaceType == "array") {
            regexp <- paste("\[\[\s*(\w+)#", toProcess$curIteratorName, 
                "\s*\]\]", sep = "")
            tags <- gsubfn(regexp, function(tag, arrayMatch) {
                replaceText <- eval(parse(text = paste("toProcess$", 
                    arrayMatch, "[", toProcess$curItPos[toProcess$curIteratorDepth], 
                    "]", sep = "")))
                if (is.null(replaceText)) {
                    stop("When replacing tag: ", tag, ", could not find corresponding value.")
                }
                replaceText
            }, syntaxFile, perl = perl, backref = 1)
        }
        else if (replaceType == "iterator") {
            regexp <- paste("\[\[\s*(", toProcess$curIteratorName, 
                ")\s*\]\]", sep = "")
            tags <- gsubfn(regexp, function(tag, arrayMatch) {
                replaceText <- as.character(toProcess$curItPos[toProcess$curIteratorDepth])
                if (is.null(replaceText)) {
                    stop("When replacing tag: ", tag, ", could not find corresponding value.")
                }
                replaceText
            }, syntaxFile, perl = perl, backref = 1)
        }
        if (replaceType == "final") {
            regexp <- paste("\[\[\s*(", paste(toProcess$iterators, 
                collapse = "|"), ")\s*\]\]", sep = "")
            tags <- gsubfn(regexp, function(tag, arrayMatch) {
                iteratorPosition <- grep(paste("\b", arrayMatch, 
                    "\b", sep = ""), toProcess$iterators, perl = T)
                if (length(iteratorPosition) == 1) {
                    replaceText <- as.character(toProcess$curItPos[iteratorPosition])
                }
                if (is.null(replaceText)) {
                    stop("When replacing tag: ", tag, ", could not find corresponding value.")
                }
                replaceText
            }, tags, perl = perl, backref = 1)
            regexp <- paste("\[\[\s*\b(\w+)#(", paste(toProcess$iterators, 
                collapse = "|"), ")\b\s*\]\]", sep = "")
            tags <- gsubfn(regexp, function(tag, arrayMatch, iteratorMatch) {
                iteratorPosition <- grep(paste("\b", iteratorMatch, 
                    "\b", sep = ""), toProcess$iterators, perl = T)
                if (length(iteratorPosition) == 1) {
                    replaceText <- eval(parse(text = paste("toProcess$", 
                      arrayMatch, "[", toProcess$curItPos[iteratorPosition], 
                      "]", sep = "")))
                }
                if (is.null(replaceText)) {
                    stop("When replacing tag: ", tag, ", could not find corresponding value.")
                }
                replaceText
            }, tags, perl = perl, backref = 2)
        }
        tags
      }

    Run the code above in your browser using DataLab