Retrieving data in R¶
We provide an R function, query, that fetches time series data from InfluxDB server and returns data.frame object.
Setting up¶
The query function uses httr, an HTTP client library for R. Please install its ssl dependency beforehand. On Debian-based systems:
$ sudo apt-get install libssl-dev
Assuming that you’ve already installed r-base and RStudio (external instructions), you need to install httr. Please execute the following inside your RStudio environment to install httr:
> install.packages("httr")
We need two additional packages for the demonstration, devtools to fetch the query source code (api-v1.3.r) from our gist repository and optional data.table to cast the data into columns. Of course, you can skip this step, and download the source code into your environment.
> install.packages("devtools")
> install.packages("data.table")
Provided function¶
query(…)
Download¶
to download the source of api-v1.3.r, please use the
Raw
orDownload ZIP
button on the GitHubGist page
Description¶
fetches time series data with matching parameters from the database server and converts into data.frame object
Usage¶
query(domain, apiKey, timeFilter = "", device = "//", location = "//", sensor = "//", includeNetworkSensors = FALSE, channel = "//", aggFunc = "", aggInterval = "", doCast = TRUE, castAggFunc = mean, convertTimestamp = TRUE, timezone = "UTC")
Arguments¶
- domain
The domain part of the URL
- apiKey
Secret key for authenticating with the server
- timeFilter
Time filter expression written in InfluxQL, the default value
""
ignores time- device
InfluxQL regular expression to select devices, the default value
"//"
matches all devices- location
InfluxQL regular expression to select locations, the default value
"//"
matches all locations- sensor
InfluxQL regular expression to select sensors, the default value
"//"
matches all sensors- includeNetworkSensors
TRUE
allows returning network-related data
FALSE
(default)filters out all network-related data
- channel
InfluxQL regular expression to select channels, the default value
"//"
matches all channels- aggFunc
InfluxQL aggregate function evaluated on the selected data, no aggregate function is applied by default
- aggInterval
InfluxQL time interval value, used with aggFunc
- doCast
TRUE
(default)return time series into individual columns,
FALSE
return all time series in a single column
- castAggFunc
Apply aggregate function when casting data, by default
mean
function is applied- convertTimestamp
TRUE
(default)convert timestamps into date time objects,
FALSE
return timestamps in unix time in milliseconds
- timezone
Timezone name, used with convertTimestamp
Value¶
data.frame object containing time series data
Demonstration script¶
The following R script demonstrates the main functionality of query function with our demo.decentlab.com server. In this script, we use the previously installed devtools to source query from our gist repository and enable casting so that the output data can be column oriented. Please note that the casting requires the data.table package.
library(devtools)
devtools::source_gist('79f52bef3778e0dbbd5dc58437621d88',
filename = 'api-v1.3.r')
Please remove the filename parameter if above command fails.
data.df <- query(
domain = "demo.decentlab.com",
apiKey = "eyJrIjoiclhMRFFvUXFzQXpKVkZydm52b0VMRVg3M3U2b3VqQUciLCJuIjoiZGF0YS1xdWVyeS1hcGktZGVtby0yIiwiaWQiOjF9",
timeFilter = "time > '2020-01-01' AND time < '2020-01-05'",
device = "/^(3001|6414)$/",
sensor = "/sht30|mb7389/",
aggFunc = "mean",
aggInterval = "12h",
)
The above example will retrieve the data from demo.decentlab.com server, matching the following criteria:
devices with ID
3001
or6414
sensor names containing either
sht30
ormb7389
compute the mean of groups divided by 12-hour interval
received between 2020-01-01 and 2020-01-05
The value of data.df:
> head(data.df)
time 3001.sensirion-sht30-humidity 3001.sensirion-sht30-temperature 6414.maxbotix-mb7389-distance
1 2020-01-01 00:00:00 31.61432 20.87331 3189.880
2 2020-01-01 12:00:00 31.19008 20.90538 3184.826
3 2020-01-02 00:00:00 30.88009 20.86234 3194.190
4 2020-01-02 12:00:00 30.75082 20.89822 3188.148
5 2020-01-03 00:00:00 30.78512 20.87438 3192.785
6 2020-01-03 12:00:00 31.26576 21.04921 3182.441
6414.maxbotix-mb7389-trials
1 15
2 15
3 15
4 15
5 15
6 15
Note that the head function shows only the first six rows.