This is a very basic template for streaming realtime data in R-Shiny.
It has two components:
-
Generating a data point every two seconds
-
Plotting the data with three packages: ggplot2, plotly and scatterD3
First, I initialize an empty dataframe as a reactiveValues
object. It receives and stores the new data points.
# initialize 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))
Then, random data is saved in a reactive values
dataframe. This values$df
will be incrementally filled every two seconds by using observeEvent()
:
observeEvent(reactiveTimer(2000)(),{ # Trigger every 2 seconds
values$df <- isolate({
# get and bind the new data
values_df <- rbind(values$df, get_new_data()) %>% filter(!is.na(x))
})
})
After receiving new data in the reactive values
dataframe, the app plots the data with three packages.
The ggplot2
example:
# 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") + 1000
y_axis_range <- c(0, 70)
values$df %>%
ggplot(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)
})