User Inputs in R

User Inputs in R

  • R
  • 4 mins read

In this article, we will study how to accept inputs from the user in R

Programming is all about creating an interactive environment for the user server system. The programs should be developed in such a way that they work with any customized input from the user. This not only increases the user experience but also makes the program more robust. It improves the thinking ability of the user to cover all the corner cases and exceptions that the program may encounter. 

User Inputs Methods in R

There are two methods available in R to accept user inputs, which are: 

readline() input method

The readline() method in R is used to input the data from the user in a string format. The user has the liberty to input numerical, complex, or character data. However, all the data elements are implicitly converted into a string format by this method. In order to retrieve the values, the conversion into the desired format has to be made. It basically accepts text lines. 

The use of this method eliminates the leading and trailing space characters from the input string value. The method has the following syntax : 

readline (prompt = )

In this method, the prompt parameter is used to display a string value to the user, mostly to guide him about what is expected from him/her. It is an optional parameter. 

#accepting an input from the user
input = readline()
The code produces the following output : 
“FoxInfoTech”

In case the user inputs a numerical value, which is implicitly converted to a string, it can be coerced back to the desired data type using the following in-built methods : 

  1. as_integer(): In case the inputted value is an integer, it can be retrieved back using this method
  2. as_complex():  In case the inputted value is a complex number, it can be retrieved back using this method
  3. as_numeric(): In this method, the inputted value can be retrieved back to obtain a float or a double value. 
  4. as.Date(): This method is used to obtain a date object in R
  5. as.character(): This method is used to retrieve the data in the character data type.
#accepting an input from the user
input = readline()

#converting into complex number
print(as.complex(input))
The code produces the following output : 
“4+3i”
[1] 4+3i

It is also possible to accept multiple inputs with prompt parameters from the user also, all of which may or may not belong to the same data type. 

#taking multiple inputs from the user
var1 = readline(prompt = "Enter a real number : ")
var2 = readline(prompt = "Enter a character : ")
var3 = readline(prompt = "Enter a string : ")
var4 = readline(prompt = "Enter a complex number : ")
var5 = readline(prompt = "Enter a date : ")

#converting the outputs 
var1 = as.integer(var1)
var2 = as.character(var2)
var4 = as.complex(var4)
var5 = as.Date(var5)

#printing the output
cat("var1 : ", var1)
cat("var2 : ", var2)
cat("var3 : ", var3)
cat("var4 : ", var4)
cat("var5 : ", var5)
The code produces the following output : 
Enter a real number : 1
Enter a character : A
Enter a string : FoxInfoTech
Enter a complex number : 4-2i
Enter a date : 2022-10-03
[1] 1
[1] “A”
[1] “FoxInfoTech”
[1] 4-2i
[1] “2022-10-03”

scan() input method

The scan() method in R is used to accept input in the form of a list or vector. This method can take an infinite number of inputs until the process is terminated by pressing the “Enter” key twice on the console screen. It can be quickly used to input values in a dataset or to simulate mathematical calculations. The method has the following syntax : 

scan (what = )

where the parameter is used to accept what kind of data type will be input in this method. The value of what parameter is “ “ for string, double() for a double value, and character() for the character. 

By default, the scan() method takes an integer as input. 

#taking multiple inputs from the user
var1 = scan()

print(var1)
The code produces the following output : 
1: 1
2: 2
3: 3
4: 4
5: 5

[1] 1 2 3 4 5
Read 5 items