1- Run the generate_stream_data.R from Terminal 2- Run shiny app

1 Generate and save randomly dimonds.csv file

2 server.R

library(shiny)
library(data.table)
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
library(ggplot2)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
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 <- list.files(pattern="*.csv", full.names=TRUE)
  return(length(filenames))
}



ReadAllData  <- function(){ 
  # A function that calculates the underlying value
  filenames <- list.files(pattern="*.csv", full.names = TRUE)
  read_csv(filenames[length(filenames)])
}



function(input, output, session) {

  # 10: number of milliseconds to wait between calls to checkFunc
  sampled_data <- reactivePoll(10, session, IsThereNewFile, ReadAllData)    
  
  output$plot1 <- renderPlot({
    
    sample_data <-  sampled_data()
    
    g1 <- ggplot(sample_data, aes(depth, fill = cut, colour = cut)) +
      geom_density(alpha = 0.1) +xlim(55, 70) +
      ggtitle("Distribution of Depth by Cut") +
      theme(plot.title = element_text(color="darkred",size=18,hjust = 0.5),
            axis.text.y = element_text(color="blue",size=12,hjust=1),
            axis.text.x = element_text(color="darkred",size=12,hjust=.5,vjust=.5),
            axis.title.x = element_text(color="red", size=14),
            axis.title.y = element_text(size=14))
    
    g2 <- ggplot(sample_data, aes(carat, ..count.., fill = cut)) +
      geom_density(position = "stack") + 
      ggtitle("Total Carat by Count") +
      theme(plot.title = element_text(color="purple",size=18,hjust = 0.5),
            axis.text.y = element_text(color="blue",size=12,hjust=1),
            axis.text.x = element_text(color="darkred",size=12,hjust=.5,vjust=.5),
            axis.title.x = element_text(color="red", size=14),
            axis.title.y = element_text(size=14))
    
    g3 <- ggplot(sample_data, aes(carat, ..count.., fill = cut)) +
      geom_density(position = "fill") + 
      ggtitle("Conditional Density Estimate") +
      theme(plot.title = element_text(color="black",size=18,hjust = 0.5),
            axis.text.y = element_text(color="blue",size=12,hjust=1),
            axis.text.x = element_text(color="darkred",size=12,hjust=.5,vjust=.5),
            axis.title.x = element_text(color="red", size=14),
            axis.title.y = element_text(size=14))
    
    g4 <- ggplot(sample_data,aes(carat,price)) + 
       geom_boxplot() +
      facet_grid(.~cut) +
      ggtitle("Price by Carat for each cut") +
      theme(plot.title = element_text(color="darkblue",size=18,hjust = 0.5),
            axis.text.y = element_text(color="blue",size=12,hjust=1),
            axis.text.x = element_text(color="darkred",size=12,hjust=.5,vjust=.5),
            axis.title.x = element_text(color="red", size=14),
            axis.title.y = element_text(size=14))
    
    grid.arrange(g1,g2,g3,g4)
  })
}
## function(input, output, session) {
## 
##   # 10: number of milliseconds to wait between calls to checkFunc
##   sampled_data <- reactivePoll(10, session, IsThereNewFile, ReadAllData)    
##   
##   output$plot1 <- renderPlot({
##     
##     sample_data <-  sampled_data()
##     
##     g1 <- ggplot(sample_data, aes(depth, fill = cut, colour = cut)) +
##       geom_density(alpha = 0.1) +xlim(55, 70) +
##       ggtitle("Distribution of Depth by Cut") +
##       theme(plot.title = element_text(color="darkred",size=18,hjust = 0.5),
##             axis.text.y = element_text(color="blue",size=12,hjust=1),
##             axis.text.x = element_text(color="darkred",size=12,hjust=.5,vjust=.5),
##             axis.title.x = element_text(color="red", size=14),
##             axis.title.y = element_text(size=14))
##     
##     g2 <- ggplot(sample_data, aes(carat, ..count.., fill = cut)) +
##       geom_density(position = "stack") + 
##       ggtitle("Total Carat by Count") +
##       theme(plot.title = element_text(color="purple",size=18,hjust = 0.5),
##             axis.text.y = element_text(color="blue",size=12,hjust=1),
##             axis.text.x = element_text(color="darkred",size=12,hjust=.5,vjust=.5),
##             axis.title.x = element_text(color="red", size=14),
##             axis.title.y = element_text(size=14))
##     
##     g3 <- ggplot(sample_data, aes(carat, ..count.., fill = cut)) +
##       geom_density(position = "fill") + 
##       ggtitle("Conditional Density Estimate") +
##       theme(plot.title = element_text(color="black",size=18,hjust = 0.5),
##             axis.text.y = element_text(color="blue",size=12,hjust=1),
##             axis.text.x = element_text(color="darkred",size=12,hjust=.5,vjust=.5),
##             axis.title.x = element_text(color="red", size=14),
##             axis.title.y = element_text(size=14))
##     
##     g4 <- ggplot(sample_data,aes(carat,price)) + 
##        geom_boxplot() +
##       facet_grid(.~cut) +
##       ggtitle("Price by Carat for each cut") +
##       theme(plot.title = element_text(color="darkblue",size=18,hjust = 0.5),
##             axis.text.y = element_text(color="blue",size=12,hjust=1),
##             axis.text.x = element_text(color="darkred",size=12,hjust=.5,vjust=.5),
##             axis.title.x = element_text(color="red", size=14),
##             axis.title.y = element_text(size=14))
##     
##     grid.arrange(g1,g2,g3,g4)
##   })
## }

3 ui.R

fluidPage(
  tags$h2("Visualizing Streaming Data with Shiny",
          style="color:blue;text-align:center"),
  plotOutput("plot1", height = "600px")
)

Visualizing Streaming Data with Shiny