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!
Keeping you updated with latest technology trends, Join TechVidvan on Telegram
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:
- How to Read user input in R?
- How to display output in R?
- How to write in a file?
- How to read from a file?
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:
1. readline() function
We can read the input given by the user in the terminal with the
input_read <- readline()
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:
input_read2 <- readline()
input_read2 input_read2 <- strsplit(input_read2," ") input_read2 input_read2 <- as.integer(input_read2[]) input_read2
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.
input_scan <- scan()
Read 5 items
input_scan2 <- scan()
1: 34 566 2 a 2+1i
Error in scan() : scan() expected ‘a real’, got ‘a’
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:
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:
How to Write in a File?
write.table() function is a very handy way to write tabular data to a file. For example:
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)
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
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:
matrix_scan <- matrix(scan("/home/techvidvan/Documents/matrix"), nrow=3) matrix_scan
2. readlines() function
We can also use the
readlines() function to read a file one line at a time. For example:
lines <- file("/home/techvidvan/Documents/matrix") readLines(lines,n=3)
3. read.table() function
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:
tabl <- read.table("/home/techvidvan/Documents/table") tabl
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.