Q:

Write lines of text to a file in R

In the R scripting language, how do I write lines of text, e.g. the following two lines
Hello
World

to a file named "output.txt"?

A:

fileConn<-file("output.txt")
writeLines(c("Hello","World"), fileConn)
close(fileConn)

A:

Actually you can do it with sink():
sink("outfile.txt")
cat("hello")
cat("\n")
cat("world")
sink()

hence do:
file.show("outfile.txt")
# hello
# world

A:

I would use the cat() command as in this example:
> cat("Hello",file="outfile.txt",sep="\n")
> cat("World",file="outfile.txt",append=TRUE)

You can then view the results from with R with
> file.show("outfile.txt")
hello
world

