onGiantShoulders onGiantShoulders - 1 month ago 5x
R Question

Writing to a file inside an apply function

For memory reasons, I do not want to save the output of an apply function to a file, but I would like to write the results to a file while the apply loop is running.

I've made the following working example:

checkfun <- function(TheArray, fileConn){
write(as.character(TheArray[1]), fileConn)
random_data <- matrix(nrow=10, ncol=10, data=runif(100))
outfile <- file("checkout.txt")
apply(random_data, 2, checkfun, fileConn=outfile)

Reading the file
will only contain the value (
) of the last column of the
matrix. Whereas I want it to output the full first row of that matrix.

Is there something I'm doing wrong?

Just to be sure, the function in my original apply loop takes relatively long to finish, so I don't think writing to the file will produce too much overhead, even though it would produce quite some overhead in the function here.



change 1 line of your code to open the file for writing in text mode:

outfile <- file("checkout.txt", "w")