0% found this document useful (0 votes)
10 views6 pages

Unit 3 1

Uploaded by

RAHUL SHARMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Unit 3 1

Uploaded by

RAHUL SHARMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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

Accessing the keyboard and Monitor

R provides several functions for accessing the keyboard and monitor. Here, we’ll discuss the
scan(), readline(), print(), and cat() functions.

Using the readline() Function

In R, we can get input from users using the

function. This function allows us to promptreadline()

the user for input and then read the input from the console. Here's a simple example:
# Prompt the user for their name

name <- readline( )


name <- readline("Enter your name: ")
name <- readline(prompt = "Enter your 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.

Here's an example that reads a numeric value from the user:


# Prompt the user for a numeric value
numeric_input <- readline(prompt = "Enter a numeric value: ")

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

# Convert the input to a numeric type


numeric_input <- as.numeric(numeric_input)

Remember!! Handle user input carefully, especially if it involves converting to different data types, to avoid
potential errors or unexpected behavior.

 Using the scan() Function

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

data <- scan( )


data <- scan(prompt = "Enter numeric values: ")

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

123 123 abc Abc


4 5 4.2 5 de f 123 6
6 6 g Y

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("z1.txt") scan("z2.txt") scan("z3.txt")


scan("z4.txt",what="")
Read 4 items Read 4 items Error in scan(file,
Output: Output: what, nmax, sep, dec, Read 4 items
123 4 5 6 123.0 4.2 5.0 6.0 quote, skip, nlines, Output:
na.strings, : "abc" "123" "6" "y"
scan() expected 'a
real', got 'abc'

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

each line as a string, as follows:


Consider the file Z3.txt
abc
de f
g
> x1 <- scan("z3.txt",what="") # Read 4 items
> x2 <- scan("z3.txt",what="", sep="\n") # Read 3 items
> x1 Output: "abc" "de" "f" "g"
> x2 Output: "abc" "de f" "g"
> x1[2] Output: "de"
> x2[2] Output: "de f"
In the first case, the strings "de" and "f" were assigned to separate elements of x1. But in the
second case, we specified that elements of x2 were to be delineated by end-of-line characters, not
spaces. Since "de" and "f" are on the same line, they are assigned together to x[2].
We can use scan() to read from the keyboard by specifying an empty string for the filename:
> v <- scan("")
1: 12 5 13
4: 3 4 5
7: 8
8:
Read 7 items
>v Output: 12 5 13 3 4 5 8
Note that we are prompted with the index of the next item to be input, and we signal the end of
input with an empty line.
If you do not wish scan() to announce the number of items it has read, include the argument
quiet=TRUE.
 Printing to the Screen

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

You can even set sep to be a vector of strings, like this:


> x <- c(5,12,13,8,88) > cat(x, sep=c(".",".",".","\n","\n"))
5.12.13.8
88

print() function cat() function

 It takes only one argument.  It takes multiple arguments.


 It does not concatenate multiple outputs  It concatenates multiple outputs
Example: Example:

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

You might also like