<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/visualizing-streaming-data-and-alert-notification-with-shiny/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/visualizing-streaming-data-with-shiny/"> DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Streaming Data is data that is generated continuously and it includes various sources such as sensors, log files, geospatial services, etc. The data may come at regular intervals and we may want to have a dashboard which updates by itself and incorporates the newly added data so that we can use it for deta driven decision making. For example in IOT, it can help to monitor sensors which are failing or sensors which report abnormal readings.</p>
<p>This week short blog post is on visualizing streaming data using shiny. The dashboard updates automatically to incorporate newly added data. In the end of this post you will find a video tutorial on how to send text and email alerts, based on streaming sensor data, when there is an abnormal reading or when a sensor fails.</p>
<p><a title="" data-rel="lightbox-0" target="_blank" rel="nofollow" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/02/streaming_shiny.gif?ssl=1"><img alt="" class="wp-image-5518" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/02/streaming_shiny.gif?resize=456%2C205&ssl=1" data-recalc-dims="1"></a></p>
<pre>
library(ggplot2)
library(dplyr)
data(&quot;diamonds&quot;)

while(TRUE){
        temp=sample_frac(diamonds,0.1)
        write.csv(temp, paste0(&quot;sampled&quot;, gsub(&quot;[^0-9]&quot;,&quot;&quot;,Sys.time()),&quot;.csv&quot;),
                  row.names = FALSE)
        Sys.sleep(10) # Suspend execution of R expressions. The time interval to suspend execution for, in seconds.

}
</pre>
<p>Then, the following are the shiny code that fetch the newly generated data and create a shiny dashboard.</p>
<p><strong>ui.R</strong></p>
<pre>
library(shiny)
fluidPage(
        tags$h2(&quot;Visualizing Streaming Data with Shiny&quot;,
                style=&quot;color:blue;text-align:center&quot;),
        plotOutput(&quot;plot1&quot;,height = &quot;600px&quot;)
     )
</pre>
<p><strong>server.R</strong></p>
<pre>
library(shiny)
library(data.table)
library(ggplot2)
library(gridExtra)
library(readr)
IsThereNewFile=function(){  #  cheap function whose values over time will be tested for equality;
                            #  inequality indicates that the underlying value has changed and needs to be 
                            #  invalidated and re-read using valueFunc
        filenames &lt;- list.files(pattern=&quot;*.csv&quot;, full.names=TRUE)
        length(filenames)
}
ReadAllData=function(){ # A function that calculates the underlying value
        filenames &lt;- list.files(pattern=&quot;*.csv&quot;, full.names=TRUE)
       read_csv(filenames[length(filenames)])
              }
function(input, output, session) {
 sampled_data &lt;- reactivePoll(10, session,IsThereNewFile, ReadAllData)    
         # 10: number of milliseconds to wait between calls to checkFunc
output$plot1&lt;-renderPlot({     
        sampled_data=  sampled_data()
        g1= ggplot(sampled_data, aes(depth, fill = cut, colour = cut)) +
                geom_density(alpha = 0.1) +xlim(55, 70)+ggtitle(&quot;Distribution of Depth by Cut&quot;)+
                theme(plot.title = element_text(color=&quot;darkred&quot;,size=18,hjust = 0.5),
                     axis.text.y = element_text(color=&quot;blue&quot;,size=12,hjust=1),
                      axis.text.x = element_text(color=&quot;darkred&quot;,size=12,hjust=.5,vjust=.5),
                      axis.title.x = element_text(color=&quot;red&quot;, size=14),
                      axis.title.y = element_text(size=14))     
       g2=ggplot(sampled_data, aes(carat, ..count.., fill = cut)) +
                geom_density(position = &quot;stack&quot;)+ggtitle(&quot;Total Carat by Count&quot;)+
                theme(plot.title = element_text(color=&quot;purple&quot;,size=18,hjust = 0.5),
                      axis.text.y = element_text(color=&quot;blue&quot;,size=12,hjust=1),
                      axis.text.x = element_text(color=&quot;darkred&quot;,size=12,hjust=.5,vjust=.5),
                      axis.title.x = element_text(color=&quot;red&quot;, size=14),
                      axis.title.y = element_text(size=14))
       g3=ggplot(sampled_data, aes(carat, ..count.., fill = cut)) +
                geom_density(position = &quot;fill&quot;)+ggtitle(&quot;Conditional Density Estimate&quot;)+
                theme(plot.title = element_text(color=&quot;black&quot;,size=18,hjust = 0.5),
                      axis.text.y = element_text(color=&quot;blue&quot;,size=12,hjust=1),
                      axis.text.x = element_text(color=&quot;darkred&quot;,size=12,hjust=.5,vjust=.5),
                      axis.title.x = element_text(color=&quot;red&quot;, size=14),
                      axis.title.y = element_text(size=14))
       g4=ggplot(sampled_data,aes(carat,price))+geom_boxplot()+facet_grid(.~cut)+
                ggtitle(&quot;Price by Carat for each cut&quot;)+
                theme(plot.title = element_text(color=&quot;darkblue&quot;,size=18,hjust = 0.5),
                      axis.text.y = element_text(color=&quot;blue&quot;,size=12,hjust=1),
                      axis.text.x = element_text(color=&quot;darkred&quot;,size=12,hjust=.5,vjust=.5),
                      axis.title.x = element_text(color=&quot;red&quot;, size=14),
                      axis.title.y = element_text(size=14))
        grid.arrange(g1,g2,g3,g4)
})
}</pre>
<ul>
<li><a target="_blank" rel="nofollow" href="https://www.youtube.com/watch?v=0xZZC8SAxAQ">Email and Text Alerts Based on Streaming Sensor Data</a></li>
</ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a title="Permanent Link toMetro Systems Over Time: Part 3" target="_blank" rel="nofollow" href="https://datascienceplus.com/metro-systems-over-time-part-3/">Metro Systems Over Time: Part 3</a></li>
<li><a title="Permanent Link toMetro Systems Over Time: Part 2" target="_blank" rel="nofollow" href="https://datascienceplus.com/metro-systems-over-time-part-2/">Metro Systems Over Time: Part 2</a></li>
<li><a title="Permanent Link toMetro Systems Over Time: Part 1" target="_blank" rel="nofollow" href="https://datascienceplus.com/metro-systems-over-time-part-1/">Metro Systems Over Time: Part 1</a></li>
<li><a title="Permanent Link toOutlier App: An Interactive Visualization of Outlier Algorithms" target="_blank" rel="nofollow" href="https://datascienceplus.com/outlier-app-an-interactive-visualization-of-outlier-algorithms/">Outlier App: An Interactive Visualization of Outlier Algorithms</a></li>
<li><a title="Permanent Link toCreating an animation using R" target="_blank" rel="nofollow" href="https://datascienceplus.com/creating-an-animation-using-r/">Creating an animation using R</a></li>
</ol></strong>
<div>
<a target="_blank" rel="nofollow" href="http://feeds.feedburner.com/~ff/datascienceplus?a=y_GuhZD5a_M:kY7vyvArgwQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/datascienceplus?i=y_GuhZD5a_M:kY7vyvArgwQ:D7DqB2pKExk" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.feedburner.com/~ff/datascienceplus?a=y_GuhZD5a_M:kY7vyvArgwQ:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=qj6IDK7rITs" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.feedburner.com/~ff/datascienceplus?a=y_GuhZD5a_M:kY7vyvArgwQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=yIl2AUoC8zA" border="0"></a>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://datascienceplus.com/visualizing-streaming-data-with-shiny/"> DataScience+</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/OgO2Q9Jrnlc"></a></div>