Input & Output Functions in R – scan(), cat(), write.table()

Input and output functions are a programming language’s ability to talk and interact with its users. Have a look at many such functions in R.

Today, we are going to learn about functions in R that let us get input from the user or a file and display output to the screen or to store it in a file. R has many functions that make these tasks extremely simple.

So without further ado, let’s get going!

Input/Output Functions in R

With R, we can read inputs from the user or a file using simple and easy-to-use functions. Similarly, we can display the complex output or store it to a file using the same. R’s base package has many such functions, and there are packages that provide functions that can do the same and process the information in the required ways at the same time.

In this article, you’ll get the answers to these:

Let’s take a look at a few of these functions.

How to Read User Input in R?

In R, there are multiple ways to read and save input given by the user. here are a few of them:

Follow TechVidvan on Google & Stay updated with latest technology trends

1. readline() function

We can read the input given by the user in the terminal with the readline() function.

Code:

input_read <- readline()

User Input:

45 3 45 a hello techvidvan

Code:

input_read

Output:

readline() - input output functions in r

The function returns a character vector containing the input values. If we need the input of other data types, then we need to make conversions. For example:

Code:

input_read2 <- readline()

User Input:

3 4 23 55 34 76

Code:

input_read2
input_read2 <- strsplit(input_read2," ")
input_read2
input_read2 <- as.integer(input_read2[[1]]) 
input_read2

Output:

readline() type conversion - Input and output functions in R

Functions that take vectors as input or give output in vectors are called vector functions. Learn more about them in R vector functions.

2. scan() function

We can also use the scan() function to read user input. This function, however, can only read numeric values and returns a numeric vector. If a non-numeric input is given, the function gives an error.

Code:

input_scan <- scan()

User Input:

1: 34 54 65 75 23
6:
Read 5 items

Code:

input_scan

Code:

input_scan2 <- scan()

User Input:

1: 34 566 2 a 2+1i

Error in scan() : scan() expected ‘a real’, got ‘a’

Output:

scan() - input output features in r

How to Display Output in R?

To display the output of your program to the screen, you can use one of the following functions:

1. print() functions

We can use the print() function to display the output to the terminal. The print() function is a generic function. This means that the function has a lot of different methods for different types of objects it may need to print. The function takes an object as the argument. For example:

Example 1:

print(input_read)

Example 2:

print(input_scan)

Example 3:

print("abc")

Example 4:

print(34)

Output:

print() - input output functions in r

Learn to make your R programs more efficient and readable with generic functions.

2. cat() function

We can also use the cat() function to display a string. The cat() function concatenates all of the arguments and forms a single string which it then prints. For example:

Code:

cat("hello", "this","is","techvidvan",12345,TRUE)

Output:

cat() - input output features in r

How to Write in a File?

The write.table() function is a very handy way to write tabular data to a file. For example:

Code:

data <- read.table(header=TRUE,text='
subject sex size
1 M 8
2 F 7
3 F 9
4 M NA
5 F 7
')
write.table(data,"/home/techvidvan/Documents/table",row.names=F,col.names=F)

Output:

write.table() file output - input output functions in r

How to Read From a File?

Depending upon the data and the type of the file, there are multiple functions in R that can be used to read data from a file. they are:

1. scan() function

The scan() function can read data from a file as well. The data may be stored as a data frame and, therefore, may require conversions into desired formats. For example:

Code:

matrix_scan <- matrix(scan("/home/techvidvan/Documents/matrix"), nrow=3)
matrix_scan

Output:

read from file using scan() - input output features in r

2. readlines() function

We can also use the readlines() function to read a file one line at a time. For example:

Code:

lines <- file("/home/techvidvan/Documents/matrix")
readLines(lines,n=3)

Output:

readLines() - input output functions in r

3. read.table() function

The read.table() function is another useful function that can be used to read data from a file and store it in the form of a table. For example:

Code:

tabl <- read.table("/home/techvidvan/Documents/table")
tabl

Output:

read from file using read.table() - input output features in r

Summary

In this R tutorial, we learned the various functions in R that let us read user input or data from a file. We also learned about functions that allow us to print output to the terminal and to write data into files. Input and output functions add interactivity to an R program.

These functions make R programs easier to use and also make them much more interactive.

If you find any error while working on these Input and output functions in R.

Ask our TechVidvan Experts.

Keep practicing!!

Did you like this article? If Yes, please give TechVidvan 5 Stars on Google | Facebook


Leave a Reply

Your email address will not be published. Required fields are marked *