The data.table tables () function performs some of my .Rprofile functions

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 are usually harmless unless I accidentally type them! The first makes a function .refreshthat will stop and restart the R session. The second empties the global environment. However, when using a function tables()from data.tablethese two functions are performed, which is not entirely desirable.

At the moment, I removed them from .Firstmine, but I'm curious if there is a way to avoid this. Offensive lines in a function tables():

tt = objects(envir = env, all.names = TRUE)
ss = which(as.logical(sapply(tt, function(x) is.data.table(get(x, 
    envir = env)))))
+5
source share
1 answer

, . .rm .refresh, (.. .rm() .refresh()) ?

.First :

.First <- function() {
    assign(".rm", 
           function() {rm(list=ls(envir=.GlobalEnv), envir=.GlobalEnv)}, 
           pos = .GlobalEnv)
}

## Try it out
j <- 1:10
ls()
.First()
.rm()
ls()

, :

, , , , .rm "" . , rm(...), ( .rm(). .rm / - (, tables()), , rm() .:

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()
+8

All Articles