-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.R
107 lines (84 loc) · 3.06 KB
/
app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
library(shiny)
library(tidyverse)
library(plotly)
library(scatterD3)
ui <- shinyServer(fluidPage(
h1("Template for plotting real-time data in R with Shiny"),
p("Task: Every two seconds, plot a new random point."),
p("It is accomplished by combining `reactiveValues()`, an object that collects the existing and incoming data, with `observeEvent()`, that executes data fetching proccess every two seconds."
),
a(href = "https://github.com/cutterkom/r-shiny-realtime-streaming-data", "More information in the Readme file on Github."),
h2("ggplot2 Library"),
plotOutput("plot"),
h2("Plotly Library"),
plotlyOutput("plotly"),
h2("D3"),
scatterD3Output("d3")
))
server <- shinyServer(function(input, output, session) {
# helper: shut down Shiny session in RStudio, when you close the browser tab
session$onSessionEnded(stopApp)
# function to create a dataframe with column x as a random number and y as the current timestamp
get_new_data <- function() {
y = sample(10:70, 1) # draw a random number between 10 and 70
x = as.POSIXct(Sys.time(), format = "%Y-%m-%d %H%m", origin = "1970-01-01")
data.frame(x, y)
}
# initialise an empty dataframe as a reactiveValues object.
# it is going to store all upcoming new data
values <- reactiveValues(df = data.frame(x = NA, y = NA))
# call the function get_new_data() every two seconds
# and bind the resulting dataframe to the reactiveValues dataframe
observeEvent(reactiveTimer(2000)(), {
# Trigger every 2 seconds
values$df <- isolate({
# get and bind the new data
rbind(values$df, get_new_data()) %>%
filter(!is.na(x)) # filter the first value to prevent a first point in the middle of the plot
})
})
# create ggplot2 chart
output$plot <- renderPlot({
x_axis_start <-
as.POSIXct(min(values$df$x), format = "%Y-%m-%d", origin = "1970-01-01")
x_axis_end <-
as.POSIXct(min(values$df$x), format = "%Y-%m-%d", origin = "1970-01-01") + 200
y_axis_range <- c(0, 70)
ggplot(data = values$df, aes(
x = as.POSIXct(x, format = "%Y-%m-%d", origin = "1970-01-01"),
y = y
)) +
geom_point() +
expand_limits(x = c(x_axis_start, x_axis_end), y = y_axis_range)
})
# create plotly chart
output$plotly <- renderPlotly({
x_axis_start <-
as.POSIXct(min(values$df$x), format = "%Y-%m-%d", origin = "1970-01-01")
x_axis_end <-
as.POSIXct(min(values$df$x), format = "%Y-%m-%d", origin = "1970-01-01") + 200
plot_ly(
data = values$df,
x = ~ as.POSIXct(x, format = "%Y-%m-%d", origin = "1970-01-01"),
y = ~ y,
type = "scatter",
mode = "markers"
) %>%
layout(xaxis = list(
range = c(x_axis_start, x_axis_end),
type = "date"
),
yaxis = list(range = c(0, 70)))
})
# create D3 chart
output$d3 <- renderScatterD3({
scatterD3(
data = values$df,
x = x,
y = y,
xlim = c(min(values$df$x), min(values$df$x) + 200),
ylim = c(0, 70)
)
})
})
shinyApp(ui = ui, server = server)