如何使用lapply将文件加载到全局环境中? [英] How do I use lapply to load files into the global environment?

查看:64
本文介绍了如何使用lapply将文件加载到全局环境中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下工作代码:

############################################
###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)
})

但是出于调试的原因,我想将这些东西从lapply加载到全局环境中.

But I would like to load the things from lapply into the global environment, for debugging reasons.

但是当我尝试使用它时,出现以下错误:

But when I attempt to use it, I get the following error:

FUN(X [[i]],...)中的错误:恢复文件幻数错误(文件可能是已损坏)-未加载任何数据此外:警告消息:文件"az_wac_S000_JT00_2004.csv.gz"的魔术编号为"w_geo",使用保存不建议使用2之前的版本

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

我做错什么了吗?还是不赞成使用"load"?

Am I doing something wrong, or is 'load' deprecated or the like?

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

The gzfile(x) converts the .gz (zipped) file to a .csv so that shouldn't be an issue...

推荐答案

load 以二进制格式加载文件(例如 .rda 文件).您正在以文本格式加载文件,即 .csv 文件.这就是为什么您使用 read.table 的原因.当您尝试使用 load 读取文本格式文件时,会出现该错误.

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.

用法: lapply(文件名,加载,.GlobalEnv),将 .GlobalEnv 传递给 load ,而不传递给 lapply .这只是读取格式与文件格式不同的文件列表的另一种方式. load 可以将对象放置在不同的环境中,以防止您覆盖当前环境中与正在加载的对象同名的对象.使用 save (您可以通过 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.

这两个方法都将对象加载到 .GlobalEnv 中.因此,您的代码可以按您希望的方式工作.您可以通过在运行代码后尝试访问对象来表明您的对象没有以某种方式被读取到其他环境中.如果您可以使用为它们命名的对象访问它们

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

这篇关于如何使用lapply将文件加载到全局环境中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆