samedi 9 mai 2020

Running "Source" in R Script Resulting in "no loop for break/next, jumping to top level" Error?

I am trying to figure how why a certain part of my code is giving me an error when I try to use the "Source" and "Source with Echo" instead of highlighting the entire code and running it.

Code Section that is causing problems:

# Function to move columns in data frame to "before" or "after" assigned column----
if (exists("moveme")) {
  next
} else {
  moveme = function(data, tomove, where = "last", ba = NULL) {
    temp = setdiff(names(data), tomove)
    x = switch(
      where,
      first = data[c(tomove, temp)],
      last = data[c(temp, tomove)],
      before = {
        if (is.null(ba)) stop("must specify ba column")
        if (length(ba) > 1) stop("ba must be a single character string")
        data[append(temp, values = tomove, after = (match(ba, temp)-1))]
      },
      after = {
        if (is.null(ba)) stop("must specify ba column")
        if (length(ba) > 1) stop("ba must be a single character string")
        data[append(temp, values = tomove, after = (match(ba, temp)))]
      })
    x
  }
}
# Function to correctly capitalize state names----
if (exists("simpleCap")) {
  next
} else {
  simpleCap <- function(x) {
    s <- strsplit(x, " ")[[1]]
    paste(toupper(substring(s, 1,1)), substring(s, 2),
          sep="", collapse=" ")
  }
}

Which returns the error:

Error in eval(ei, envir) : no loop for break/next, jumping to top level

I have tried to rewrite the function, but still get this error.

Aucun commentaire:

Enregistrer un commentaire