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.
- 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))
}
- 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
Intelligent Point Label Placement in R
How to Replace Na Values in a Table For Selected Columns
Ggplot2 Two-Line Label With Expression
Dplyr Mutate Rowsums Calculations or Custom Functions
Create a Data.Frame Where a Column Is a List
Increment by 1 For Every Change in Column
Remove an Entire Column from a Data.Frame in R
Why Does X[Y] Join of Data.Tables Not Allow a Full Outer Join, or a Left Join
All Levels of a Factor in a Model Matrix in R
How to Convert Long to Wide Format With Counts
Yaml Current Date in Rmarkdown
Basic Lag in R Vector/Dataframe
Replace Missing Values With Column Mean