TC7.7 - How to Save resulting data via API from R Studio


Warning message

You can't delete this newsletter because it has not been sent to all its subscribers.

Test Case Title

TC7.7 - How to save resulting data via API from R Studio




Using a PC with a web browser. You need credentials to access to the Node-Red application.

The following functionalities are available only for specific Snap4city users with specific privileges.

Expected successful result





  1. Install the “httr” package with the command install.packages(“httr”) and load it with the command library(httr); install the “jsonlite” package with the command install.packages(“jsonlite”) and load it with the command library(jsonlite).


  1. Convert the list that contains all new data into a Json format with the command toJSON:

request_body_json <- toJSON (dataList, auto_unbox = TRUE)

    1. To save a single value (for example the average value of the last 2 hours for a specific variable):


  • It’is necessary to register the attributes for a specific Service Uri, creating a Json in the following format:



To create a Json in R is possible to create a list first, and then using the function toJSON to convert the list in a Json: 

register$attributes= list(4)
register$attributes[[1]]=list("valueName"= "noiseLAeqAvg2h", "valueUnit"="#", "dataType"="float", "value_refresh_rate"="null", "message"="string")

register_body_json <- toJSON (register, auto_unbox = TRUE)

Using the POST function of the httr package is possible to register the attributes:

registerPOST <- POST (url = "", body = register_body_json, encode = "json", add_headers ("Content-Type" = "application/json"))

  • To save the data, Json format must be of the following form:
{"serviceUri":"http://...","attributes":[{"valueName":"…","value": …,"valueDate":"…","valueAcqDate":"…","message":"…"}]} 

Using the POST function is possible to save the data for a specific Service Uri:

 resultPOST <- POST (url = "", body = request_body_json, encode = "json", add_headers ("Content-Type" = "application/json"))


    1. To save multiple values as trends (for example the hourly average per day of the measures related to a specific sensor category) is necessary to create the following Json:


result <- POST (url = http://..., body = request_body_json, encode = "json", add_headers("Content-Type" = "application/json"))


Note that to save data to create a heatmap the following url in the POST function has to be use


The Json (a single json for each lat and long):

{"mapName": "name",
"description": "description of the map",
"days": 0,
"clustered": 0,
"latitude": 43.1,
"longitude": 11.1,
"value": 3001,
"date": "2018-12-18T00:00:00Z"