The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.
library(hakaiApi)
# Initialize the client
# follow link in console and paste auth. code in console
# (ignore alignment issue)
<- hakaiApi::Client$new() client
Here’s a simple demonstration of how to filter data from the API. The API has limits in terms of how much data you can download in one query. So, instead of querying for all data of a certain type, it’s good to narrow it down to the time period, sites, or parameters that you are really interested in. Though the API has many options for querying, filtering, and sorting data, most R users will be more comfortable filtering data using R packages such as dplyr.
A good way to build a query is in steps. Let’s say we want all the chlorophyll data from QU39, from after 2016, with only accepted values, and only glass fibre filters (GF/F) from the surface.
# First return some chl data with no filters
<- "/eims/views/output/chlorophyll"
endpoint <- client$get(paste0(client$api_root, endpoint))
all_chl # by default only 20 rows are returned
str(all_chl) # Look at what columns are available to filter on
# Narrow it down to QU39
<- "?site_id=QU39"
query $get(paste0(client$api_root, endpoint, query))
client
#Get back only accepted values
<- "?site_id=QU39&chla_flag=AV"
query $get(paste0(client$api_root, endpoint, query))
client
# Get values from 2017
<- "?site_id=QU39&chla_flag=AV&date.year=2017"
query $get(paste0(client$api_root, endpoint, query))
client
# Include only GF/F from the surface
# It may be useful to split up the queries and rejoin them over multiple lines
# Using the paste() or paste0() functions
<- paste("site_id=QU39",
query "chla_flag=AV",
"date.year=2017",
"filter_type=GF/F",
"line_out_depth=0", sep = "&")
$get(paste0(client$api_root, endpoint, "?", query))
client
# Select only the columns I'm interested in
<- paste("site_id=QU39",
query "chla_flag=AV",
"date.year=2017",
"filter_type=GF/F",
"line_out_depth=0",
"fields=date,chla,lab_technician", sep = "&")
$get(paste0(client$api_root, endpoint, "?", query))
client
# This looks good so now you can assign a variable and remove the limit
<- paste("site_id=QU39",
query "chla_flag=AV",
"date.year=2017",
"filter_type=GF/F",
"line_out_depth=0",
"fields=date,chla,lab_technician",
"limit=-1", sep = "&")
<- client$get(paste0(client$api_root, endpoint, "?", query))
a_great_chl_query
plot(a_great_chl_query$date,
$chla,
a_great_chl_queryxlim = as.Date(c("2017-01-01", "2018-01-01")),
ylim = c(0, 5))
Let’s say you’re only interested in receiving the highest 10 values for chlorophyll from 2017 from the portal. We can do that with the API as well using the sort descending capability and limiting the return to only 10 values.
<- "/eims/views/output/chlorophyll"
endpoint <- paste("fields=date,chla,site_id,line_out_depth",
query "chla>0", # Note: added chla>0 to remove NA values
"date.year=2017",
"sort=-chla",
"limit=10", sep = "&")
<- client$get(paste0(client$api_root, endpoint, "?", query)) top_10_chl
For more great querying capabilities see the querying-data docs
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.