martin gregory
2024-Oct-27 12:13 UTC
[R] readLines on open connection reads only first write on MacOS
I was using readLines to read data from a file which is being written to by another process. readLines documentation says "If the connection is open it is read from its current position". With R 4.4.1 on Linux 5.15.160 this is true but does not seem to be the case as far as R 4.4.1 on MacOS 12.7.6 (Intel) is concerned. Here, the first write to the file is read correctly but subsequent reads return nothing. The minimal program below shows the behaviour and produces the following results: Linux: input 1: lines written/read: 4 / 4 input 2: lines written/read: 3 / 3 input 3: lines written/read: 2 / 2 MacOS: input 1: lines written/read: 4 / 4 input 2: lines written/read: 3 / 0 input 3: lines written/read: 2 / 0 I have searched NEWS and found only https://bugs.r-project.org/show_bug.cgi?id=18555, but I am not specifying an encoding so not sure whether it is relevant or not. I also tried with a 1 second delay between flush and read, but this had no effect. I have found an alternative, scan with skip of the number lines already read, and this works on both Linux and MacOS. But I would still like to know how to have readLines work with open connections on MacOS. Regards, Martin ## Program to demonstrate the behaviour ## input data rL <- list(paste0("Line ", 1:4), paste0("Line ", 1:3), paste0("Line ", 1:2)) ## create an empty file and open write and read connections to the file close(file("rL.log",open="w")) rLconn.w <- file("rL.log", open="a") rLconn.r <- file("rL.log", open="r") ## write the test data and read it for (i in 1:3) { writeLines(rL[[i]], rLconn.w) flush(rLconn.w) out <- readLines(rLconn.r, warn=FALSE) writeLines(c(paste0("input ", i, ": lines", " written/read: ", length(rL[[i]]), " / ", length(out)))) } close(rLconn.w) close(rLconn.r)
Apparently Analagous Threads
- readLines() segfaults on large file & question on how to work around
- readLines() segfaults on large file & question on how to work around
- readLines() segfaults on large file & question on how to work around
- readLines() segfaults on large file & question on how to work around
- Typos in writeLines.Rd, readLines.Rd, and data.matrix.Rd