Pulling in BEA API Data Into R

I have begun writing a script that I would like to pass on.  It pulls in GDP by MSA data using the BEA’s API into R.  This is the beginning of an analysis I am hoping to preform.  Hopefully others find this helpful.

# Load the needed libraries

# Set my BEA API key
bea.api.url <- "http://www.bea.gov/api/data"

# Read in the FIPS codes for the MSA's
msa = read.csv("bea_msa_fips.csv",head=TRUE,sep=",")
fips = msa$FIPS

# Create data frame to hold the API results
df <- data.frame()

# Loop through the MSA's to request the MSA GDP
for (i in 1:length(fips))
# Make the request to the API
req <- paste("http://www.bea.gov/api/data/?&UserID=", bea.api.key, "&method=GetData&datasetname=RegionalData&KeyCode=GDP_MP&GeoFIPS=", fips[i], "&ResultFormat=json", sep="")

# Parse the JSON response
res <- fromJSON(getURL(req))

# Since there are multiple years in the response we need to loop through them
for (j in 1:length(res$BEAAPI$Results$Data))
# Append the API result to the data frame
r <- as.data.frame(res$BEAAPI$Results$Data[j])
df <- rbind(df,r)

msa.gdp <- dcast(df, GeoName + GeoFips ~ TimePeriod, value.var="DataValue")

If you are interested in using this you will need this csv.