read_RDS_or_RDA: Read a single object from an RDS or RDA file

View source: R/read_RDS_or_RDA.R

read_RDS_or_RDAR Documentation

Read a single object from an RDS or RDA file

Description

This is like [readRDS()] except that it can also read from an RData file containing a single object (i.e. the kind of file that is read using [load()]). Use this in place of 'readRDS()' if you want to be slightly more forgiving about what kind of R data file you accept.

Usage

read_RDS_or_RDA(filename, expected.class = "ANY")

Arguments

filename

The file to read an R object from.

expected.class

If specified, the object will be coerced into this class using 'as()', which will throw an error as normal if the coercion is not possible. This allows you to restrict what kind of objects you will accept. This can also be a function that accepts a single argument and performs the proper coercion itself.

Value

The object read from the file, possibly after coercing it into another class.

See Also

[readRDS()], [read_single_object_from_rda()], [as()]

Examples


tmpf <- tempfile()
saveRDS(1:10, tmpf)
read_RDS_or_RDA(tmpf)
read_RDS_or_RDA(tmpf, "character")
# Using a function instead of a class name.
read_RDS_or_RDA(tmpf, as.character)
read_RDS_or_RDA(tmpf, "factor")
read_RDS_or_RDA(tmpf, "data.frame")

## Not run: 
# This will throw an error because the coercion to "lm" is not
# possible.
read_RDS_or_RDA(tmpf, "lm")

## End(Not run)


hhilbig/haschaR documentation built on Dec. 26, 2024, 5:40 a.m.