R-Enthusiast
R-Enthusiast

Reputation: 350

Leaving connections open indefinitely using file() in R

I have this function to write streaming data from Twitter into one file for 12 hours, then to another file for 12 hours. This is so we can clean, parse, and store the data twice a day.

conn <- file(description = "after12.json", open = "a")
conn2 <- file(description = "before12.json", open = "a")
write.tweets <- function(x) {
  if (nchar(x) > 0 && format(Sys.time(), " %H") >= 12){
    writeLines(x, conn, sep = "")
  } else {
    writeLines(x, conn2, sep = "")
  }
}

This is within a much larger function to pull and write the data. My question is pretty simple. I want to leave both connections open indefinitely to be able to call the connection after 12 hours of inactivity. Is there a way I can do this?

Upvotes: 1

Views: 108

Answers (1)

Ricardo Saporta
Ricardo Saporta

Reputation: 55350

use open

conn <- file(description = "after12.json")
open(conn, open = "a")

as per ?open:

open opens a connection. In general functions using connections will open them if they are not open, but then close them again, so to leave a connection open call open explicitly.

Upvotes: 2

Related Questions