library(tidycensus)
library(tidyverse)


#log on with API
census_api_key("7a853acf81fd5758228680556ac831138c40b83e")



callData = function(year,table,municipality) {
  
  #year is between 2000 and 2020
  #table is dp02pr, dp03, dp04, dp05
  
  #load variables
  variables = load_variables(year,"acs5/profile")
  #load variable vectors
  codes = variables$name
  codesBool = startsWith(codes,table)
  
  codes = codes[codesBool]
  labels = variables$label[codesBool]
  
  #pull table
  bigTable = get_acs(geography = "county",
                 state = "PR",
                 year = year,
                 county = municipality,
                 variables = codes)
  
  bigTable$Label = labels
  bigTable = bigTable[c("Label","estimate","moe")]
  return(bigTable)
}

table = callData(2020,"DP05","Aguada")

