I have a few convenience functions in my .Rprofile, such as this handy function for returning the size of objects in memory. Sometimes I like to clean out my workspace without restarting and I do this with rm(list=ls()) which deletes all my user created objects AND my custom functions. I’d really like to not blow up my custom functions.
One way around this seems to be creating a package with my custom functions so that my functions end up in their own namespace. That’s not particularly hard, but is there an easier way to ensure custom functions don’t get killed by rm()?
Combine
attachandsys.sourceto source into an environment and attach that environment. Here I have two functions in filemy_fun.R:Before I load these functions, they are obviously not found:
Create an environment and source the file into it:
Still not visible as we haven’t attached anything
and when we do so, they are visible, and because we have attached a copy of the environment to the search path the functions survive being
rm()-ed:I still think you would be better off with your own personal package, but the above might suffice in the meantime. Just remember the copy on the search path is just that, a copy. If the functions are fairly stable and you’re not editing them then the above might be useful but it is probably more hassle than it is worth if you are developing the functions and modifying them.
A second option is to just name them all
.foorather thanfooasls()will not return objects named like that unless argumentall = TRUEis set: