top of page

Taking Input from User in R Programming

there are two methods in R.

  • Using readline() method

  • Using scan() method

Using readline() method


In R language readline() method takes input in string format. If one inputs an integer then it is inputted as a string, lets say, one wants to input 255, then it will input as “255”, like a string. So one needs to convert that inputted value to the format that he needs. In this case, string “255” is converted to integer 255. To convert the inputted value to the desired data type, there are some functions in R,

  • as.integer(n); —> convert to integer

  • as.numeric(n); —> convert to numeric type (float, double etc)

  • as.complex(n); —> convert to complex number (i.e 3+2i)

  • as.Date(n) —> convert to date …, etc

Syntax: var = readline(); var = as.integer(var); Note that one can use “<-“ instead of “=”

Example:


# R program to illustrate
# taking input from the user

# taking input using readline()
# this command will prompt you
# to input a desired value
var = readline();

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

# print the value
print(var)

Output:

255
[1] 255

One can also show message in the console window to tell the user, what to input in the program. To do this one must use a argument named prompt inside the readline() function. Actually prompt argument facilitates other functions to constructing of files documenting. But prompt is not mandatory to use all the time.

Syntax: var1 = readline(prompt = “Enter any number : “); or, var1 = readline(“Enter any number : “);

Example:

# R program to illustrate
# taking input from the user

# taking input with showing the message
var = readline(prompt = "Enter any number : ");

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

# print the value
print(var)

Output:

Enter any number : 255
[1] 255


Taking multiple inputs in R


Taking multiple inputs in R language is same as taking single input, just need to define multiple readline() for inputs. One can use braces for define multiple readline() inside it.


# R program to illustrate
# taking input from the user

# taking multiple inputs
# using braces
{
	var1 = readline("Enter 1st number : ");
	var2 = readline("Enter 2nd number : ");
	var3 = readline("Enter 3rd number : ");
	var4 = readline("Enter 4th number : ");
}

# converting each value
var1 = as.integer(var1);
var2 = as.integer(var2);
var3 = as.integer(var3);
var4 = as.integer(var4);

# print the sum of the 4 number
print(var1 + var2 + var3 + var4)

Output:

Enter 1st number : 12
Enter 2nd number : 13
Enter 3rd number : 14
Enter 4th number : 15
[1] 54


Taking String and Character input in R


To take string input is the same as an integer. For “String” one doesn’t need to convert the inputted data into a string because R takes input as string always. And for “character”, it needs to be converted to ‘character’. Sometimes it may not cause any error. One can take character input as same as string also, but that inputted data is of type string for the entire program. So the best way to use that inputted data as ‘character’ is to convert the data to a character.


Example:



# R program to illustrate
# taking input from the user

# string input
var1 = readline(prompt = "Enter your name : ");

# character input
var2 = readline(prompt = "Enter any character : ");
# convert to character
var2 = as.character(var2)

# printing values
print(var1)
print(var2)

Output:

Enter your name : codeswithpankaj
Enter any character : c
[1] "codeswithpankaj"
[1] "c"


Using scan() method


Another way to take user input in R language is using a method, called scan() method. This method takes input from the console. This method is a very handy method while inputs are needed to taken quickly for any mathematical calculation or for any dataset. This method reads data in the form of a vector or list. This method also uses to reads input from a file also.

Syntax: 
x = scan()
scan() method is taking input continuously, to terminate the input process, need to press Enter key 2 times on the console.

Example: This is simple method to take input using scan() method, where some integer number is taking as input and print those values in the next line on the console.


# R program to illustrate
# taking input from the user

# taking input using scan()
x = scan()
# print the inputted values
print(x)

Output:

1: 1 2 3 4 5 6
7: 7 8 9 4 5 6
13: 
Read 12 items
 [1] 1 2 3 4 5 6 7 8 9 4 5 6

Explanation: Total 12 integers are taking as input in 2 lines when the control goes to 3rd line then by pressing Enter key 2 times the input process will be terminated.


Taking double, string, character type values using scan() method


To take double, string, character types inputs, specify the type of the inputted value in the scan() method. To do this there is an argument called what, by which one can specify the data type of the inputted value.

Syntax: x = scan(what = double()) —-for double x = scan(what = ” “) —-for string x = scan(what = character()) —-for character

Example:

# R program to illustrate
# taking input from the user

# double input using scan()
d = scan(what = double())

# string input using 'scan()'
s = scan(what = " ")

# character input using 'scan()'
c = scan(what = character())

# print the inputted values
print(d) # double
print(s) # string
print(c) # character

Output:

1: 123.321 523.458 632.147
4: 741.25 855.36
6: 
Read 5 items
1: codeswithpankaj
4: c++ R java python
8:
Read 7 items
1: c o d e s w i t h p a n k a j
8: p 4 n
14: 
Read 13 items
[1] 123.321 523.458 632.147 741.250 855.360
[1] "codeswithpankaj" "c++" "R" "java" "python"  
[1] "c" "o" "d" "e" "s" "w" "i" "t" "h" "p" "a" "n" "k" "a" "j"

Explanation: Here, count of double items is 5, count of sorting items is 7, count of character items is 13.



Related Posts

See All

A Comprehensive Guide to R Data Types

Introduction In the realm of data analysis and statistical computing, the R programming language stands as a powerful tool. Understanding data types in R is fundamental to unleashing its full potentia

R - CSV Files

Getting and Setting the Working Directory You can check which directory the R workspace is pointing to using the getwd() function. You can also set a new working directory using setwd()function. # Get

Comments


bottom of page