Input and Output in R

In this tutorial, you are going to familiarize yourself with the basic input and output functions.

How to take input from a user in R?

In R language input from the user is taken from the console using two methods. They are

  1. readline() function: Takes input from user in string format.
  2. scan() function: Reads data from the user console or file

What is a readline() in R?

A readline() function takes or reads input from a user console in string format.

Syntax for readline()


<variable_name>  <- readline()
<variable_name>  <- as.integer(<variable_name>)
 

The readline() takes input from the user in character or string form and assigns it to a variable of any declared data type. The next line as. integer() method converts the string data type read by readline() method to integer type or any other type based on the predefined variable data type. Then assigns the converted value to a variable.

Example for readline()

a <- readline()
a <- as.integer(a)
 

In the given example variable declared is a.The method readline() reads input converts to integer data type and stores value to variable a

Program to read input from user using readline()


# Program to read input from user using readline()

#  readline() reads input from user console

a = readline();

# convert the inputted value to integer
a = as.integer(a);

# print the value
print(a)

 

Output:


5
[1] 5

What are functions used to convert variables of string datatype?

Some of the functions to convert the variables of string data types to their corresponding data types like integer,float,complex etc are given in below table a is a variable.

Conversion type Use
as.integer(a) a gets converted from string to integer
s.numeric(a) a gets converted from string to numeric type.
as.complex(a) a gets converted to complex number
as.Date(a) a gets converted to date

Note: No string type conversion is available in R because readline takes input or read data from console as string format.

program showing conversions


# Program to read input from user using readline()

#  readline() reads input from user console as string
a = readline("Enter a value of floating type :")   #4.3
b = readline("Enter a value of complex type :")   #4+2i

# convert the inputted value to float ,complex
a = as.numeric(a)
b = as.complex(b)

# print the value
print(a)
print(b)
 

Output:


Enter a value of floating type :4.5
Enter a value of complex type :4+2i
[1] 4.5
[1] 4+2i

What is prompt() function in R ?

The prompt () function is a built-in function that displays the text message in the console where the user can read the message and perform the corresponding action.

Syntax for prompt()


<variable_name>  <- readline(prompt = “              “)
<variable_name>  <- as.integer(<variable_name>)
 

Example


a <- readline(prompt = "Enter value of a : ")
a <- as.integer(a)
 

A variable a is declared to which a value is assigned by readline() method. The readline() methods read the data from the user console or screen. You can see the highlighted portion (prompt = "Enter value of a : ") the prompt() function prints the given strings inside quotes into the user console. The user can visualize the message Enter value of a and the corresponding number will be entered by the user. The number read to a is of string type which is converted in the next line to integer type using as.integer(a) and gets stored in a itself.

Similarly, read a variable b in the below program. An addition operation is performed and outputs the value to the console using cat() function.

Program to take input from user using readline () and prompt()


a <- readline(prompt = "Enter value of a : ")
a <- as.integer(a)

b <- readline(prompt = "Enter value of b :  ")
b<- as.integer(b)

add = a + b
cat("The sum after addition is ",add)

 

Output:


Enter value of a : 6
Enter value of b :  6
The sum after addition is  12

What is scan() function in R?

In R program scan() function is a built-in function that reads data or input from the user console.

Program using scan()


# R program to illustrate
# taking input from the user

# taking input using scan()
A = scan()
B = scan(what = " ")
c = scan(what = character())


# print the inputted values
print(A) #integer
print(B) # string
print(c) # character

 

Output:


1: 4 5 6 7
5: 8 9 10 11
9: 
Read 8 items
1: hello welcome to Rlanguage
1: hello welcome to Rlanguage
5: enjoy learning
7: 
Read 6 items
1: 
Read 0 items
[1]  4  5  6  7  8  9 10 11
[1] "hello"     "welcome"   "to"        "Rlanguage" "enjoy"     "learning" 
character(0)

Output Functions in R

R program supports built-in functions to display output to the user console or screen. They are

  1. print()
  2. cat()

In R the print() and cat functions have almost similar functionality of displaying the contents to the user console. That is print() and cat() act as functions that provide the user with corresponding output.

Syntax for print()


print(arguments)                     #print arguments to console       
 

Syntax for cat()


cat(any_text)                              #print text to console
 

Program using print() and cat() in R


language <- "R programming language"    #created a string language
print(language)                         #Apply print to character string language

cat(language)                           #Apply cat to character string

 

Output:


[1] "R programming language"
R programming language

Note :

print() function returns an object or character vector passed to the function to the console .

cat() function returns character string or prints the arguments without quotes.cat() function just displays the contents(text) to the screen or console.