An application programming interface (API) is a way for two or more computer programs to communicate with each other.
More information about how to amend API links can be found here. Useful introductions to using APIs in R can be found here.
11.1 Ex. Load the first 25 rows (default) of data
# install.packages(c("httr", "jsonlite"))library(httr)library(jsonlite)library(dplyr)# link to the APIapi_link <-"https://apps-st.fisheries.noaa.gov/ods/foss/afsc_groundfish_survey/"res <- httr::GET(url = api_link)# res # Test connectiondata <- jsonlite::fromJSON(base::rawToChar(res$content))# names(data)tibble::as_tibble(data$items) %>% dplyr::mutate_if(is.character, type.convert, as.is =TRUE) %>% dplyr::mutate(across(where(is.numeric), round, 3)) %>%head(3) %>% flextable::flextable() %>% flextable::theme_zebra() %>% flextable::colformat_num(x = ., j =c("year", "cruise", "species_code", "tsn", "ak_survey_id"), big.mark ="")
Load the first 25 rows (default) of data.
year
srvy
survey
survey_id
cruise
haul
stratum
station
vessel_name
vessel_id
date_time
latitude_dd
longitude_dd
species_code
common_name
scientific_name
taxon_confidence
cpue_kgha
cpue_kgkm2
cpue_kg1000km2
cpue_noha
cpue_nokm2
cpue_no1000km2
weight_kg
count
bottom_temperature_c
surface_temperature_c
depth_m
distance_fished_km
net_width_m
net_height_m
area_swept_ha
duration_hr
tsn
ak_survey_id
links
2002
AI
Aleutian Islands Bottom Trawl Survey
52
200201
6
722
307-63
Vesteraalen
94
05/17/2002 18:56:58
53.737
-167.016
95020
feathery bryozoan
Eucratea loricata
Low
0.017
1.749
1,749.445
0.044
0
4.1
5.3
187
1.561
16.112
7.25
2.515
0.28
155809
1917453
[[data.frame]]
2002
AI
Aleutian Islands Bottom Trawl Survey
52
200201
6
722
307-63
Vesteraalen
94
05/17/2002 18:56:58
53.737
-167.016
79000
squid unid.
Decapodiformes
High
0.022
2.227
2,226.567
3.181
318.081
318,080.93
0.056
8
4.1
5.3
187
1.561
16.112
7.25
2.515
0.28
1917454
[[data.frame]]
2002
AI
Aleutian Islands Bottom Trawl Survey
52
200201
6
722
307-63
Vesteraalen
94
05/17/2002 18:56:58
53.737
-167.016
24191
shortfin eelpout
Lycodes brevipes
High
0.036
3.578
3,578.410
0.795
79.520
79,520.23
0.090
2
4.1
5.3
187
1.561
16.112
7.25
2.515
0.28
165258
1917455
[[data.frame]]
# install.packages(c("httr", "jsonlite"))library(httr)library(jsonlite)library(dplyr)# link to the APIapi_link <-"https://apps-st.fisheries.noaa.gov/ods/foss/afsc_groundfish_survey/"res <- httr::GET(url = api_link)# res # Test connectiondata <- jsonlite::fromJSON(base::rawToChar(res$content))# names(data)tibble::as_tibble(data$items) %>% dplyr::mutate_if(is.character, type.convert, as.is =TRUE) %>% dplyr::mutate(across(where(is.numeric), round, 3)) %>%head(3) %>% flextable::flextable() %>% flextable::theme_zebra() %>% flextable::colformat_num(x = ., j =c("year", "cruise", "species_code", "tsn", "ak_survey_id"), big.mark ="")
Load the first 25 rows (default) of data.
year
srvy
survey
survey_id
cruise
haul
stratum
station
vessel_name
vessel_id
date_time
latitude_dd
longitude_dd
species_code
common_name
scientific_name
taxon_confidence
cpue_kgha
cpue_kgkm2
cpue_kg1000km2
cpue_noha
cpue_nokm2
cpue_no1000km2
weight_kg
count
bottom_temperature_c
surface_temperature_c
depth_m
distance_fished_km
net_width_m
net_height_m
area_swept_ha
duration_hr
tsn
ak_survey_id
links
2002
AI
Aleutian Islands Bottom Trawl Survey
52
200201
6
722
307-63
Vesteraalen
94
05/17/2002 18:56:58
53.737
-167.016
95020
feathery bryozoan
Eucratea loricata
Low
0.017
1.749
1,749.445
0.044
0
4.1
5.3
187
1.561
16.112
7.25
2.515
0.28
155809
1917453
[[data.frame]]
2002
AI
Aleutian Islands Bottom Trawl Survey
52
200201
6
722
307-63
Vesteraalen
94
05/17/2002 18:56:58
53.737
-167.016
79000
squid unid.
Decapodiformes
High
0.022
2.227
2,226.567
3.181
318.081
318,080.93
0.056
8
4.1
5.3
187
1.561
16.112
7.25
2.515
0.28
1917454
[[data.frame]]
2002
AI
Aleutian Islands Bottom Trawl Survey
52
200201
6
722
307-63
Vesteraalen
94
05/17/2002 18:56:58
53.737
-167.016
24191
shortfin eelpout
Lycodes brevipes
High
0.036
3.578
3,578.410
0.795
79.520
79,520.23
0.090
2
4.1
5.3
187
1.561
16.112
7.25
2.515
0.28
165258
1917455
[[data.frame]]
11.2 Ex. Load the first 10,000 rows of data
# Not run because too big:res <- httr::GET(url =paste0(api_link, "?offset=0&limit=10000"))data <- jsonlite::fromJSON(base::rawToChar(res$content))print(paste0("rows: ", dim(data$items)[1], "; cols: ", dim(data$items)[2]))