Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

r - rm(list=ls()) doesn't completely clear the workspace

This is a very minor issue, but I would like to understand exactly what is going on here.

Say I do the following:

library(RMySQL)
con <- dbConnect(MySQL(), host="some.server.us-east-1.rds.amazonaws.com",user="aUser", password="password", dbname="mydb")

values1 <- dbGetQuery(con,"select x,y from table1")
attach(values1)

At this point, I can do

rm(list=ls())

values2 <- dbGetQuery("select x,y from table1")
attach(values2)

but the attach gives me a warning about masking an x and y. I thought I had already clobbered those. What is going on? How do I completely clear a workspace?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

attach() does not make copies of x and y in your global environment, it attaches a dataframe to the search path.

From ?attach:

The database is not actually attached.  Rather, a new environment
 is created on the search path and the elements of a list
 (including columns of a data frame) or objects in a save file or
 an environment are _copied_ into the new environment.  If you use
 ‘<<-’ or ‘assign’ to assign to an attached database, you only
 alter the attached copy, not the original object.  (Normal
 assignment will place a modified version in the user's workspace:
 see the examples.)  For this reason ‘attach’ can lead to
 confusion.

For example:

> search()
[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"     
> a <- data.frame(stuff=rnorm(100))
> search()
[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"     
> attach(a)
> search()
 [1] ".GlobalEnv"        "a"                 "package:stats"    
 [4] "package:graphics"  "package:grDevices" "package:utils"    
 [7] "package:datasets"  "package:methods"   "Autoloads"        
[10] "package:base"     
> rm(list=ls())
> search()
 [1] ".GlobalEnv"        "a"                 "package:stats"    
 [4] "package:graphics"  "package:grDevices" "package:utils"    
 [7] "package:datasets"  "package:methods"   "Autoloads"        
[10] "package:base"     
> stuff
  [1] -0.91436377  0.67397624  0.62891651 -0.99669584  2.07692590 -0.62702302
  [...]
> detach(a)
> search()
[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...