Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3803404
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:21:28+00:00 2026-05-19T14:21:28+00:00

I have a few convenience functions in my .Rprofile, such as this handy function

  • 0

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()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:21:29+00:00Added an answer on May 19, 2026 at 2:21 pm

    Combine attach and sys.source to source into an environment and attach that environment. Here I have two functions in file my_fun.R:

    foo <- function(x) {
        mean(x)
    }
    
    bar <- function(x) {
        sd(x)
    }
    

    Before I load these functions, they are obviously not found:

    > foo(1:10)
    Error: could not find function "foo"
    > bar(1:10)
    Error: could not find function "bar"
    

    Create an environment and source the file into it:

    > myEnv <- new.env()
    > sys.source("my_fun.R", envir = myEnv)
    

    Still not visible as we haven’t attached anything

    > foo(1:10)
    Error: could not find function "foo"
    > bar(1:10)
    Error: could not find function "bar"
    

    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:

    > attach(myEnv)
    > foo(1:10)
    [1] 5.5
    > bar(1:10)
    [1] 3.027650
    > rm(list = ls())
    > foo(1:10)
    [1] 5.5
    

    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 .foo rather than foo as ls() will not return objects named like that unless argument all = TRUE is set:

    > .foo <- function(x) mean(x)
    > ls()
    character(0)
    > ls(all = TRUE)
    [1] ".foo"         ".Random.seed"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have few text fields like SSN , phone number and email id. I
I have few question in this regard When you create an internet page, does
We have few components like libraries dlls When initially created I ran the following
This should be a simple one, basically I have a few paths drawn with
I have a few Python packages that I would like to tidy up and
I have few questions about ssis transction isolation levels. consider a scenario:I have an
i have few confusion regarding ORM(Object Relational Mapping) i am thinking about to use
I have few basic questions on session management in GWTP. Now, we create a
I have few controls (Label, Custom Textbox, Datagridview) docked on a form. When I
I have few basic questions on session management in GWTP. Client : GWTP, Server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.