# Function to read range of cranberries new package files
# and count number of new packages for the months in question
# The function takes a starting year and ending year as inputs
# and assumes each year has a full 12 months of data.

num_new <- function(start,end){
  # start and end must be years between 2008 and 2016 with start <= end
  index <- start - (start -1)
  y_span <- start:end
  m_span <- length(y_span) * 12
  
  num_new_pkgs <- vector(mode="integer", length=m_span)
  for (year in y_span){
    two_digits <- c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")
    for (month in two_digits) {
      url <- paste("http://dirk.eddelbuettel.com/cranberries/", year, "/", month, sep="")
      raw.data <- readLines(url)
      num_new_pkgs[index] <- length(grep("New package", raw.data, value=TRUE))
      index <- index + 1
    }
  }
  return(num_new_pkgs)
}

#results <- num_new(2008, 2016)