ReadAssign: Read in a list of files, and assign them to variables based...

Description Usage Arguments Details Note Author(s) Examples

View source: R/ReadAssign.R

Description

Read an Rds file and assign the value to the global environment with a the name implied from the filename

Usage

1
ReadAssign(x, assignGlobal = TRUE)

Arguments

x

file name, and source of the future object name

assignGlobal

Assign the object to the global space or return invisibly? Default is TRUE, which assigns the object globally.

Details

This function will open a serialized R object and assign the object to the name implied by the object. For example a matrix saved as "Results/Yhat867.Rds" will be loaded and put into a matrix named "Yhat867" if you call ReadAssign("Results/Yhat867.Rds").

This function is particularly valuable when you use file names that match the object names you would use for the saved objects. For example, imagine that in a folder "results" you had 1,000 .Rds files named Vec0001.Rds, Vec0002.Rds ... up to Vec1000.Rds, and imagine that each file had one vector of length 60. If you wanted to load Vec200.Rds to Vec299.Rds into memory you could get get the file names using list.files: MyFiles <- list.files('results/', full.names=T, pattern = "vec2[0-9][0-9].Rds"). Then you could load those vectors into variables in the global enviornment using ReadAssign: sapply(MyFiles, ReadAssign) Since these happen to be the same length, you could also put them all into a matrix using this syntax: MyMatrix <- sapply(MyFiles, ReadAssign, assignGlobal = F)

This pattern is useful when you 1) Use file names that match your object names 2) Have many files / objects and want to relaod all / some of them at different times.

Note

2014-04-01 SJR Updated to take a path and filename

2014-05-05 GWL Adding function to geneorama package

2014-05-05 GWL Adding $ to gsub pattern argument

Author(s)

Gene Leynes and Scott Rodgers

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
		require(geneorama)
	##----------------------------------------------------------------------
	## Create example data, save it, then load it from disk using ReadAssign
	##----------------------------------------------------------------------
		set.seed(27)
		## Generate Data
		MyExampleData <- data.frame(x=letters, y=rnorm(length(letters)))
		saveRDS(MyExampleData, file="MyExampleData.Rds")
		rm(MyExampleData)
		## MyExampleData" is no longer in the environment
		ls()
		## Now reload "MyExampleData"
		ReadAssign("MyExampleData.Rds")
		ls()
		## Clean up
		unlink("MyExampleData.Rds")
		rm(MyExampleData)


		## Not run: 
	##----------------------------------------------------------------------
		## Example of making 600 files, and reloading 100 of them
	##----------------------------------------------------------------------
	mat <- matrix(rnorm(60000), 100, 600)
     for(i in 1:ncol(mat)){
         fp <-  file.path("ReadAssignExampleData",
                          paste0("vec", sprintf("%03.f", i),".Rds"))
         saveRDS(object = mat[,i],
                 file = fp)
     }
     mat_recovered <- sapply(list.files('ReadAssignExampleData/',
                                        full.names=T,
                                        pattern = "vec3[0-9][0-9].Rds"),
                             ReadAssign, assignGlobal = F)
		head(mat[,300:310])
		head(mat_recovered[,1:11])
		## Clean Up
		unlink("ReadAssignExampleData")
		rm(mat, mat_recovered)
		
## End(Not run)

geneorama/geneorama documentation built on Oct. 17, 2020, 12:35 a.m.