如何使用lapply将文件加载到全局环境中?

时间:2021-04-06 23:20:42

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.

但出于调试原因,我想将lapply中的内容加载到全局环境中。

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

FUN错误(X [[i]],...):错误的恢复文件幻数(文件可能已损坏) - 没有加载数据另外:警告消息:文件'az_wac_S000_JT00_2004.csv.gz'有幻数' w_geo'不推荐使用2之前的保存版本

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...

gzfile(x)将.gz(压缩)文件转换为.csv,这应该不是问题...

2 个解决方案

#1


2  

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.

加载以二进制格式加载文件(例如,.rda文件)。您正在以文本格式加载文件,.csv文件。这就是你使用read.table的原因。当您尝试使用load读取文本格式文件时,您将收到该错误。

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.

用法:lapply(filenames,load,.GlobalEnv),传递.GlobalEnv加载,而不是lapply。这只是一种不同的读取文件列表的方式,这些文件格式与您的格式不同。 load可以将对象放在不同的环境中,以防止您使用与正在加载的对象相同的名称覆盖当前环境中的对象。使用save创建的二进制对象(您可以使用load加载)随身携带它们的名称。加载它们时,不要将它们分配给名称。它们可以在您选择以其原始名称加载到的环境中访问。

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

两种方法都将对象加载到.GlobalEnv中。所以你的代码按照你想要的方式工作。您可以通过在运行代码后尝试访问它们来判断您的对象未以某种方式被读入不同的环境。如果您可以使用您为其命名的对象来访问它们

#2


0  

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().

attach()也可以使用;但是更加敏感,附加多个文件会让人感到困惑。 (即,确保你分离()你附加的任何文件()。

#1


2  

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.

加载以二进制格式加载文件(例如,.rda文件)。您正在以文本格式加载文件,.csv文件。这就是你使用read.table的原因。当您尝试使用load读取文本格式文件时,您将收到该错误。

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.

用法:lapply(filenames,load,.GlobalEnv),传递.GlobalEnv加载,而不是lapply。这只是一种不同的读取文件列表的方式,这些文件格式与您的格式不同。 load可以将对象放在不同的环境中,以防止您使用与正在加载的对象相同的名称覆盖当前环境中的对象。使用save创建的二进制对象(您可以使用load加载)随身携带它们的名称。加载它们时,不要将它们分配给名称。它们可以在您选择以其原始名称加载到的环境中访问。

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

两种方法都将对象加载到.GlobalEnv中。所以你的代码按照你想要的方式工作。您可以通过在运行代码后尝试访问它们来判断您的对象未以某种方式被读入不同的环境。如果您可以使用您为其命名的对象来访问它们

#2


0  

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().

attach()也可以使用;但是更加敏感,附加多个文件会让人感到困惑。 (即,确保你分离()你附加的任何文件()。