Learn R Programming

MplusAutomation (version 0.2-5)

recurseReplace:

Usage

recurseReplace(bodysection, toProcess, curiterator = 1)

Arguments

bodysection
toProcess
curiterator

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 (bodysection, toProcess, curiterator = 1) 
    {
        if (!is.list(toProcess)) {
            stop("Argument list passed to recurseReplace is not a list")
        }
        thisIterator <- toProcess$iterators[curiterator]
        toProcess$curIteratorName <- thisIterator
        toProcess$curIteratorDepth <- curiterator
        for (i in toProcess[[thisIterator]]) {
            toProcess$curItPos[curiterator] <- i
            print(paste("current iterator is: ", thisIterator, "position:", 
                as.character(i)))
            bodysection <- processForeachTags(bodysection, toProcess)
            if (curiterator < length(toProcess$iterators)) {
                recurseReplace(bodysection, toProcess, curiterator = curiterator + 
                    1)
            }
            else {
                toWrite <- processConditionalTags(bodysection, toProcess, 
                    perl = T)
                toWrite <- replaceTags(regexp = NULL, toWrite, toProcess, 
                    perl = T, replaceType = "final")
                regexp <- paste("\[\[\s*foreach\s+(", paste(toProcess$iterators, 
                    collapse = "|"), ")\s*\]\]", sep = "")
                tagStarts <- grep(regexp, toWrite, perl = T)
                if (length(tagStarts) > 0) {
                    for (i in 1:length(tagStarts)) {
                      endregexp <- paste("\[\[\s*\/foreach\s*\]\]", 
                        sep = "")
                      tagEnd <- grep(endregexp, toWrite[-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 foreach tag")
                      }
                      toWrite <- c(toWrite[1:(tagStarts[i] - 1)], 
                        toWrite[(tagEnd + 1):length(toWrite)])
                    }
                }
                toWrite <- replaceTags(regexp = NULL, toWrite, toProcess, 
                    perl = T, replaceType = "final")
                filename <- replaceTags(regexp = NULL, toProcess$filename, 
                    toProcess, perl = T, replaceType = "final")
                print(paste("writing file: ", filename))
                curdir <- getwd()
                outputDir <- replaceTags(regexp = NULL, toProcess$outputDirectory, 
                    toProcess, perl = T, replaceType = "final")
                if (!file.exists(outputDir)) {
                    dir.create(outputDir, recursive = T)
                }
                setwd(outputDir)
                toWrite <- unlist(lapply(toWrite, function(line) {
                    if (nchar(line) > 90) {
                      strwrap(line, width = 85, exdent = 5)
                    }
                    else line
                }))
                writeLines(toWrite, con = filename, sep = "")
                setwd(curdir)
            }
        }
      }

    Run the code above in your browser using DataLab