Write Lines of Text to TXT File in R (3 Examples)

 

This article explains how to export text lines to an external TXT file in the R programming language.

The content of the tutorial is structured as follows:

Let’s start right away.

 

Creation of Exemplifying Data

Have a look at the following example data:

text_lines <- c("This is my first line of text.",      # Create example data
                "Another line of text.",
                "What a beautiful sentence created in R!")
text_lines                                             # Print example data
# [1] "This is my first line of text." 
# [2] "Another line of text."
# [3] "What a beautiful sentence created in R!"

The previously shown output of the RStudio console illustrates the structure of our example data – We have created a vector of character strings containing different lines of text.

Let’s write these lines of text to a TXT file!

 

Example 1: Write Lines of Text to TXT File Using writeLines() Function

Example 1 illustrates how to save our text lines to a TXT file using the writeLines function in R.

Within the writeLines function we have to specify the data object containing our lines of text (i.e. text_lines) and the name of the TXT file we want to create:

writeLines(text_lines, "my_file1.txt")                 # Apply writeLines function

After running the previous R code, you should find a text file in your current working directory that looks as in the screenshot below:

 

TXT file exported from R

 

Example 2: Write Lines of Text to TXT File Using sink() & cat() Functions

In Example 2, I’ll illustrate how to apply the sink and cat functions to create a new text file line by line.

Have a look at the following R code:

sink("my_file2.txt")                                   # Apply sink & cat functions
cat(text_lines[1])
cat("\n")
cat(text_lines[2])
cat("\n")
cat(text_lines[3])
cat("\n")
sink()

After running this R syntax, you should find another TXT file called my_file2 in your current working directory that contains exactly the same data as the first file that we have exported in Example 1.

 

Example 3: Write Lines of Text to TXT File Using cat() Function & append Argument

As a third alternative, I’ll explain in this example how to use the cat function in combination with the append argument to write text lines to a text file.

As you can see in the following R code, we first have to initialize our file without specifying the append argument.

Afterwards, we can add our text line by line using the append argument:

cat(text_lines[1], file = "my_file3.txt", sep = "\n")  # Apply cat & append
cat(text_lines[2], file = "my_file3.txt", sep = "\n", append = TRUE)
cat(text_lines[3], file = "my_file3.txt", sep = "\n", append = TRUE)

At this point, you should find another text file called my_file3 containing the same sentences as in the previous examples.

The advantage of the previously shown code is that you can also add text lines at a later point.

 

Video, Further Resources & Summary

Do you want to know more about exporting TXT files? Then I can recommend having a look at the following video of my YouTube channel. I show the R programming codes of this article in the video:

 

The YouTube video will be added soon.

 

In addition to the video, you could have a look at the related tutorials that I have published on my website.

 

In summary: In this R programming tutorial you have learned how to write text to TXT files. In case you have any further questions or comments, let me know in the comments below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


2 Comments. Leave new

  • Buena tu pagina, pero la cantidad de propaganda que tiene, apareciendo una y otra vez, videos que se inician a cada rato… hacen imposible leer el contenido, este se desplaza, desparece, etc, etc,,

    Reply
    • Hey,

      Thank you for the comment! I don’t speak Spanish, but based on Google Translate I assume that you complain about the number of ads on this website?

      Please note that ads are (currently) the only way I monetize this website. All the content is freely available and this is only possible due to the ads on the site.

      However, in case you feel too annoyed by the ads, feel free to use an ad blocker or to visit my website over private mode.

      Regards

      Joachim

      Reply

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top