The Wayback Machine - https://web.archive.org/web/20240828110754/https://www.geeksforgeeks.org/printing-output-of-an-r-program/
Open In App

Printing Output of an R Program

Last Updated : 22 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 console
x <- "GeeksforGeeks"
x


Output:

[1] "GeeksforGeeks"

output-of-R-program

Print output using print() function

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 string
print("GFG")
 
# print variable
# it will print 'GeeksforGeeks' on the console
x <- "GeeksforGeeks"
print(x)


Output:

[1] "GFG"
[1] "GeeksforGeeks"

Print output using paste() function inside print() function

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())"

Print output using sprintf() function

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" # string
x1 = 255            # integer
x2 = 23.14          # float
 
# string print
sprintf("%s is best", x)
 
# integer print
sprintf("%d is integer", x1)
 
# float print
sprintf("%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"

Print output using cat() function

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 line
x = "GeeksforGeeks"
cat(x, "is best\n")
 
# print normal string
cat("This is R language")


Output:

GeeksforGeeks is best
This is R language

Print output using message() function

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 variable
message(x, "is best")
 
# print normal string
message("This is R language")


Output:

GeeksforGeeks is best
This is R language 

Write output to a file

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 variable
write.table(x, file = "my_data1.txt")
 
# write normal string
write.table("GFG is best", file = "my_data2.txt")


Output: output-of-R-program



Previous Article
Next Article

Similar Reads

Printing out to the Screen or to a File in R Programming - cat() Function
cat() function in R Language is used to print out to the screen or to a file. Syntax: cat(..., file = "", sep = " ", fill = FALSE, labels = NULL, append = FALSE)Parameters: ...: atomic vectors, names, NULL and objects with no output file: the file in which printing will be done sep: specified separator fill: If fill=TRUE, a new line will be printed
2 min read
Printing plots generated in a function using R Markdown in RStudio
In this article, we will explore how to generate and print the plots created within the function using R Markdown in RStudio. R Markdown is a powerful tool for creating dynamic documents that integrate code, results, and narrative text. We will demonstrate how to create the plot within the R function and then print that plot in an R Markdown docume
3 min read
Suppress Output of Command in R
In this article, we are going to discuss how to suppress the output of a command in R programming language. Suppressing means making the output invisible. We can make the output should not appear. By using invisible() function we can suppress the output. Syntax: invisible(variable_name) Where variable_name is the name of the created variable Exampl
1 min read
How to Change the Displayed Column Names in Flextable Output in R
Flextable is a powerful R package designed for creating and customizing tables in a variety of formats, including Word and PowerPoint documents. It provides a range of functions to manipulate table appearance and structure, making it a great tool for reporting and presentations. One common task is to change the displayed column names to make them m
3 min read
R program to find prime and composite numbers in an interval
A natural number (1, 2, 3, 4, 5 and so on) is called a prime number if it is greater than 1 and cannot be written as the product of two smaller natural numbers. The numbers greater than 1 that are not prime are called composite numbers. A composite number is a positive integer that can be formed by multiplying two smaller positive integers. Equival
7 min read
R Program to reverse a number - rev() Function
In this article, we will discuss how to reverse a number in R Programming Language. To reverse a number we will use in built method in R. In R Programming Language rev() function is used to return the reverse version of data objects. The data objects can be defined as Vectors, Data Frames by Columns &amp; by Rows, etc. Syntax: rev(x) Parameter: x:
2 min read
R program to find perfect numbers upto n numbers and in the given range
Given a range the task is to write a R program to print all perfect numbers in that range. A positive integer that is equal to the sum of its proper divisors is known as a perfect number. The smallest perfect number is 6, which is the sum of 1, 2, and 3. Other perfect numbers are 28, 496, and 8,128. Please note that all the perfect square numbers e
3 min read
R Program Commands
R is a powerful programming language and environment designed for statistical computing and data analysis. It is widely used by statisticians, data scientists, and researchers for its extensive capabilities in handling data, performing statistical analysis, and creating visualizations. Table of Content Overview of R CommandsData Visualizations comm
6 min read
How Important is R Program Language for a Management Consultant?
Answer: R programming is important for a management consultant, especially when dealing with data-driven projects that require statistical analysis, data visualization, and predictive modeling. While not all consulting roles require R, having proficiency in R can significantly enhance a consultant's ability to analyze complex datasets, generate ins
3 min read
How can I use C++ Call R Program?
You can use C++ to call an R program by leveraging the RInside and Rcpp packages. These packages allow seamless integration between C++ and R, enabling C++ programs to execute R code, call R functions, and even manipulate R objects directly within the C++ environment. This integration is particularly useful when you need the statistical power of R
3 min read
JIIT 128 Placement Training Program
A mock test will be conducted for 4th year CSE students of JIIT 128. The process would be similar to Amazon’s on campus placement. The process would involve an online test followed by face to face interview of top performers. The test would be of 30 MCQs from DS, algorithms, DBMS, OS, C/C++, JAVA and Aptitude, and 2 coding questions. On the basis o
1 min read
GeeksQuiz Authorship Program
If you have specialized knowledge on any of the below topics, then we provide an opportunity to earn. 1) Operating Systems 2) Theory of Computation 3) Database Management System 4) Computer Organization and Architecture 5) Compiler Design 6) Computer Networks 7) Numerical Methods 8) Mathematical Logic 9) Probability 10) Calculus 11) Digital Logic Y
2 min read
Incentive based Campus Geek Program
Incentive based Campus Geek Program Please note currently this program can be opted by only  5 Campus Ambassadors shortlisted by our team on the basis of applications and personal interviews. It is mandatory to associate GeeksforGeeks as official partner for your college programming club in order to conduct Geek Classes during this program. As offi
3 min read
Campus Training Program
{ "mobile":"+91-8076573709", "whatsapp":"+91-8076573709", "mail":"saurabh.mahawar@geeksforgeeks.org", "brochure":"https://media.geeksforgeeks.org/GeeksforGeeks-Campus-Training-Program.pdf", "b2b_section1":{ "title": "Placement Training Program", "desc": "A B2B program that focuses on technical campus training to enable students to become interview
7 min read
Campus Ambassador Program by GeeksforGeeks
.appSectionButton, #userActionsIcon, .entry-header{display: none !important;}article{padding: 0px !important;}#primary{margin: 0 !important;padding-top: 5px;}.ca-row{margin-right:0px!important;margin-left:0px!important;position:relative;}.last-container{margin-bottom: -15px!important;}.ca-container div:first-of-type{line-height:0!important}.ca-cont
1 min read
Data analysis using R
Data Analysis is a subset of data analytics, it is a process where the objective has to be made clear, collect the relevant data, preprocess the data, perform analysis(understand the data, explore insights), and then visualize it. The last step visualization is important to make people understand what's happening in the firm. Steps involved in data
5 min read
Frequency table in R
In this article, we are going to learn how to make a frequency table in the R programming language. Table of ContentFrequency TableOne-Way Frequency Tables in RTwo-Way Frequency Tables in RFrequency TableA frequency table is a list of objects with the frequency of each item shown in the table. When evaluating categorical data to determine how frequ
5 min read
How to import an Excel File into R ?
In this article, we will discuss how to import an excel file in the R Programming Language. There two different types of approaches to import the excel file into the R programming language and those are discussed properly below. File in use: Method 1: Using read_excel() In this approach to import the Excel file in the R, the user needs to call the
3 min read
R Tutorial | Learn R Programming Language
R Programming Tutorial is designed for beginners and experts. This free R Tutorial gives you knowledge basic to advanced of concepts of the R programming language. Here you will get a detailed introduction, features, installation, variables, data types, operators, if statements, vectors, data handling, graphics, and statistical modeling of R progra
9 min read
R - Data Frames
R Programming Language is an open-source programming language that is widely used as a statistical software and data analysis tool. Data Frames in R Language are generic data objects of R that are used to store tabular data.  Data frames can also be interpreted as matrices where each column of a matrix can be of different data types. R DataFrame is
10 min read
Taking Input from User in R Programming
Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. Like other programming languages in R it's also possible to take input from the user. For doing so, there are two methods in R. Using readline() me
7 min read
Printing Output of an R Program
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 pres
4 min read
Read contents of a CSV File in R Programming - read.csv() Function
read.csv() function in R Language is used to read "comma separated value" files. It imports data in the form of a data frame. Syntax: read.csv(file, header, sep, dec) Parameters: file: the path to the file containing the data to be imported into R. header: logical value. If TRUE, read.csv() assumes that your file has a header row, so row 1 is the n
3 min read
R - Matrices
R-matrix is a two-dimensional arrangement of data in rows and columns. In a matrix, rows are the ones that run horizontally and columns are the ones that run vertically. In R programming, matrices are two-dimensional, homogeneous data structures. These are some examples of matrices: Creating a Matrix in RTo create a matrix in R you need to use the
12 min read
R Data Types
Different forms of data that can be saved and manipulated are defined and categorized using data types in computer languages, including R. Each R data type has unique properties and associated operations. What are R Data types?R Data types are used to specify the kind of data that can be stored in a variable. For effective memory consumption and pr
8 min read
R Factors
Factors in R Programming Language are data structures that are implemented to categorize the data or represent categorical data and store it on multiple levels. They can be stored as integers with a corresponding label to every unique integer. The R factors may look similar to character vectors, they are integers and care must be taken while using
5 min read
Getting started with Data Visualization in R
Data visualization is the technique used to deliver insights in data using visual cues such as graphs, charts, maps, and many others. This is useful as it helps in intuitive and easy understanding of the large quantities of data and thereby make better decisions regarding it. Data Visualization in R Programming LanguageThe popular data visualizatio
6 min read
R Operators
Operators are the symbols directing the compiler to perform various kinds of operations between the operands. Operators simulate the various mathematical, logical, and decision operations performed on a set of Complex Numbers, Integers, and Numericals as input operands. R Operators R supports majorly four kinds of binary operators between a set of
9 min read
R Programming Language - Introduction
The R Language stands out as a powerful tool in the modern era of statistical computing and data analysis. Widely embraced by statisticians, data scientists, and researchers, the R Language offers an extensive suite of packages and libraries tailored for data manipulation, statistical modeling, and visualization. In this article, we explore the fea
7 min read
Simple Linear Regression in R
Regression shows a line or curve that passes through all the data points on the target-predictor graph in such a way that the vertical distance between the data points and the regression line is minimum What is Linear Regression?Linear Regression is a commonly used type of predictive analysis. Linear Regression is a statistical approach for modelli
12 min read
Article Tags :