append to a file in r

To append data to an existing file in R, you need to open the file in "append" mode using the file() function with the parameter open = "a". Then, you can use the cat() function to write text or data to the file.

main.r
# open a file in append mode
file_conn <- file("path/to/file.txt", open = "a")

# append text to the file
cat("New data to append.\n", file = file_conn)

# always remember to close the file connection
close(file_conn)
218 chars
9 lines

In the example above, we open the file "path/to/file.txt" in append mode and create a file connection named file_conn. Then, we use the cat() function to append the text "New data to append." to the file. The "\n" at the end of the string is important to add a newline character.

Finally, we close the file connection using the close() function to save the changes to the file.

related categories

gistlibby LogSnag