In this tutorial, you are going to familiarize yourself with the basic input and output functions.
In R language input from the user is taken from the console using two methods. They are
readline()function: Takes input from user in string format.
scan()function: Reads data from the user console or file
readline() function takes or reads input from a user console in string format.
<variable_name> <- readline() <variable_name> <- as.integer(<variable_name>)
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.
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() # readline() reads input from user console a = readline(); # convert the inputted value to integer a = as.integer(a); # print the value print(a)
5  5
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.
|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 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)
Enter a value of floating type :4.5 Enter a value of complex type :4+2i  4.5  4+2i
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.
<variable_name> <- readline(prompt = “ “) <variable_name> <- as.integer(<variable_name>)
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
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)
Enter value of a : 6 Enter value of b : 6 The sum after addition is 12
In R program
scan() function is a built-in function that reads data or input from the user console.
# 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
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  4 5 6 7 8 9 10 11  "hello" "welcome" "to" "Rlanguage" "enjoy" "learning" character(0)
R program supports built-in functions to display output to the user console or screen. They are
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.
print(arguments) #print arguments to console
cat(any_text) #print text to console
language <- "R programming language" #created a string language print(language) #Apply print to character string language cat(language) #Apply cat to character string
 "R programming language" R programming language
• 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.