Printing Output of an R Program

22 Mar, 2022
In R there are various methods to print the output. Most common method to print output in R program, there is a function called print() is used. Also if the program of R is written over the console line by line then the output is printed normally, no need to use any function for print that output. To do this just select the output variable and press run button. Example: 
R
# select 'x' and then press 'run' button# it will print 'GeeksforGeeks' on the consolex <- "GeeksforGeeks"x
Output:
[1] "GeeksforGeeks"
Using print() function to print output is the most common method in R. Implementation of this method is very simple.
Syntax: print(“any string”) or, print(variable)
Example: 
R
# R program to illustrate# printing output of an R program # print stringprint("GFG") # print variable# it will print 'GeeksforGeeks' on the consolex <- "GeeksforGeeks"print(x)
Output:
[1] "GFG"
[1] "GeeksforGeeks"
R provides a method paste() to print output with string and variable together. This method defined inside the print() function. paste() converts its arguments to character strings. One can also use paste0() method.
Note: The difference between paste() and paste0() is that the argument sep by default is ” “(paste) and “”(paste0).
Syntax: print(paste(“any string”, variable)) or, print(paste0(variable, “any string”))
Example: 
R
# R program to illustrate# printing output of an R program x <- "GeeksforGeeks" # using paste inside print()print(paste(x, "is best (paste inside print())")) # using paste0 inside print()print(paste0(x, "is best (paste0 inside print())"))
Output:
[1] "GeeksforGeeks is best (paste inside print())"
[1] "GeeksforGeeksis best (paste0 inside print())"
sprintf() is basically a C library function. This function is use to print string as C language. This is working as a wrapper function to print values and strings together like C language. This function returns a character vector containing a formatted combination of string and variable to be printed.
Syntax: sprintf(“any string %d”, variable) or, sprintf(“any string %s”, variable) or, sprintf(“any string %f”, variable)) etc.
Example: 
R
# R program to illustrate# printing output of an R program x = "GeeksforGeeks" # stringx1 = 255            # integerx2 = 23.14          # float # string printsprintf("%s is best", x) # integer printsprintf("%d is integer", x1) # float printsprintf("%f is float", x2)
Output:
> sprintf("%s is best", x)
[1] "GeeksforGeeks is best"
> sprintf("%d is integer", x1)
[1] "255 is integer"
> sprintf("%f is float", x2)
[1] "23.140000 is float"
Another way to print output in R is using of cat() function. It’s same as print() function. cat() converts its arguments to character strings. This is useful for printing output in user defined functions.
Syntax: cat(“any string”) or, cat(“any string”, variable)
Example: 
R
# R program to illustrate# printing output of an R program # print string with variable# "\n" for new linex = "GeeksforGeeks"cat(x, "is best\n") # print normal stringcat("This is R language")
Output:
GeeksforGeeks is best
This is R language
Another way to print something in R by using message() function. This is not used for print output but its use for showing simple diagnostic messages which are no warnings or errors in the program. But it can be used for normal uses for printing output.
Syntax: message(“any string”) or, message(“any string”, variable)
Example: 
R
# R program to illustrate# printing output of an R program x = "GeeksforGeeks"# print string with variablemessage(x, "is best") # print normal stringmessage("This is R language")
Output:
GeeksforGeeks is best
This is R language 
To print or write a file with a value of a variable there is a function called write(). This function is used a option called table to write a file.
Syntax: write.table(variable, file = “file1.txt”) or, write.table(“any string”, file = “file1.txt”)
Example: 
R
# R program to illustrate# printing output of an R program x = "GeeksforGeeks"# write variablewrite.table(x, file = "my_data1.txt") # write normal stringwrite.table("GFG is best", file = "my_data2.txt")
Output: 
simmytarika5
Picked
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change column name of a given DataFrame in R
Filter data by multiple conditions in R using Dplyr
How to Replace specific values in column in R DataFrame ?
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Loops in R (for, while, repeat)
Adding elements in a vector in R programming - append() method
Group by function in R using Dplyr
How to change Row Names of DataFrame in R ?
Convert Factor to Numeric and Numeric to Factor in R Programming