87

I have to load a isfar.RData file to use it in other computation (which are not important to describe here). And I would like to simply see how looks data in this isfar.RData file e.g. what numbers, columns, rows it carries.

First I load my file:

isfar<-load("C:/Users/isfar.RData") 

When I try to obtain this information (I'm using Rcmdr) by ls() function or marking isfar at the beginning after loading I get in the output window: [1] "isfar" instead of the table. Why?

Thanks a lot, I appreciate all of the answers! Hope it's comprehensible what I wrote, Im not a native speaker.

1
  • 1
    If you would like to save/load a single R object, then look at the alternative approach using the complementary functions saveRDS() and readRDS.
    – Matt L.
    Commented Sep 20, 2017 at 16:47

10 Answers 10

94

I think the problem is that you load isfar data.frame but you overwrite it by value returned by load.

Try either:

load("C:/Users/isfar.RData") 
head(isfar)

Or more general way

load("C:/Users/isfar.RData", ex <- new.env())
ls.str(ex) 
2
  • your more general approach works well! However, can you please more explain why using ex<- new.env()? Does not this creates a new environment? My aim is to include .RData within the project in GitLab and than allow everyone to access the data. But, would not you approach create a new environment instead? Thanks!
    – maycca
    Commented Jan 17, 2020 at 10:08
  • 2
    @maycca Yes, it creates new environment, so it does not overwrite anything. If you want to share the data then better use saveRDS, readRDS (or from readr package write_rds, read_rds). Then instead of load you assign them to an object (like read.csv): x <- readRDS("some_file.rds")
    – Marek
    Commented Jan 17, 2020 at 11:11
45

you can try

isfar <- get(load('c:/users/isfar.Rdata'))

this will assign the variable in isfar.Rdata to isfar . After this assignment, you can use str(isfar) or ls(isfar) or head(isfar) to get a rough look of the isfar.

3
  • 1
    get(load('isfar.Rdata')) seems to create the variable even without the assignment isfar <- bit
    – Harley
    Commented Jul 26, 2021 at 4:15
  • get(load('xxx.Rdata')) will retrieve the variable stored inside the Rdata file, and the variable name is the same name when you saved the Rdata file,e.g.,save(yyy,file = xxx.Rdata'), the variable name might not be the same as the filename. I suggest using the assignment because you may get lost when there is too many variables inside your workspace.
    – siaosing
    Commented Aug 2, 2021 at 4:44
  • 1
    you end up with two variables with the same contents if you assign it to something with a different name and this adds to having too many variables inside your workspace
    – Harley
    Commented Aug 3, 2021 at 5:11
10

Look at the help page for load. What load returns is the names of the objects created, so you can look at the contents of isfar to see what objects were created. The fact that nothing else is showing up with ls() would indicate that maybe there was nothing stored in your file.

Also note that load will overwrite anything in your global environment that has the same name as something in the file being loaded when used with default behavior. If you mainly want to examine what is in the file, and possibly use something from that file along with other objects in your global environment then it may be better to use the attach function or create a new environment (new.env) and load the file into that environment using the envir argument to load.

9

This may fit better as a comment but I don't have enough reputation, so I put it here.
It worth mentioning that the load() function will retain the object name that was originally saved no matter how you name the .Rdata file.

Please check the name of the data.frame object used in the save() function. If you were using RStudio, you could check the upper right panel, Global Environment-Data, to find the name of the data you load.

6

If you have a lot of variables in your Rdata file and don't want them to clutter your global environment, create a new environment and load all of the data to this new environment.

load(file.path("C:/Users/isfar.RData"), isfar_env <- new.env() )

# Access individual variables in the RData file using '$' operator
isfar_env$var_name 

# List all of the variable names in RData:
ls(isfar_env)
2

You can also import the data via the "Import Dataset" tab in RStudio, under "global environment." Use the text data option in the drop down list and select your .RData file from the folder. Once the import is complete, it will display the data in the console. Hope this helps.

1

It sounds like the only varaible stored in the .RData file was one named isfar.

Are you really sure that you saved the table? The command should have been:

save(the_table, file = "isfar.RData")

There are many ways to examine a variable.

Type it's name at the command prompt to see it printed. Then look at str, ls.str, summary, View and unclass.

1
  • I'm not exactly sure what was saved in that file, I assume it was a table. But even if it was one variable saved, how can I see how it looks (numbers)?
    – Ewa
    Commented Sep 1, 2011 at 12:59
1

You don't seem to need to assign it to a variable. That bit magically happens. In fact, assigning it to a variable might mean you end up with two variables with the same data.

get(load('C:/Users/isfar.Rdata'))

Or if it's in the same folder as your R code...

get(load('isfar.Rdata'))
0
isfar<-load("C:/Users/isfar.RData") 
if(is.data.frame(isfar)){
   names(isfar)
}

If isfar is a dataframe, this will print out the names of its columns.

0
0
num <- seq(0, 5, length.out=10) #create object num
num
[1] 0.00 1.25 2.50 3.75 5.00
save(num, file = 'num.RData') #save num ro RData
rm(num) #remove num 
load("num.RData") #load num from RData
num
[1] 0.00 1.25 2.50 3.75 5.00

> isfar<-load("num.RData")
> typeof(isfar)
 [1] "character"
> isfar  #list objects saved in RData
 [1] "num"

Not the answer you're looking for? Browse other questions tagged or ask your own question.