Unit 3 1
Unit 3 1
Date:
A,B,C(AIDS)
Name of Faculty: Seema Kaloria Name of Course: R Programming Code: BADCCE5104
R provides several functions for accessing the keyboard and monitor. Here, we’ll discuss the
scan(), readline(), print(), and cat() functions.
the user for input and then read the input from the console. Here's a simple example:
# Prompt the user for their name
In this example, the readline() function is used to get input from the user, and the prompt
argument is used to display a message to instruct the user on what input is expected.
Note: readline() function is very handy to read in a single line from the keyboard.
Note: readline() function reads input as a character string. If you need to convert the input to a different data type,
you can use functions like as.numeric() or as.integer() for numeric input, or as.logical() for boolean input.
1
Faculty of : FCE Program: B.Tech Class/Section: Sem V, Sec. Date:
A,B,C(AIDS)
Name of Faculty: Seema Kaloria Name of Course: R Programming Code: BADCCE5104
Remember!! Handle user input carefully, especially if it involves converting to different data types, to avoid
potential errors or unexpected behavior.
You can use scan() to read in a vector, whether numeric or character, from a file or the keyboard.
With a little extra work, we can even read in data to form a list.
Example:
# Read numeric values from the console
In this example, the prompt argument is used to provide a message to the user. The user is
prompted to enter numeric values, and those values are stored in the variable data.
We can also use file parameter with scan() to read data from a file. For example:
Suppose we have files named z1.txt, z2.txt, z3.txt, and z4.txt. The z1.txt file contains the following:
z1.txt Z2.txt Z3.txt Z4.txt
2
Faculty of : FCE Program: B.Tech Class/Section: Sem V, Sec. Date:
A,B,C(AIDS)
Name of Faculty: Seema Kaloria Name of Course: R Programming Code: BADCCE5104
scan("z3.txt",what="")
Read 4 items
Output:
"abc" "de" "f" "g"
Explanation: In the first call, we got a vector of four integers (though the mode is numeric). The
second time, since one number was non integral, the others were shown as floating-point
numbers, too.
In the third case, we got an error. The scan() function has an optional argument named what,
which specifies mode, defaulting to double mode. So, the nonnumeric contents of the file z3
produced an error. But we then tried again, with what="". This assigns a character string to what,
indicating that we want character mode. (We could have set what to any character string.)
The last call worked the same way. The first item was a character string, so it treated all the
items that followed as strings too.
Note: We would assign the return value of scan() to a variable. Here’s an example:
> v <- scan ("z1.txt")
By default, scan() assumes that the items of the vector are separated by whitespace, which
includes blanks, carriage return/line feeds, and horizontal tabs. We can use the optional sep
argument for other situations. As example, we can set sep to the newline character to read in
3
Faculty of : FCE Program: B.Tech Class/Section: Sem V, Sec. Date:
A,B,C(AIDS)
Name of Faculty: Seema Kaloria Name of Course: R Programming Code: BADCCE5104
4
Faculty of : FCE Program: B.Tech Class/Section: Sem V, Sec. Date:
A,B,C(AIDS)
Name of Faculty: Seema Kaloria Name of Course: R Programming Code: BADCCE5104
print() function
> x <- 1:3
> print(x^2) Output: 1 4 9
Note: print() is a generic function, so the actual function called will depend on the class of the
object that is printed. If, for example, the argument is of class "table", then the print.table()
function will be called.
cat() function
It’s a little better to use cat() instead of print(), as the latter can print only one
expression and its output is numbered, which may be a nuisance. Compare the results
of the functions:
> print("abc") Output: [1] "abc"
> cat("abc\n") Output: abc
Note that we needed to supply our own end-of-line character, "\n", in the call to cat().
Without it, our next call would continue to write to the same line.
The arguments to cat() will be printed out with intervening spaces:
> x <- 1:3
>x Output: 1 2 3
> cat(x,"abc", "de\n") Output: 1 2 3 abc de
If you don’t want the spaces, set sep to the empty string "", as follows:
> cat(x,"abc","de\n",sep="") Output: 123abcde
Any string can be used for sep. Here, we use the newline character:
> cat(x,"abc","de\n",sep="\n")
5
Faculty of : FCE Program: B.Tech Class/Section: Sem V, Sec. Date:
A,B,C(AIDS)
Name of Faculty: Seema Kaloria Name of Course: R Programming Code: BADCCE5104
1
2
3
abc
de
A=5 A=5
print(A) #Output: 5 cat(A) #Output: 5
print(“A”) #Output: A cat(“A”) #Output: A
print(“A=”, A) #Output: cat(“A=”, A) #Output:
A= A=5