readRDS(base)
readRDS()所属R语言包:base
Serialization Interface for Single Objects
单对象的序列化接口
译者:生物统计家园网 机器人LoveR
描述----------Description----------
Functions to write a single R object to a file, and to restore it.
功能,编写一个R对象到一个文件,并恢复它。
用法----------Usage----------
saveRDS(object, file = "", ascii = FALSE, version = NULL,
compress = TRUE, refhook = NULL)
readRDS(file, refhook = NULL)
参数----------Arguments----------
参数:object
R object to serialize.
R对象进行序列化。
参数:file
a connection or the name of the file where the R object is saved to or read from.
连接或R对象被保存或读取的文件名。
参数:ascii
a logical. If TRUE, an ASCII representation is written; otherwise (default except for text-mode connections), a binary one is used. See the comments in the help for save.
一个逻辑。如果TRUE,写入ASCII表示,否则(默认情况下,除了文本模式连接),使用一个二进制。请参阅在save帮助的意见。
参数:version
the workspace format version to use. NULL specifies the current default version (2). Versions prior to 2 are not supported, so this will only be relevant when there are later versions.
使用工作区的格式版本。 NULL指定当前默认的版本(2)。 2之前的版本不支持,所以这只会是相关的,当有更高版本。
参数:compress
a logical specifying whether saving to a named file is to use "gzip" compression, or one of "gzip", "bzip2" or "xz" to indicate the type of compression to be used. Ignored if file is a connection.
逻辑指定是否保存到指定的文件是使用"gzip"压缩,或"gzip","bzip2"或"xz"指示要使用的压缩类型。如果file是连接忽略。
参数:refhook
a hook function for handling reference objects.
一个钩子函数处理参考对象。
Details
详情----------Details----------
These functions provide the means to save a single R object to a connection (typically a file) and to restore the object, quite possibly under a different name. This differs from save and load, which save and restore one or more named objects into an environment. They are widely used by R itself, for example to store metadata for a package and to store the help.search databases: the ".rds" file extension is most often used.
这些功能提供了手段保存一个单一的R对象连接(通常是文件),并很可能在一个不同的名称来恢复对象。这不同于save和load,保存和恢复环境的一个或多个命名对象。它们被广泛用于R本身,例如包存储的元数据和存储help.search数据库:".rds"文件扩展名是最常用的。
Functions serialize and unserialize provide a slightly lower-level interface to serialization: objects serialized to a connection by serialize can be read back by readRDS and conversely.
功能serialize和unserialize提供一个稍微低级别接口序列化:对象的序列化serializereadRDS相反可以读取连接。
All of these interfaces use the same serialization format, which has been used since R 1.4.0 (but extended from time to time as new object types have been added to R). However, save writes a single line header (typically "RDXs\n") before the serialization of a single object (a pairlist of all the objects to be saved).
这些接口都使用相同的序列化格式,由于R 1.4.0已使用(但不时延长已添加新的对象类型为R)。然而,save写一个单一的行头(通常"RDXs\n")前一个单一对象的序列化(所有要保存的对象pairlist)。
Compression is handled by the connection opened when file is a file name, so is only possible when file is a connection if handled by the connection. So e.g. url connections will needed to be wrapped in a call to gzcon.
压缩处理连接,打开当file是一个文件名,所以只能当file是一个连接,如果连接处理。因此,如url连接将需要被包裹在调用gzcon。
If a connection is supplied it will be opened (in binary mode) for the duration of the function if not already open: if it is already open it must be in binary mode for saveRDS(ascii = FALSE) (the default).
如果提供连接,它会被打开(二进制模式)功能的持续时间,如果尚未打开:如果它已经打开,它必须是二进制模式saveRDS(ascii = FALSE)(默认)。
值----------Value----------
For readRDS, an R object.
readRDS,R对象。
For saveRDS, NULL invisibly.
saveRDS,NULL无形。
参见----------See Also----------
serialize, save and load.
serialize,save和load。
The "R Internals" manual for details of the format used.
“R内部手册所用的格式的详细信息。
举例----------Examples----------
## save a single object to file[#保存一个对象文件]
saveRDS(women, "women.rds")
## restore it under a different name[根据不同的名称#恢复]
women2 <- readRDS("women.rds")
identical(women, women2)
## or examine the object via a connection, which will be opened as needed.[#或研究对象,通过连接,根据需要,将开放。]
con <- gzfile("women.rds")
str(readRDS(con))
close(con)
## Less convenient ways to restore the object[#不太方便的方法来恢复对象]
## which demonstrate compatibility with unserialize()[#这表明与反序列化(兼容)]
con <- gzfile("women.rds", "rb")
identical(unserialize(con), women)
close(con)
con <- gzfile("women.rds", "rb")
wm <- readBin(con, "raw", n = 1e4) # size is a guess[大小是一个猜测]
close(con)
identical(unserialize(wm), women)
## Format compatibility with serialize():[#序列化格式的兼容性():]
con <- file("women2", "w")
serialize(women, con) # ASCII, uncompressed[ASCII码,解压缩]
close(con)
identical(women, readRDS("women2"))
con <- bzfile("women3", "w")
serialize(women, con) # binary, bzip2-compressed[bzip2压缩的二进制文件,]
close(con)
identical(women, readRDS("women2"))
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|