12

Is there any method in R to find out what data-sets have been attached. In my work flow i use the console and build a script. I try out the lines of code in console and once i am satisfied with the results, i add them to a script so that I can reproduce the results later. For past week i have been playing with a few data-sets. I think I attached and detached a number of them over time. But now I need to know what data-sets are current attached, so that I can detach them.

Sam
  • 6,812
  • 15
  • 40
  • 59

4 Answers4

17

I guess you are searching for the search() command. This should show the attached dataframes and packages you have included.

also type help(search) and check what it is doing.

zipizip
  • 289
  • 2
  • 5
12

Use search() to find out which objects are attached.

Since this will also tell you about all packages that are attached, you can use a regular expression to remove the packages from the search results:

Attach mtcars:

attach(mtcars)
The following object(s) are masked from 'package:ggplot2':

    mpg

Now use search() and a regexp:

attached <- search()
attached[!grepl("package", attached)]
[1] ".GlobalEnv"    "mtcars"        "tools:rstudio" "Autoloads" 
Andrie
  • 163,419
  • 39
  • 422
  • 472
6

To bring the answer in line with the suggestions in the comments, I am separating the code into three parts. The first simply produces a list of attached datasets. The second removes a single instance of attached datasets. The third handles situations where a dataset has been attached multiple times:

1. Output List of Attached Datasets

This code will produce a list of attached datasets for the user to examine:

intersect(search(), objects())

if you get "character(0)" - there are no data objects attached in the global env

2. Automatically "Cleanup" & Detach All Attached Datasets

This code will take that list of attached dataset and detach all attached objects (datasets). It should leave non-objects alone. So, it is a fairly simple, safe, and reliable way to "clean-up" the Global Environment:

Note: this will only work if each object is attached only once, otherwise you will have to run the command again. The next portion of the answer handles this case also.

#works if only attached each object once
lapply(X = intersect(search(), objects()), 
FUN = function(X){detach(name = X, character.only = TRUE)})

3. Detach All Even When Objects Are Attached Multiple Times

3(a) Multiple attachments This code will detach all attached objects (datasets) even if they have been attached multiple times. The above operation cannot handle this because R will not include the name of the object twice, even if it has been attached twice by the user.

So:

attach(data.df)
intersect(search(), objects())

And,

attach(data.df)
attach(data.df)
attach(data.df)
intersect(search(), objects())

Will produce the same output: [1] "data.df"

Even though the latter case needs detach(data.df) to be called three times to clear the Global Environment. As a result, we need to put the answer from Part 2 in a loop.

3(b) Produce output for user In addition, the clean-up will output a list of dataset names for each successful "detach()" operation. This lets the user know which datasets were detached and how many times.

Note: I am sure this could be cleaned up a little, but this piece of code gets the job done reliably.

# the repeat handles cases where objects of identical name were 
# attached (or, object was attached twice by mistake). 
# It also outputs the name of objects as they are detached. 

repeat{
  x <- lapply(X = intersect(search(), objects()),
         FUN = function(X){detach(name = X, character.only = TRUE)})

  y <- lapply(x, function(X){cat(attr(X,"name"), "\n")})

  if(identical(x, list())){break}
}
HoneyBuddha
  • 596
  • 5
  • 13
  • Please don't add the same answer to multiple questions. Answer the best one and flag the rest as duplicates. See http://meta.stackexchange.com/questions/104227/is-it-acceptable-to-add-a-duplicate-answer-to-several-questions – Bhargav Rao Sep 27 '16 at 18:05
  • Furthermore, It doesn't look like you've answered the post accurately. The question is about finding a list of attached datasets. But you're going one step further and detaching. – Bhargav Rao Sep 27 '16 at 18:18
  • You can join us in the room if you wanna discuss more about this http://chat.stackoverflow.com/rooms/25312/r-public – Bhargav Rao Sep 27 '16 at 18:31
  • I was trying to respond to the intent of the question as captured by the last few lines in the question: _"I attached and detached a number of [data-sets] over time. But now I need to know what data-sets are current attached, **so that I can detach them.**"_ The code does exactly that - it removes attached datasets (it does so without needing manual input). It produces a list of removals. And, it handles cases where datasets have been attached multiple times. I suppose I can split the code into two parts (1) one that "narrowly" targets the question (2) a part that targets the question's intent – HoneyBuddha Sep 28 '16 at 22:53
  • In response to informative feedback by Matthew Lundberg and Bhargav Rao, I have updated the answer to: (1) First, answer the "narrow" question: how to produce a list of attached datasets. (2) Then, I include code that I believe targets the intent of the question: a clean-up operation i.e. how to systematically remove all attached datasets after a period of data exploration. (3) In the final portion, I allow the code to handle the specific case mentioned by the question (i.e. multiple attachments of the same dataset). This code also prints out the name of the detached datasets to inform user. – HoneyBuddha Sep 28 '16 at 23:40
4

To avoid this problem it is better to not use attach and detach for datasets. You can also run into the problem of having the same variable defined in 2 or more attached datasets (and possibly the global environment) and accidentally getting the wrong one. It is better to use the data argument to functions like plot and lm and use functions like with, within, and transform for other cases. Then R will always look first in the specified dataset and not leave it attached afterwards.

Greg Snow
  • 45,559
  • 4
  • 73
  • 98