Imagine you have a file
sink(\"example.txt\")
data.frame(a = runif(10), b = runif(10), c = runif(10))
sink()
and would want to add some hea
in R there is no need to work with an extra file. You can just do :
writeLines(c(header,readLines(File)),File)
Yet, using the linux shell seems the most optimal solution, as R is not famous for performant file reading and writing. Especially not since you have to read in the complete file first.
Example :
Lines <- c(
"First line",
"Second line",
"Third line")
File <- "test.txt"
header <- "A line \nAnother line \nMore line \n\n"
writeLines(Lines,File)
readLines(File)
writeLines(c(header,readLines(File)),File)
readLines(File)
unlink(File)