I have an R script that runs a set of functions on multiple datasets and creates a set of concentration-response models. What I would like to do is save out the model parameters and other results to a csv file.
My plan is to have the script create an empty csv file and then as the script progresses, results are appended to that csv file. Every time the script is run I would like the results to be overwritten.
I've tried to create an empty file using
system("copy /y NUL results.csv > NUL")
to create the file, but the file is not created. This command (i.e.,
copy/y NUL results.csv > NUL) works properly when run directly in the Windows terminal.
Am I missing something simple? System is Windows XP.
Is there anything wrong with
file.create(), which is portable across operating systems?
file.create("my.csv") #  TRUE
You can then append to the file, e.g. using the
append=TRUE argument to
write.table(), perhaps like this:
df <- data.frame(a=1:4, b=4:1) write.table(df, file="my.csv", sep=",", row.names=FALSE, append=TRUE) write.table(df, file="my.csv", sep=",", row.names=FALSE, col.names=FALSE, append=TRUE)
EDIT If you will be doing a ton of writes to each file, it can save considerable time to open a connection to the file once, and only close it when you're done. If that is not the case, the approach above works just fine.
Because it is shell command you should use shell instead of system.
shell("copy /y NUL results.csv > NUL")
EDIT. More portable solution:
how about just
echo > results.csv
f <- file("my.csv", open="w") truncate(f)