Printing Output on the Console in R

Printing Output on the Console in R

  • R
  • 3 mins read

In this article, we will study the procedure of printing output on the console screen in the R programming language. 

R contains various in-built methods used to display outputs of programs on the display screen. The console output is visible whenever any user presses the “Run” button to visualize the manipulations done by the code. 

Output methods in R

There are a large number of methods available for printing outputs on the console screen : 

print() method 

The print() method is used to take in parameters, either a variable or a value to be displayed. The variable and the value can belong to any data type

#declaring variables
vara = 2
varb = 4
varc = 9

#printing the value of variable
print(vara)

#printing the sum of all variables
print(vara+varb+varc)
The output produced on the console is as follows : 
[1] 2
[1] 15

Therefore, the print() method can also be used to perform arithmetic operations or type coercions in R. Instead of assigning the output of any function or equation to a variable, the result can also be directly displayed using the print method. 

paste() method 

The print() method doesn’t work in case of multiple output printing simultaneously. In the case of the print() method, string concatenation during output display is not possible. However, in the paste() method, the comma-separated values or variables can be specified, which are then concatenated using a “ “ character. The result of this method is also a string that contains all the concatenated parameters of the method. The method can be used to concatenate different variables belonging to different data types. 

paste(var1, var2.. varn)
#declaring variables
vara = 2
varb = 4
varc = 9

#printing multiple values on console 
paste(vara,varb,varc)
The code produces the following output : 
[1] "2 4 9"

paste0() method

The paste0 method is used to print the components specified as arguments in the method using a “” separator. The parameters to be combined may belong to different data types. 

paste0(var1, var2.. varn)
#declaring variables
varb = "Fox"
varc = "Info"
vard = "tech"
vare = 2

#printing multiple values on console 
paste0(varb,varc,vard,vare)
The code produces the following output : 
[1] "FoxInfotech2"

message() method

The message() method in R is generally used to display a diagnostic text to the user. It can be used to print a single string, or concatenate and print data elements together. 

message(var, string)

where the var is an optional parameter. The var may belong to any data type. There may be one or more variables both before or after the string. The output is displayed in the form of red-colored text. 

#declaring variables
varb = "Fox"
varc = "Info"
vard = "tech"
vare = 2

#printing multiple values on console 
message(vare,varb,"Hi",varc)
The output is : 
2FoxHiInfo

cat() method

The cat() method is similar in usage to the paste() method where the parameters are concatenated and printed together each separated by a “ “.

#declaring variables
varb = "Fox"
varc = "Info"
vard = "tech"
vare = 2

#printing multiple values on console 
cat(“Hi”,vare,varb,varc,vard)
The output produced is as follows : 
Hi 2 Fox Info tech
sprintf() method

The sprintf() method in R is used to generate formatted output on the console. The output of this method is a character vector containing a mixture of variables and text. This method finds its usage in a large number of applications, such as scientific notation computation or manipulating the decimal places of any number. 

#declaring a number
num <- 32.13

#print the data up to 6 decimal points.
sprintf("%.6f", num)

#print the data in scientific notation
sprintf("%E", num)
Output:
[1] "32.130000"
[1] "3.213000E+01"