hiding personal functions in R

后端 未结 7 1486
别那么骄傲
别那么骄傲 2020-12-13 00:40

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 witho

7条回答
  •  心在旅途
    2020-12-13 01:02

    Here are two ways:

    1) Have each of your function names start with a dot., e.g. .f instead of f. ls will not list such functions unless you use ls(all.names = TRUE) therefore they won't be passed to your rm command.

    or,

    2) Put this in your .Rprofile

    attach(list(
       f = function(x) x, 
       g = function(x) x*x
    ), name = "MyFunctions")
    

    The functions will appear as a component named "MyFunctions" on your search list rather than in your workspace and they will be accessible almost the same as if they were in your workspace. search() will display your search list and ls("MyFunctions") will list the names of the functions you attached. Since they are not in your workspace the rm command you normally use won't remove them. If you do wish to remove them use detach("MyFunctions") .

提交回复
热议问题