class - How can I remove all custom methods and classes from an R workspace? -
i've been experimenting lot s4 classes lately, , pain restart r in order clear class definitions , custom methods workspace. rm(list=ls(all.names=true))
of no use. manually remove classes , methods individually writing lines one-by-one, i'm sure there's got easier way.
an example showcasing problem:
.myclass <- setclass("myclass", representation=representation(myslot="numeric")) myslot <- function(x) x@myslot setmethod("[", signature=c("myclass", "numeric", "missing"), function(x, i, j, ...) { initialize(x, myslot=myslot(x)[i]) })
try remove rm()
:
rm(list=ls(all.names=true))
however, class definition , custom method still present:
> x <- new("myclass", myslot=1:4) > x[1] error in x[1] : not find function "myslot"
since myslot()
object removed rm
, method referencing myslot()
remained. i'd know how remove all classes , all custom methods in 1 fell swoop.
it's hard know you're hoping r remember of session. can
removeclass("myclass", where=.globalenv) removemethods("[", where=.globalenv)
or if you've lost track of what-all you've done following hacks might help
## class definitions prefixed '.__c__' mangled <- grep(".__c__", ls(all=true, envir=.globalenv), value=true) classes <- sub(".__c__", "", mangled) (cl in classes) removeclass(cl, where=.globalenv) ## methods tables prefixed '.__t__' mangled <- grep(".__t__", ls(all=true, envir=.globalenv), value=true) methods <- unique(sub(".__t__(.*):.*", "\\1", mangled)) (meth in methods) removemethods(meth, where=.globalenv)
Comments
Post a Comment