Jeff Hammerbacher
Jeff Hammerbacher

Reputation: 4236

Does R have a string-like object that exposes a file API?

It's sometimes convenient to write a string to an in-memory object and then pass that object to functions in place of a file handle. In Python, there's StringIO. Does R have something equivalent?

Upvotes: 1

Views: 256

Answers (1)

hrbrmstr
hrbrmstr

Reputation: 78792

Something like:

con <- textConnection("the_thing_i_am_writing_to", "w")

cat("Some text without a newline", file = con, append = TRUE)
cat("more text with two newlines\n\n", file = con, append = TRUE)

writeLines(rownames(mtcars), con = con)

cat("this is after the car names", file = con, append = TRUE)

close(con)

str(the_thing_i_am_writing_to)
## chr [1:35] "Some text without a newlinemore text with two newlines" "" "Mazda RX4" "Mazda RX4 Wag" ...

(the_thing_i_am_writing_to gets created in the environment for you)

Upvotes: 4

Related Questions