closure <- function(..., frame=3) {Using this function you can affect variables outside of a function's scope. For instance:
parent <- parent.frame()
frame <- ifelse(frame=="sapply", 4, frame)
grandparent <- sys.frame(sys.nframe()-frame)
for(name in c(...))
assign(name, get(name, envir=parent), envir=grandparent)
}
x <- 0By calling closure() the variable x is set in the parent (actually, grandparent's) environment, meaning that by the time you reach the print statement x is 5. This is great for things like counters within lapply.
lapply(list(1,2,3,4,5), function(y) {
x <- x + 1
message("Run number ", x)
closure("x")
print(y);
})
print(x)
So, dude, like we should, like, totally just stop using money, and, like, start living on farms and like treating animals with respect. And everybody should have free healthcare, which we won't really need 'cause there won't be any McDonalds, and, like, college should be free.
Totally dude, down with capitalist pigs and up with real pigs, man, like they're smart animals, man. I mean, like, society is totally screwed up, like fuck it, dude.Of course, I then can only think of retorting in Mr. Lebowski style:
Sure! Fuck it! That's your answer! Tattoo it on your forehead! Your answer to everything! Your "revolution" is over, [smelly hippie]! Condolences! The bums lost! My advice is, do what your parents did! Get a job, sir! The bums will always lose-- do you hear me?!
CharlestonCulture.com Launch Party from Anthony Spencer on Vimeo.
]]>