R identify workspace objects used in source -
is there way identify workspace objects created, modified or referenced in sourced script? have hundreds of randomly-named objects in workplace , 'cleaning house' - able more proactive in future , rm() heck out of sourced script @ end.
the simplest way store environment objects in list before sourcing, sourcing, comparing new environment objects old list.
here pseudo-code.
old_objects <- ls() source(file) new_objects <- setdiff(ls(), c(old_objects, "old_objects"))
this identify created objects. identify whether object modified, don't see solution store objects in list beforehand , running identical afterwards.
# rm(list = ls(all = true)) <- 1 b <- 1 old_obj_names <- ls() old_objects <- lapply(old_obj_names, get) names(old_objects) <- old_obj_names # source should go here <- 2 c <- 3 # add "old_obj_names" , "old_objects" in setdiff these # created after call ls before source new_objects <- setdiff(ls(), c(old_obj_names, "old_obj_names", "old_objects")) modified_objects <- sapply(old_obj_names, function(x) !identical(old_objects[[x]], get(x)), use.names = true) modified_objects <- names(modified_objects[modified_objects])
new_objects
indeed "c" , modified_objects
indeed "a" in example. obviously, work, need ensure neither old_objects
nor old_obj_names
in way created or modified in sourced file!
Comments
Post a Comment