Table of Contents
Saving R output to a text file allows you to preserve the results of your code for future reference or sharing with others. To do so, you can use the “sink” function in R, which directs the output to a specified file instead of the console. Alternatively, you can use the “write” function to manually write the output to a text file. Examples of using these methods include saving the summary statistics of a dataset or the results of a statistical analysis to a text file. This allows for easy organization and accessibility of R output for future use.
Save R Output to Text File (With Examples)
There are two common ways to save R output to a text file:
Method 1: Use the sink() Function
#define file name
sink("my_data.txt")
#write this string to file
"here is some text"
#close the external connection
sink() Method 2: Use the cat() Function
#write string to file
cat("here is some text", file = "my_data.txt")The following examples show how to use each method in practice.
Example 1: Save R Output to Text File Using sink()
We can use the following sink() function to export a character string to a text file:
#define file name
sink("my_data.txt")
#write this string to file
"here is some text"
#close the external connection
sink()
We can then navigate to the and open the text file:

The file contains the string that we specified.
We can also use the cat() function to export something more complex like a data frame to a text file:
#define file name
sink("my_data.txt")
#define data frame to write to file
df <- data.frame(player=c('A', 'B', 'C', 'D','E'),
points=c(12, 29, 24, 30, 19),
assists=c(5, 5, 7, 4, 10))
print(df)
#close the external connection
sink()
We can then navigate to the current working directory and open the text file:

The file contains the data frame that we created.
Example 2: Save R Output to Text File Using cat()
We can use the following cat() function to save a string to a text file:
#save string to text file
cat("here is some text", file = "my_data.txt")
We can then navigate to the current working directory and open the text file:

The text file contains the string that we specified.
Notice that the cat() function doesn’t include any line numbers from the R console in the text file, unlike the sink() function.
Feel free to use whichever function suits your needs.
Cite this article
stats writer (2024). How can I save R output to a text file, and what are some examples of how to do so?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-save-r-output-to-a-text-file-and-what-are-some-examples-of-how-to-do-so/
stats writer. "How can I save R output to a text file, and what are some examples of how to do so?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-save-r-output-to-a-text-file-and-what-are-some-examples-of-how-to-do-so/.
stats writer. "How can I save R output to a text file, and what are some examples of how to do so?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-save-r-output-to-a-text-file-and-what-are-some-examples-of-how-to-do-so/.
stats writer (2024) 'How can I save R output to a text file, and what are some examples of how to do so?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-save-r-output-to-a-text-file-and-what-are-some-examples-of-how-to-do-so/.
[1] stats writer, "How can I save R output to a text file, and what are some examples of how to do so?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I save R output to a text file, and what are some examples of how to do so?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
