R Input/Output


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


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

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)


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

Printing Output
In R there are various methods to print the output. Most common method to print output in R program, there is a function called print() is used. Also if the program of R is written over the console line by line then the output is printed normally, no need to use any function for print that output.

Print output using print() function

Using print() function to print output is the most common method in R. Implementation of this method is very simple.

Syntax: 
 print(“any string”) or,
 print(variable)
Example:
# R program to illustrate
# printing output of an R program

# print string
print("mec")

# print variable
# it will print 'GeeksforGeeks' on the console
x <- "Model engineering College"
print(x)
output
[1] "mec"
[1] "Model Engineering College"

Print output using paste() function inside print() function

R provides a method paste() to print output with string and variable together. This method defined inside the print() function. paste() converts its arguments to character strings. One can also use paste0() method.


Note: The difference between paste() and paste0() is that the argument sep by default is ” “(paste) and “”(paste0).

Syntax: 
    print(paste(“any string”, variable)) or, print(paste0(variable, “any string”))

# R program to illustrate
# printing output of an R program

x <- "Model Engineering College"

# using paste inside print()
print(paste(x, "is best "))

# using paste0 inside print()
print(paste0(x, "is best"))

output
[1] "Model Engineering College is best "
[1] "Model Engineering Collegeis best"


Print output using sprintf() function

sprintf() is basically a C library function. This function is use to print string as C language. This is working as a wrapper function to print values and strings together like C language. This function returns a character vector containing a formatted combination of string and variable to be printed.

Syntax: 
sprintf(“any string %d”, variable) or, sprintf(“any string %s”, variable) or, 
sprintf(“any string %f”, variable)) etc.

example
# R program to illustrate
# printing output of an R program

x = "Model Engg College" # string
x1 = 255 # integer
x2 = 23.14 # float

# string print
sprintf("%s is best", x)

# integer print
sprintf("%d is integer", x1)

# float print
sprintf("%f is float", x2)

output
[1] "Model Engg College is best"
[1] "255 is integer"
[1] "23.140000 is float"

Print output using cat() function

Another way to print output in R is using of cat() function. It’s same as print() function. cat() converts its arguments to character strings. This is useful for printing output in user defined functions.

Syntax: 
cat(“any string”) or, cat(“any string”, variable)

# R program to illustrate
# printing output of an R program

# print string with variable
# "\n" for new line
x = "Model Engg College"
cat(x, "is best\n")

# print normal string
cat("This is R language")

output
Model Engg College is best
This is R language

Print output using message() function

Another way to print something in R by using message() function. This is not used for print output but its use for showing simple diagnostic messages which are no warnings or errors in the program. But it can be used for normal uses for printing output.

Syntax: 
    message(“any string”) or, message(“any string”, variable)
Example:
# R program to illustrate
# printing output of an R program

x = "Model Engg College "
# print string with variable
message(x, "is best")

# print normal string
message("This is R language")

output
Model Engg College is best
This is R language

Comments

Popular posts from this blog

Programming in R - Dr Binu V P

Introduction

R Data Types