The Art of R Programming

(WallPaper) #1
contains the filesfileaandfileb, as well as a subdirectorydir2, which holds the
filefilec. The contents of the files are as follows:


  • filea:5, 12, 13

  • fileb:3, 4, 5

  • filec:24, 25, 7


Ifdir1is in our current directory, the callsumtree("dir1")will yield the
sum of those nine numbers, 98. Otherwise, we need to specify the full path-
name ofdir1, such assumtree("/home/nm/dir1"). Here is the code:

1 sumtree <- function(drtr) {
2 tot <- 0
3 # get names of all files in the tree
4 fls <- dir(drtr,recursive=TRUE)
5 for (f in fls) {
6 # is f a directory?
7 f <- file.path(drtr,f)
8 if (!file.info(f)$isdir) {
9 tot <- tot + sum(scan(f,quiet=TRUE))
10 }
11 }
12 return(tot)
13 }

Note that this problem is a natural for recursion, which we discussed in
Section 7.9. But here, R has done the recursion for us by allowing it as an
option indir(). Thus, in line 4, we setrecursive=TRUEin order to find the files
throughout the various levels of the directory tree.
To callfile.info(), we need to account for the fact that the current file-
namefis relative todrtr, so our filefileawould be referred to asdir1/filea.
In order to form that pathname, we need to concatenatedrtr, a slash, and
filea. We could use the R string concatenation functionpaste()for this, but
we would need a separate case for Windows, which uses a backslash instead
of a slash. Butfile.path()does all that for us.
Some commentary pertaining to line 8 is in order. The function
file.info()returns information aboutfas a data frame, one of whose col-
umns isisdir, with one row for each file and with row names being the file-
names. That column consists of Boolean values indicating whether each file
is a directory. In line 8, then, we can detect whether the current filefis a
directory. Iffis an ordinary file, we go ahead and add its contents to our
running total.

10.3 Accessing the Internet.......................................................


R’s socket facilities give the programmer access to the Internet’s TCP/IP
protocol. For readers who are not familiar with this protocol, we begin with
an overview of TCP/IP.

246 Chapter 10

Free download pdf