Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 596197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:08:45+00:00 2026-05-13T16:08:45+00:00

I have a process in R that creates a bunch of objects, serializes them,

  • 0

I have a process in R that creates a bunch of objects, serializes them, and puts them into plain text files. This seemed like a really good way to handle things since I am working with Hadoop and all output needs to stream through stdin and stdout.

The problem I am left with is how to read these objects out of the text file and back into R on my desktop machine. Here’s a working example that illustrates the challenge:

Let’s create a tmp file and write a single object into it. This object is just a vector:

outCon <- file("c:/tmp", "w")
mychars <- rawToChar(serialize(1:10, NULL, ascii=T))
cat(mychars, file=outCon)
close(outCon)

The mychars object looks like this:

> mychars
[1] "A\n2\n133633\n131840\n13\n10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"

when written to the text file it looks like this:

A
2
133633
131840
13
10
1
2
3
4
5
6
7
8
9
10

I’m probably overlooking something terribly obvious, but how do I read this file back into R and unserialize the object? When I try scan() or readLines() both want to treat the new line characters as record delimiters and I end up with a vector where each element is a row from the text file. What I really want is a text string with the whole contents of the file. Then I can unserialize the string.

Perl will read line breaks back into a string, but I can’t figure out how to override the way R treats line breaks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:08:45+00:00Added an answer on May 13, 2026 at 4:08 pm

    JD, we do that in the digest package via serialize() to/from raw. That is nice as you can store serialized objects in SQL and other places. I would actually store this as RData as well which is way quicker to load() (no parsing!) and save().

    Or, if it has to be RawToChar() and ascii then use something like this (taken straight from help(digest) where we compare serialization of the file COPYING:

     # test 'length' parameter and file input
     fname <- file.path(R.home(),"COPYING")
     x <- readChar(fname, file.info(fname)$size) # read file
     for (alg in c("sha1", "md5", "crc32")) {
       # partial file
       h1 <- digest(x    , length=18000, algo=alg, serialize=FALSE)
       h2 <- digest(fname, length=18000, algo=alg, serialize=FALSE, file=TRUE)
       h3 <- digest( substr(x,1,18000) , algo=alg, serialize=FALSE)
       stopifnot( identical(h1,h2), identical(h1,h3) )
       # whole file
       h1 <- digest(x    , algo=alg, serialize=FALSE)
       h2 <- digest(fname, algo=alg, serialize=FALSE, file=TRUE)
       stopifnot( identical(h1,h2) )
     }
    

    so with that your example becomes this:

    R> outCon <- file("/tmp/jd.txt", "w")
    R> mychars <- rawToChar(serialize(1:10, NULL, ascii=T))
    R> cat(mychars, file=outCon); close(outCon)
    R> fname <- "/tmp/jd.txt"
    R> readChar(fname, file.info(fname)$size)
    [1] "A\n2\n133633\n131840\n13\n10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"
    R> unserialize(charToRaw(readChar(fname, file.info(fname)$size)))
    [1]  1  2  3  4  5  6  7  8  9 10
    R> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a process in Perl that creates another one with the system command
I have a process that is going to create a piece of STATIC html
I am in the process of designing a web interface that would support 2
Overview I am working on building a Kynetx ruleset that will find a bunch
This is a long question that I do not know how to summarize... I
Good Morning, I have created my first generic method pieced together from a bunch
I am trying to create a Timer/TimerTask that will run the same day of
I certainly don't know how to title this question, sorry. I'm having some problems
Maybe there's no simple answer to this question, but I ask in case someone
I am trying to get crash dump debugging working with 2010, but it keeps

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.