R Studio

R is a powerful tool to analyse data. Attached the step by step description how to access the data base with rStudio and create some plots

We have RStudio pre installed on the managed server and its ready to use. We also provide a REST API within R to get metadata and historical data.

Get Data from anywhere via RestAPI

Attached the step by step description to access the data base with rStudio. The data base can be accessed via an REST API from every where. The machine where this code runs can be anywhere. You need the following code and the helper function. You can directly download the helper function here. The helper function will handle the REST communication. Copy the following code to the RStudio workspace and run it.

## Install packages if you do not have it already

# Load library


## Call reading Data from NEST, has to be in the same project folder

## Your personal username and password
user = "YourUsername@nest.local"
password = "YourPassword"

allSensorMetadata <- getAllSensorMetadata(user,password)

## Individual sensor data with "numericId" ,"start date" ,"end date"
dat.single <- getSingleSensorData(user, password, "42190139", "2019-01-01", "2019-03-31")

## Data from multiple sensors with "several numericIds separated with comma" ,"start date" ,"end date"
dat.multi <- getMultiSensorData(user, password, c("3200008","100067"), "2019-01-01", "2019-03-31")

## Plot
  geom_line(aes(x=time, y=value, color=numericId))+
  facet_wrap(.~numericId, ncol=1, scales="free_y")+
  scale_x_datetime(breaks = "weeks")+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Example Query with stored procedure from managed server

The following example shows how you can use the stored procedure within the NEST TEST Database to return time series data. This only works if you are using RStudio from the Managed Server Copy the following code to the RStudio workspace and run it. You can directly download it here.

Example Code

#Load Librarys

#Connect to Database
con <- dbConnect(odbc(),Driver = "SQL Server",Server = "NEST-SQL-HIST",Database = "NestTest")

#Execute sql stored procedure
result2 <-  dbGetQuery (con, "set nocount on\n EXEC [usp].[uspSingleLine36] 

#Execute sql Query
result <- dbGetQuery(con, "SELECT TOP (1000) * FROM [dbo].[vKepMET51] where _NUMERICID=3200000 order by _TIMESTAMP desc")


plot(x= result2$`_TIMESTAMP`,
     y= result2$`401180211`,
     ylab='Aussentemperatur °C',
     xlab = 'Date and Time',
     main = 'Zeitgraf',
     col  = (rainbow(n = 4,start = 0,end = 1)))

sp3<-ggplot(result$`_TIMESTAMP`, aes(x=wt, y=mpg, color=mpg)) + geom_point()
# Gradient between n colors
sp3+scale_color_gradientn(colours = rainbow(5))