# Write a program in several steps to graph the pairs
#   (rushing yardage, receiving yardage)
# across the 32 NFL teams.
# 
# 1. Start by reading a single team's statistics page.

rm(list=ls())

if (!require("XML")) { # for readHTMLTable()
    install.packages("XML")
    stopifnot(require("XML"))
}
if (!require("curl")) { # for curl()
    install.packages("curl")
    stopifnot(require("curl"))
}

link = "https://www.packers.com/team/stats/2021/REG"
## Get a list of data frames corresponding to the HTML tables.
tables = readHTMLTable(readLines(curl(link), warn=FALSE))
print(tables) # Which are the rushing and receiving tables?

print(tables[[8]]) # Where is the best rusher's season yardage?
rushing = tables[[8]][ , 3] # YDS ("yards") column
print(rushing) # Oops, it's character strings
print(as.numeric(rushing)) # Now it's numbers

## Do it right in one line.
rushing = sum(as.numeric(tables[[8]][ , 3]))

## Similarly, the best receiver's season yardage is:
receiving = sum(as.numeric(tables[[9]][ , 3]))

cat(sep="", "rushing=", rushing, ", receiving=", receiving, "\n")
