Reputation: 531
I have the following working code:
############################################
###Read in all the wac gzip files###########
###Add some calculated fields ###########
############################################
library(readr)
setwd("N:/Dropbox/_BonesFirst/65_GIS_Raw/LODES/")
directory<-("N:/Dropbox/_BonesFirst/65_GIS_Raw/LODES/")
to.readin <- as.list(list.files(pattern="2002.csv"))
LEHD2002<-lapply(to.readin, function(x) {
read.table(gzfile(x), header = TRUE, sep = ",", colClasses = "numeric", stringsAsFactors = FALSE)
})
But I would like to load the things from lapply into the global environment, for debugging reasons.
This provides a way to do so.
# Load data sets
lapply(filenames, load, .GlobalEnv)
But when I attempt to use it, I get the following error:
Error in FUN(X[[i]], ...) : bad restore file magic number (file may be corrupted) -- no data loaded In addition: Warning message: file ‘az_wac_S000_JT00_2004.csv.gz’ has magic number 'w_geo' Use of save versions prior to 2 is deprecated
Am I doing something wrong, or is 'load' deprecated or the like?
The gzfile(x) converts the .gz (zipped) file to a .csv so that shouldn't be an issue...
Upvotes: 0
Views: 2301
Reputation: 531
Quick and dirty way is to load it into the global environment, with <<- rather than <-
LEHD2002<<-lapply(to.readin, function(x)
LEHD2002<-lapply(to.readin, function(x)
attach() can also be used; but is touchier, and attaching multiple files makes a mess. (ie, make sure you detach() any files you attach().
Upvotes: 0
Reputation: 7610
load
loads files in a binary format (e.g., .rda
files). You're loading in files in a textual format, .csv
files. This is why you're using read.table
. When you try to read textual format files using load
, you will get that error.
The usage: lapply(filenames, load, .GlobalEnv)
, passes .GlobalEnv
to load
, not to lapply
. This is just a different way of reading in a list of files that are in a different format than yours. load
can put the objects in a different environment as a way to protect you from overwriting objects in your current environment with the same name as the objects you're loading. Binary objects created using save
(which you can load in with load
) carry their names with them. When you load them in, you do not assign them to a name. They are accessible in the environment you choose to load them into with their original name.
Both methods load the objects into .GlobalEnv
. So your code works the way you want it to. You can tell that your objects have not somehow been read into a different environment by trying to access them after you run the code. If you can access them using the object you named them with
Upvotes: 2