### https://www.census.gov/retail/marts/www/timeseries.html
census_bureau_retail_links<-function(cb_url="https://www.census.gov/retail/marts/www/timeseries.html")
{
  my_con<-file(cb_url,"r")
  my_html<-readLines(my_con,-1)
  close(my_con)
  my_lines<-my_html[grep("[.]txt",my_html)]
  my_desc<-unlist(lapply(strsplit(my_lines,"href=\""),function(x) {strsplit(strsplit(x[2],"\"[>]")[[1]][2],"[<][/]a[>]")[[1]][1]}))
  my_links<-unlist(lapply(strsplit(my_lines,"href=\""),function(x) {strsplit(x[2],"\"[>]")[[1]][1]}))

  return(data.frame(Description=my_desc,Url=my_links))
}
getCensusBureauRetailDataDeseasonalized<-function(Item="Retail and Food Services, total")
{
  my_data_links<-census_bureau_retail_links()
  v<-which(Item==as.character(my_data_links$Description))
  my_df<-NULL
  if (length(v)>0) {
    my_con<-file(paste0("https://www.census.gov/",as.character(my_data_links$Url[v[1]])),"r")
    my_txt<-readLines(my_con,-1)
    close(my_con)
    #### Parse Data
    d_header<-grep("YEAR",my_txt)[1]+1
    d_footer<-grep("SEASONAL",my_txt)[1] - 1
    my_data<-na.omit(unlist(lapply(as.list(my_txt[d_header:d_footer]),function(x) {as.numeric(trimws(strsplit(x,"   ")[[1]],which="both"))})))
    my_data<-c(my_data,rep(NA,13*(floor(length(my_data)/13)+1)-length(my_data)))
    my_matrix<-matrix(my_data,ncol=13,byrow=TRUE)
    my_df<-data.frame(my_matrix)
    names(my_df)<-c("Year",month.name)
    ####
  } else {
    cat("I did not find ",Item," available.  Available items are:\n")
    print(as.character(my_data_links$Description))
  }
  return(my_df)
}
getCensusBureauRetailDataSeasonalFactors<-function(Item="Retail and Food Services, total")
{
  my_data_links<-census_bureau_retail_links()
  v<-which(Item==as.character(my_data_links$Description))
  my_df<-NULL
  if (length(v)>0) {
    my_con<-file(paste0("https://www.census.gov/",as.character(my_data_links$Url[v[1]])),"r")
    my_txt<-readLines(my_con,-1)
    close(my_con)
    #### Parse Data
    d_header<-grep("YEAR",my_txt)[2]+1
    d_footer<-length(my_txt)
    my_data<-na.omit(unlist(lapply(as.list(my_txt[d_header:d_footer]),function(x) {as.numeric(trimws(strsplit(x,"   ")[[1]],which="both"))})))
    my_data<-c(my_data,rep(NA,13*(floor(length(my_data)/13)+1)-length(my_data)))
    my_matrix<-matrix(my_data,ncol=13,byrow=TRUE)
    my_df<-data.frame(my_matrix)
    names(my_df)<-c("Year",month.name)
    ####
  } else {
    cat("I did not find ",Item," available.  Available items are:\n")
    print(as.character(my_data_links$Description))
  }
  return(my_df)
}
getCensusBureauRetailDataRecovered<-function(Item="Retail and Food Services, total")
{
  my_df<-NULL
  deseasonalized<-getCensusBureauRetailDataDeseasonalized(Item=Item)
  if (!is.null(deseasonalized)) {
    seasonalFactor<-getCensusBureauRetailDataSeasonalFactors(Item=Item)
    my_df<-deseasonalized
    for (mymonth in names(my_df)[2:ncol(my_df)]) {
      my_df[,mymonth]<-as.numeric(as.character(seasonalFactor[1:nrow(my_df),mymonth]))*as.numeric(as.character(my_df[,mymonth]))
    }
  }
  return(my_df)
}
convertCensusBureauRetailDataTo_ts<-function(Data=getCensusBureauRetailDataRecovered())
{
  y<-unlist(lapply(as.list(1:nrow(Data)),function(x) {unlist(as.numeric(Data[x,2:ncol(Data)]))}))
  y.ts<-ts(y,
           start=c(as.numeric(as.character(Data[1,1])),1),
           end=c(as.numeric(as.character(Data[nrow(Data),1])),12),
           frequency=12)
  return(y.ts)
}
convertCensusBureauRetailDataTo_xts<-function(Data=getCensusBureauRetailDataRecovered())
{
  require(xts)
  y<-unlist(lapply(as.list(1:nrow(Data)),function(x) {unlist(as.numeric(Data[x,2:ncol(Data)]))}))
  y.xts<-xts(y,order.by=seq(as.Date(paste0(as.character(Data[1,1]),"-01-01")),
                           as.Date(paste0(as.character(Data[nrow(Data),1]),"-12-01")),by="1 months"))
  return(y.xts)
}
month_of_year_plot<-function(Data=convertCensusBureauRetailDataTo_xts(),title="Retail and Food Services, total")
{
  Ordered_Monthly<-as.numeric(format(index(Data),"%m"))
  Ordered_Monthly<-factor(Ordered_Monthly,levels=1:12,labels=month.abb)
  
  plot(x=Ordered_Monthly,
       y=as.numeric(Data[,1])/1000,
       xlab="Month of Year",
       ylab="Sales in Billions",
       main=title)
}
