Sometimes when working with data, you want to format it with a line break when printing it out. This often makes the data text easier to read. Fortunately, R programming provides a way of indicating a line break along with functions that will produce the printout with line breaks.
Description
There are two functions that will produce line breaks within R programming. One is the writeLines function and it has the format of writeLines(text). The other one is the cat function which has the format of cat(text). Both functions require the line break indicator “\n” to work. When they are supplied with a text that has this indicator it will produce a line break where the indicator is located. These are simple functions with only a single argument.
Explanation
Both the writeLines and the cat functions go through the indicated text looking for the line break indicator of “\n”. When they find this indicator, they produce a line break in the text that they print out where the indicator is located. Without this indicator in your text, you do not get a line break. However, when you have the line break indicator in your text, these functions produce as many line breaks as you indicate. If done right your text will be properly formatted.
Examples
Here are four code examples related to doing a line break in R programming. The first two show what does not work, and the second two show what actually works.
> x = “This is a library example! \n This first example does not work.”
> x
[1] “This is a library example! \n This first example does not work.”
This is the simplest example of printing out the contents of a variable and it does not produce a line break.
> x = “This is a library example! \n This second example does not work.”
> print(x)
[1] “This is a library example! \n This second example does not work.”
In this example, we use the print function, but it does not produce a line break, but it does print the line break indicator.
> x = “This is a library example! \n This third example works fine.”
> writeLines(x)
This is a library example!
This third example works fine.
In this example, we use the writeLines function. It produces a line break where the linebreak indicator is found. If you look at the text you will see the line break indicator denoted by “\n” and that is where the line break occurs.
> x = “This is a library example! \n This fourth example works perfectly.”
> cat(x)
This is a library example!
This fourth example works perfectly.
In this example, we use the cat function to produce a line break with the line break indicator is located in our text. This is the second of the two methods that actually print the line brakes.
Application
The main application of this function and the line break indicator is the process of formatting data text to be printed out on the screen or printer. The process helps with providing properly formatted text for presenting the data you are trying to display. A properly formatted text makes it easier to read and understand, consequently, it helps you to provide easier-to-understand data. This is because line breaks can help separate thoughts that would otherwise blur together.
When you are trying to format data for printout, being able to use line breaks makes the process easier. Adding a line break indicator to your text and using the proper functions is the way to accomplish this task. This makes it an excellent tool in your programming toolbox.