33

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?

stevejb
  • 2,414
  • 5
  • 26
  • 41

7 Answers7

29

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"    
Vince
  • 7,608
  • 3
  • 41
  • 46
  • 2
    Is there a way to clear clear the environment created by attach(a) after making changes to it? If I attach(a), make changes to a column using the column name as a reference, then detach(a) followed by attach(a) doesn't erase those changes. – kaisquared May 29 '18 at 14:23
9

Just to mention... if you, perhaps, have hidden objects in your environment, like .First and .Last functions, you can remove them with rm(list = ls(all.names = TRUE)). But in your case, use detach(objectname) to remove object from search path. detach() will remove any object in position #2, since .GlobalEnv cannot be removed (and base too). With detach() you can unload previously loaded packages, so be careful (though you can always load them with library(packagename)).

aL3xa
  • 35,415
  • 18
  • 79
  • 112
3

R itself says, in the help for rm:

## remove (almost) everything in the working environment.
## You will get no warning, so don't do this unless you are really sure.
rm(list = ls())

Note the 'almost'. There are different environments.

Did you try detach(values1)?

Borealid
  • 95,191
  • 9
  • 106
  • 122
2

Most likely you have at least one other data frame or list attached with x and y columns/components. You can use the "conflicts" function to see all objects that have potential conflicts and use the "find" function to find out where those objects are (the "search" function also helps by showing all the things you have attached).

Your question is a good example of why attaching data frames is being frowned upon, it is better to use functions like "with" or "within" so that you don't have problems from forgetting to detach objects.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
2

To use detach() function to remove a specific package from Work Space, it requires the full description for the package.

Example: Remove a package ISwR Answer 1: detach(ISwR) .... Not Work! The Package is still on the Work Space. Answer 2: detach(package:ISwR) .... WORKS! The Package is removed from the Work Space.

Taeko
  • 21
  • 1
0

if you are working with RStudio, you can just go to Session->Clear Workspace... To clear all variables from your workspace

Stefanos
  • 909
  • 12
  • 19
-1

You can use:

rm(list=ls())

or:

rm(list=(ls()))

but save your workspace before you close R.

OhBeWise
  • 5,350
  • 3
  • 32
  • 60
Andy
  • 19