-
Notifications
You must be signed in to change notification settings - Fork 1
/
nmfsTradeData.R
50 lines (42 loc) · 1.99 KB
/
nmfsTradeData.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
nmfsTradeData <- function(prodType = "GROUNDFISH", tradeType = "EXP",
fromYr = 2015, toYr = 2019, checkName = TRUE){
#
require(httr)
require(jsonlite)
require(dplyr)
if(!tradeType%in%c("EXP","IMP","REX")){
stop("tradeType must be 'EXP' (exports),'IMP' (imports), or 'REX' (re-exports).")
}
prodType <- toupper(prodType)
if(checkName){
source("./PRODUCTNAMES.R")
if(!prodType%in%.productNames)
stop("The product type was not found in the list of potential product types.\\
See product types at https://foss.nmfs.noaa.gov/apexfoss/f?p=215:2:15168872762911::NO::: ")
}
datList <- list()
for(yr in fromYr:toYr){
request <- paste0('{"name":{"$like":"%',prodType,'%"},"source":"',tradeType,'","year":"',yr,'"}')
## send the request to the NMFS server and returns a response.
response <- GET('https://www.st.nmfs.noaa.gov/ords/foss/trade_data',
query = list(q = request, limit = 10000))
## Checks for a successful reponse (any other code beside 200 is a failure). Different codes may indicate how it failed though.
if(status_code(response)!=200)
stop(paste("Request to API failed. Status code of response:",status_code(response)))
## retrives the content of the request
df <- content(response, as = "text", encoding = "UTF-8")
df <- fromJSON(df, flatten = TRUE)
df$links <- NULL
df <- df %>% data.frame()
names(df) <- sub("items.","",names(df),fixed=TRUE)
if(any(df$hasMore))
stop("The request exceeded the 10,000 observation limit and did not retrieve of the data. Try constraining your search.")
df <- df %>% transform(year=as.numeric(year), month = as.numeric(month),
links = NULL, hasMore = NULL,
limit = NULL, offset = NULL, count = NULL)
datList[[as.character(yr)]] <- df
}
dat.f <- bind_rows(datList)
dat.f <- data.frame(as.list(dat.f), stringsAsFactors=TRUE)
return(dat.f)
}