It may be useful to download data from openSenseMap only once. For reproducible results, the data should be saved to disk, and reloaded at a later point.
This avoids..
This vignette shows how to use this built in
opensensmapr
feature, and how to do it yourself in case you
want to save to other data formats.
# this vignette requires:
library(opensensmapr)
library(jsonlite)
library(readr)
All data retrieval functions of opensensmapr
have a
built in caching feature, which serializes an API response to disk.
Subsequent identical requests will then return the serialized data
instead of making another request.
To use this feature, just add a path to a directory to the
cache
parameter:
= osem_boxes(grouptag = 'ifgi', cache = tempdir())
b
# the next identical request will hit the cache only!
= osem_boxes(grouptag = 'ifgi', cache = tempdir())
b
# requests without the cache parameter will still be performed normally
= osem_boxes(grouptag = 'ifgi') b
Looking at the cache directory we can see one file for each request, which is identified through a hash of the request URL:
list.files(tempdir(), pattern = 'osemcache\\..*\\.rds')
## [1] "osemcache.17db5c57fc6fca4d836fa2cf30345ce8767cd61a.rds"
You can maintain multiple caches simultaneously which allows to only store data related to a script in the same directory:
= getwd() # current working directory
cacheDir = osem_boxes(grouptag = 'ifgi', cache = cacheDir)
b
# the next identical request will hit the cache only!
= osem_boxes(grouptag = 'ifgi', cache = cacheDir) b
To get fresh results again, just call osem_clear_cache()
for the respective cache:
osem_clear_cache() # clears default cache
osem_clear_cache(getwd()) # clears a custom cache
If you want to roll your own serialization method to support custom data formats, here’s how:
# first get our example data:
= osem_measurements('Windgeschwindigkeit') measurements
If you are paranoid and worry about .rds
files not being
decodable anymore in the (distant) future, you could serialize to a
plain text format such as JSON. This of course comes at the cost of
storage space and performance.
# serializing senseBoxes to JSON, and loading from file again:
write(jsonlite::serializeJSON(measurements), 'measurements.json')
= jsonlite::unserializeJSON(readr::read_file('measurements.json'))
measurements_from_file class(measurements_from_file)
## [1] "osem_measurements" "tbl_df" "tbl"
## [4] "data.frame"
This method also persists the R object metadata (classes, attributes). If you were to use a serialization method that can’t persist object metadata, you could re-apply it with the following functions:
# note the toJSON call instead of serializeJSON
write(jsonlite::toJSON(measurements), 'measurements_bad.json')
= jsonlite::fromJSON('measurements_bad.json')
measurements_without_attrs class(measurements_without_attrs)
## [1] "data.frame"
= osem_as_measurements(measurements_without_attrs)
measurements_with_attrs class(measurements_with_attrs)
## [1] "osem_measurements" "tbl_df" "tbl"
## [4] "data.frame"
The same goes for boxes via osem_as_sensebox()
.