Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Taking Input from User in R Programming

  • Last Updated : 07 Jul, 2021

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




# 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






# 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.
 

Syntax
var1 = readline(“Enter 1st number : “); 
var2 = readline(“Enter 2nd number : “); 
var3 = readline(“Enter 3rd number : “); 
var4 = readline(“Enter 4th number : “);
or,

var1 = readline(“Enter 1st number : “); 
var2 = readline(“Enter 2nd number : “); 
var3 = readline(“Enter 3rd number : “); 
var4 = readline(“Enter 4th number : “); 
}

Example: 
 

R




# 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.



 

Syntax:
string
var1 = readline(prompt = “Enter your name : “);
character
var1 = readline(prompt = “Enter any character : “); 
var1 = as.character(var1)

Example: 
 

R




# 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 : GeeksforGeeks
Enter any character : G
[1] "GeeksforGeeks"
[1] "G"

 

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






# 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




# 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: geeksfor geeks gfg
4: c++ R java python
8: 
Read 7 items
1: g e e k s f o
8: r g e e k s
14: 
Read 13 items
[1] 123.321 523.458 632.147 741.250 855.360
[1] "geeksfor" "geeks"    "gfg"      "c++"      "R"        "java"     "python"  
[1] "g" "e" "e" "k" "s" "f" "o" "r" "g" "e" "e" "k" "s"

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

Read File data using scan() method

To read file using scan() method is same as normal console input, only thing is that, one needs to pass the file name and data type to the scan() method.
 

Syntax: 
x = scan(“fileDouble.txt”, what = double()) —-for double 
x = scan(“fileString.txt”, what = ” “) —-for string 
x = scan(“fileChar.txt”, what = character()) —-for character

Example: 
 

R




# R program to illustrate
# taking input from the user
 
# string file input using scan()
s = scan("fileString.txt", what = " ")
 
# double file input using scan()
d = scan("fileDouble.txt", what = double())
 
# character file input using scan()
c = scan("fileChar.txt", what = character())
 
# print the inputted values
print(s) # string
print(d) # double
print(c) # character

Output: 
 

Read 7 items
Read 5 items
Read 13 items
[1] "geek"   "for"    "geeks"  "gfg"    "c++"    "java"   "python"
[1] 123.321 523.458 632.147 741.250 855.360
[1] "g" "e" "e" "k" "s" "f" "o" "r" "g" "e" "e" "k" "s"

Save the data file in the same location where the program is saved for better access. Otherwise total path of the file need to defined inside the scan() method.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :