[R] saveRDS() and readRDS() Why? [solved, kind of]

Robert David Burbidge robertburbidged@t@ @ending from y@hoo@co@uk
Thu Nov 8 13:51:23 CET 2018


Apologies, unserialize takes a connection, not a file, so you would need 
something like:

# linux (not run)
f <- file("rawData.rds", open="r")
rawData <- unserialize(f)
close(f)

The help file states that readRDS will read a file created by serialize 
(saveRDS is a wrapper for serialize).

It appears that the problem was "byte-shuffling at both ends when 
transferring data from one little-endian machine to another" and was 
worked around by using xdr = FALSE. So, this wouldn't necessarily work 
when transferring between big-endian and little-endian machines.

On 08/11/18 07:27, Patrick Connolly wrote:
> Many thanks to Berwin, Eric, Robert, and Jan for their input.
>
> I had hoped it was as simple as because I typed
>
> saveRDS("rawData", file = "rawData.rds") on the Windows side.
> but that wasn't the case.
>
> Robert Burbridge suggested:
>
>   windows (not run)
> f <- file("rawData.rds", open="w")
> serialize(rawData, f, xdr = FALSE)
> close(f)
>
> # linux
> rawData <- unserialize(file = "rawData.rds")
>
> That didn't work:
> Error in unserialize(file = "rawData.rds") :
>    unused argument (file = "rawData.rds")
> (the argument isn't 'file')
>
> Nor did
>> rawData <- unserialize("rawData.rds")
> Error in unserialize("rawData.rds") :
>    character vectors are no longer accepted by unserialize()
>
> However
>
> readRDS(file = "rawData.rds") did!
>
> So what I needed was serialize but not unserialize.
>
> I still don't know Why, but I know How.



More information about the R-help mailing list