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 8162803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:51:06+00:00 2026-06-06T18:51:06+00:00

In my .Rprofile I have the following two lines defined in my .First makeActiveBinding(.refresh,

  • 0

In my .Rprofile I have the following two lines defined in my .First

makeActiveBinding(".refresh", function() { system("R"); q("no") }, .GlobalEnv)
makeActiveBinding('.rm', function() {rm(list=ls(envir = .GlobalEnv),envir=.GlobalEnv); gc()}, .GlobalEnv)

They’re usually harmless, unless I type them by accident! The first makes a .refresh function that will quit and restart the R session. The second empties the global environment. However, when using the tables() function from data.table these two functions are run which isn’t exactly desirable.

For the moment, I’ve removed them from my .First but I’m curious if there is a way to avoid this. The offending lines in the tables() function are:

tt = objects(envir = env, all.names = TRUE)
ss = which(as.logical(sapply(tt, function(x) is.data.table(get(x, 
    envir = env)))))
  • 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-06-06T18:51:07+00:00Added an answer on June 6, 2026 at 6:51 pm

    I think you just discovered a downside to using active bindings in that way. Why don’t you instead create ordinary functions .rm and .refresh, that you call in the usual way (i.e. .rm() and .refresh()), and which won’t be executed upon simple inspection?

    Here’s what part of your .First might then look like:

    .First <- function() {
        assign(".rm", 
               function() {rm(list=ls(envir=.GlobalEnv), envir=.GlobalEnv)}, 
               pos = .GlobalEnv)
    }
    
    ## Try it out
    j <- 1:10
    ls()
    .First()
    .rm()
    ls()
    

    Edit, with solution:

    On further thought, this seems to work, only executing the core bits when .rm is ‘called’ directly. It works by inspecting the length of the call stack, and only running rm(...) if there is just one call in it (representing the current call to .rm(). If .rm is called/touched by a call to some other function (e.g. tables()), the call stack will be longer, and rm() won’t be executed.:

    makeActiveBinding('.rm', 
                     function() {
                         if(length(sys.calls())==1) {
                             rm(list=ls(envir = .GlobalEnv),envir=.GlobalEnv); gc()
                          }
                     },   
                     .GlobalEnv)
    
    ## Try _it_ out
    library(data.table)
    
    j <- 100
    .rm
    ls()
    
    j <- 100
    tables()
    ls()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have basically two questions. How do I locate the default Rprofile which is
.Rprofile (Based on these 2 posts. I had to remove .First<-function(){ } .) :
I noticed that this answer to a question about .Rprofile settings mentions setting two
I'd like to put the following in my .Rprofile: # auto width adjustment .adjustWidth
I am trying to use the function insertSource (new in R 2.12) to update
I'm trying to chase down what seems to be a conflict between function names
I wish to modify Rprofile.site file under Windows 7 R-2.11.1 using Notepad++ editor changing
I am using R, on linux. I have a set a functions that I
I have a Macintosh and I am trying to automatically load packages, homemade functions,
It's very convenient to have R scripts for doing simple plots from the command

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.