8

I am trying to load an .rda file in r which was a saved dataframe. I do not remember the name of it though.

I have tried

a<-load("al.rda")

which then does not let me do anything with a. I get the error

Error:object 'a' not found

I have also tried to use the = sign.

How do I load this .rda file so I can use it?

I restared R with load("al.rda) and I know get the following error

Error: C stack usage is too close to the limit
megv
  • 1,421
  • 5
  • 24
  • 36
  • 1
    just do `load("al.rda")` and do `ls()` to check what R objects were loaded in the global environment. – Ramnath Dec 17 '11 at 21:09
  • I get at the end closing unused connection 3 (al.rda). what should I be looking for here? – megv Dec 17 '11 at 21:13
  • 1
    @megv - A couple of more things to try: 1. Install an older version of R and try to load it there - it might be that the file is old and newer R versions have problems with it. 2. Try loading it on Linux. – Tommy Dec 19 '11 at 20:52

5 Answers5

5

I had a similar issue, and it was solved without reinstall R. for example doing

load("al.rda) works fine, however if you do a <- load("al.rda") will not work.

Samuel Shamiri
  • 121
  • 1
  • 4
5

Use 'attach' and then 'ls' with a name argument. Something like:

attach("al.rda")
ls("file:al.rda")

The data file is now on your search path in position 2, most likely. Do:

search()
ls(pos=2)

for enlightenment. Typing the name of any object saved in al.rda will now get it, unless you have something in search path position 1, but R will probably warn you with some message about a thing masking another thing if there is.

However I now suspect you've saved nothing in your RData file. Two reasons:

  1. You say you don't get an error message
  2. load says there's nothing loaded

I can duplicate this situation. If you do save(file="foo.RData") then you'll get an empty RData file - what you probably meant to do was save.image(file="foo.RData") which saves all your objects.

How big is this .rda file of yours? If its under 100 bytes (my empty RData files are 42 bytes long) then I suspect that's what's happened.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
5

I had to reinstall R...somehow it was corrupt. The simple command which I expected of

load("al.rda")

finally worked.

megv
  • 1,421
  • 5
  • 24
  • 36
4

The load function does return the list of variables that it loaded. I suspect you actually get an error when you load "al.rda". What exactly does R output when you load?

Example of how it should work:

d <- data.frame(a=11:13, b=letters[1:3])
save(d, file='foo.rda')
a <- load('foo.rda')
a # prints "d"

Just to be sure, check that the load function you actually call is the original one:

find("load") # should print "package:base"

EDIT Since you now get an error when you load the file, it is probably corrupt in some way. Try this and say what it prints:

file.info("a1.rda") # Prints the file size etc...
readBin("a1.rda", "raw", 50) # reads first 50 bytes from the file

Without having access to the file, it's hard to investigate more... Maybe you could share the file somehow (http://www.filedropper.com or similar)?

Tommy
  • 39,997
  • 12
  • 90
  • 85
  • guessing at possible problems, `getwd()` and `list.files()` (or `load(file.choose())` are some possible helpful tools for figuring out why the file might not be loading (if it's not); *and* making sure that the file is really called `al.rda` and not `a1.rda` ... – Ben Bolker Dec 17 '11 at 22:09
  • This is the command I use> l1<-load("al.rda") > l1 Error: object 'l1' not found In addition: Warning message: closing unused connection 3 (al.rda) – megv Dec 18 '11 at 02:44
  • @megv - what did `find("load")` print? – Tommy Dec 18 '11 at 02:58
1

I usually use save to save only a single object, and I then use the following utility method to retrieve that object into a given variable name using load, but into a temporary namespace to avoid overwriting existing objects. Maybe it will be helpful for others as well:

load_first_object <- function(fname){
    e <- new.env(parent = parent.frame())
    load(fname, e)
    return(e[[ls(e)[1]]])
}

The method can of course be extended to also return named objects and lists of objects, but this simple version is for me the most useful.