R example scriptΒΆ

This tutorial shows you how to get started using Frost in R. The snippets of code on this page can be put together to make your own script for extracting data.

First and most importantly, the client ID must be defined to be able to authenticate the requests.

# Library needed (jsonlite must be installed in R)
library(jsonlite)

# Insert your own client ID here
client_id = '<INSERT CLIENT ID HERE>'

In the next block of code, data is retrieved from Frost using the fromJSON function. First we build the correct URL to a Frost endpoint using parameters for sources, elements and dates.

# Define andpoint and parameters
endpoint <- paste0("https://", client_id, "@frost.met.no/observations/v0.jsonld", collapse=NULL)
sources <- 'SN18700,SN90450'
elements <- 'mean(air_temperature P1D),sum(precipitation_amount P1D),mean(wind_speed P1D)'
referenceTime <- '2010-04-01/2010-04-03'
# Build the URL to Frost
url <- paste0(
    endpoint, "?",
    "sources=", sources,
    "&referencetime=", referenceTime,
    "&elements=", elements,
    collapse=NULL
)
# Issue an HTTP GET request and extract JSON data
xs <- try(fromJSON(URLencode(url),flatten=T))

The above code will give an error output if something went wrong with the request. The block below will also make sure that there is valid data in the request.

# Check if the request worked, print out any errors
if (class(xs) != 'try-error') {
    print("Data retrieved from frost.met.no!")
    data <- xs$data
} else {
    print("Error: the data retrieval was not successful!")
}
[1] "Data retrieved from frost.met.no!"

If the above block returned an error, then the rest cannot be run. If it printed out a success, then we are all set.

Below, we insert the observation data into a table format. This is useful for doing all kinds of analysis on the returned data.

Note: the block below is tailored to the observations endpoint, and changes would need to be made to accomodate other endpoints.

# This will return a Dataframe with all of the observations in a table format
df <- data.frame()
for (i in 1:length(data$observations)) {
    row <- data$observations[[i]]
    row$sourceId <- data$sourceId[[i]]
    row$referenceTime <- data$referenceTime[[i]]
    df <- rbind(df, row)
}

You can run the line below to get a preview of the data frame table.

head(df)
elementIdvalueunittimeOffsettimeResolutiontimeSeriesIdperformanceCategoryexposureCategoryqualityCodelevel.levelTypelevel.unitlevel.valuesourceIdreferenceTime
mean(air_temperature P1D) 3.2 degC PT0H P1D 0 A 1 2 height_above_ground m 2 SN18700:0 2010-04-01T00:00:00.000Z
mean(air_temperature P1D) 3.0 degC PT6H P1D 0 A 1 2 height_above_ground m 2 SN18700:0 2010-04-01T00:00:00.000Z
sum(precipitation_amount P1D)13.5 mm PT18H P1D 0 A 1 2 NA NA NA SN18700:0 2010-04-01T00:00:00.000Z
sum(precipitation_amount P1D)29.0 mm PT6H P1D 0 A 1 2 NA NA NA SN18700:0 2010-04-01T00:00:00.000Z
mean(wind_speed P1D) 1.7 m/s PT0H P1D 0 A 1 2 height_above_ground m 10 SN18700:0 2010-04-01T00:00:00.000Z
mean(air_temperature P1D) 3.0 degC PT0H P1D 0 A 1 2 height_above_ground m 2 SN18700:0 2010-04-02T00:00:00.000Z

To make a shorter and more readable table, you can use the code below.

# These additional columns will be kept
columns <- c("sourceId","referenceTime","elementId","value","unit","timeOffset")
df2 <- df[columns]
# Convert the time value to something R understands
df2$referenceTime <- as.Date(df2$referenceTime)
# Preview the result
head(df2)
sourceIdreferenceTimeelementIdvalueunittimeOffset
SN18700:0 2010-04-01 mean(air_temperature P1D) 3.2 degC PT0H
SN18700:0 2010-04-01 mean(air_temperature P1D) 3.0 degC PT6H
SN18700:0 2010-04-01 sum(precipitation_amount P1D)13.5 mm PT18H
SN18700:0 2010-04-01 sum(precipitation_amount P1D)29.0 mm PT6H
SN18700:0 2010-04-01 mean(wind_speed P1D) 1.7 m/s PT0H
SN18700:0 2010-04-02 mean(air_temperature P1D) 3.0 degC PT0H