Get Specific Object from Rdata File

Get specific object from Rdata file

.RData files don't have an index (the contents are serialized as one big pairlist). You could hack a way to go through the pairlist and assign only entries you like, but it's not easy since you can't do it at the R level.

However, you can simply convert the .RData file into a lazy-load database which serializes each entry separately and creates an index. The nice thing is that the loading will be on-demand:

# convert .RData -> .rdb/.rdx
e = local({load("New.RData"); environment()})
tools:::makeLazyLoadDB(e, "New")

Loading the DB then only loads the index but not the contents. The contents are loaded as they are used:

lazyLoad("New")
ls()
x # if you had x in the New.RData it will be fetched now from New.rdb

Just like with load() you can specify an environment to load into so you don't need to pollute the global workspace etc.

Programmatically extract an object from collection of RData files

We found a useful solution.

  1. An extractor function
 extractorRData <- function(file, object) {
#' Function for extracting an object from a .RData file created by R's save() command
#' Inputs: RData file, object name
E <- new.env()
load(file=file, envir=E)
return(get(object, envir=E, inherits=F))
}

  1. Extract a data frame "allParams" from RData file "priorRun.RData" without loading the entire environment.
      allParams.prior <- extractorRData("priorRun.RData", "allParams")

This approach has proven to be both fast, and flexible. Useful with large data frames that would be slow to reconstruct.

Extracting specific data from an RData file

after putting the rdata file in your working directory, do something like

load("C:/Users/1_adm.RData", ex <- new.env())
ls.str(ex)

The rdata loads variable into your workspace so that command will show you what the variable names are, so you can then use them.

I don't have the file you're talking about so I can't tell you how to access the specific fields you need.

edit

Based on your comment:
Ok so the object you load is an object of type SpatialPolygonsDataFrame, and that has a Slot/property called Polygons, with objects of type Polygon. Those polygons I assume are the lat lng coordinates around the country. Accessing the coordinates should look something like

ps <- gadm@polygons[1]   

then something like

ps@coords  

How to read .rdata file in R

Just semove the df <- in your code.

load() will load all the objects contained in the .RData file. This can be multiple variables and dataframes with various names, so you do not need to specify one object name.

How can I find specific object(data) in mutiple .RData by linux(ubuntu)?

Maybe try this in R console. This will give you all the available object whose names starts with "max".

files <- dir()
rdata_files <- files[grepl(".RData", files)]
rdata_files

for (fname in rdata_files) {
obj_names <- load(fname)
cat(fname, "\n")
print(obj_names[grepl("^max", obj_names)])
}

How can I load an object into a variable name that I specify from an R data file?

If you're just saving a single object, don't use an .Rdata file, use an .RDS file:

x <- 5
saveRDS(x, "x.rds")
y <- readRDS("x.rds")
all.equal(x, y)


Related Topics



Leave a reply



Submit