Shiny module to render large time-series data with live server-client aggregation
rAmChartsTimeSeriesUI(id, width = "100%", height = "400px")rAmChartsTimeSeriesServer(
input,
output,
session,
data,
col_date,
col_series,
maxPoints = shiny::reactive(600),
tz = shiny::reactive("UTC"),
ts = shiny::reactive(c("5 min", "10 min", "30 min", "hour", "3 hour", "12 hour", "day",
"week", "month", "year")),
fun_aggr = shiny::reactive("mean"),
treat_missing = shiny::reactive(FALSE),
maxgap = shiny::reactive(Inf),
type_aggr = shiny::reactive("first"),
na.rm = shiny::reactive(TRUE),
main = shiny::reactive(""),
ylab = shiny::reactive(""),
color = shiny::reactive(c("#2E2EFE", "#31B404", "#FF4000", "#AEB404")),
type = shiny::reactive(c("line")),
bullet = shiny::reactive(NULL),
bulletSize = shiny::reactive(2),
linetype = shiny::reactive(c(0, 5, 10, 15, 20)),
linewidth = shiny::reactive(c(1, 1, 1, 1, 1, 1)),
fillAlphas = shiny::reactive(0),
precision = shiny::reactive(1),
connect = shiny::reactive(FALSE),
export = shiny::reactive(FALSE),
legend = shiny::reactive(TRUE),
legendPosition = shiny::reactive("bottom"),
legendHidden = shiny::reactive(FALSE),
ZoomButton = shiny::reactive(data.frame(Unit = "MAX", multiple = 1, label = "All")),
ZoomButtonPosition = shiny::reactive("bottom"),
periodFieldsSelection = shiny::reactive(FALSE),
scrollbar = shiny::reactive(TRUE),
scrollbarPosition = shiny::reactive("bottom"),
scrollbarHeight = shiny::reactive(40),
scrollbarGraph = shiny::reactive(NULL),
cursor = shiny::reactive(TRUE),
cursorValueBalloonsEnabled = shiny::reactive(TRUE),
creditsPosition = shiny::reactive("top-right"),
group = shiny::reactive(NULL),
dataDateFormat = shiny::reactive("YYYY-MM-DD JJ:NN:ss"),
categoryBalloonDateFormats = shiny::reactive(list(list(period = "YYYY", format =
"YYYY"), list(period = "MM", format = "YYYY-MM"), list(period = "WW", format =
"YYYY-MM-DD"), list(period = "DD", format = "YYYY-MM-DD"), list(period = "hh", format
= "YYYY-MM-DD JJ:NN"), list(period = "mm", format = "YYYY-MM-DD JJ:NN"), list(period
= "ss", format = "YYYY-MM-DD JJ:NN:ss"), list(period = "fff", format =
"YYYY-MM-DD JJ:NN:ss"))),
dateFormats = shiny::reactive(list(list(period = "YYYY", format = "YYYY"), list(period
= "MM", format = "MMM"), list(period = "WW", format = "MMM DD"), list(period = "DD",
format = "MMM DD"), list(period = "hh", format = "JJ:NN"), list(period = "mm", format
= "JJ:NN"), list(period = "ss", format = "JJ:NN:ss"), list(period = "fff", format =
"JJ:NN:ss"))),
thousandsSeparator = shiny::reactive(" "),
decimalSeparator = shiny::reactive("."),
balloonFontSize = shiny::reactive(10),
balloonMaxWidth = shiny::reactive(400)
)
a reactive expression with aggregate data and ts
character, used to specify namesapce, see shiny::NS
character, the width of the chart container. For amChartsOutput.
character, the height of the chart container. For amChartsOutput.
standard, shiny input
standard, shiny output
standard, shiny session
: data.frame to transform.
Date column name, default to "date". Must be "POSIXct"
Column name of quantitative variable(s) to be transformed. Default to setdiff(colnames(data), "date")
: Maximal number of rows in results
: Timezone of result. Defaut to "UTC".
All enabled aggregation. Default to c("5 min", "10 min", "30 min", "hour", "3 hour", "12 hour", "day", "week", "month", "year"). Can be a number, in seconds, or a character string containing one of "min", "hour", "day".... This can optionally be preceded by a positive integer and a space
: Aggregation function to use ("min", "max", "sum", "mean", "first", "last", "minabs", "maxabs"). Default to "mean".
: Boolean. Default to FALSE
Whether or not to interpolate missing values ?
see na.approx
When interpolate missing values with na.approx.
Maximum number of consecutive NAs to fill. Defaut to Inf.
character Type of aggregation
"first" : Date/Time result is equal to minimum of sequence, and this minimum is included in aggregation
"last" : Date/Time result is equal to maximum of sequence, and this maximum is included in aggregation
: aggregation only. a logical value indicating whether NA values should be stripped before the computation proceeds.
character, title.
character, value axis label.
character, color of series (in hexadecimal).
character, Type of graph. Possible values are : "line" (default),
"column", "step", "smoothedLine"
character, point shape. Possible values are : "diamond", "square",
"bubble", "yError", "xError", "round", "triangleLeft", "triangleRight", "triangleUp"
numeric, size of bullet.
numeric, line type, 0 : solid, number : dashed length
numeric, line width.
numeric, fill. Between 0 (no fill) to 1.
numeric, default set to 1.
logical, default set to FALSE. Specifies whether to connect data points if data is missing.
logical, default set to FALSE. TRUE to display export feature.
logical, enabled or not legend ? Defaut to TRUE.
character, legend position. Possible values are :
"left", "right", "bottom", "top"
logical hide some series on rendering ? Defaut to FALSE
data.frame, 3 or 4 columns :
"Unit" : Character. Times unit. 'ss', 'mm', 'hh', 'DD', 'MM', 'YYYY'
"multiple" : Numeric. multiple*unit
"label" : Character. button's label
"selected" : Boolean. Optional. To set initial selection. (One TRUE, others FALSE)
character, zoom button position. Possible values are :
"left", "right", "bottom", "top"
boolean, using zoom button, add also two fields to select period ?
boolean, enabled or not scrollbar ? Defaut to TRUE.
character, scrollbar position. Possible values are :
"left", "right", "bottom", "top"
numeric, height of scroll bar. Default : 40.
character, name of serie (column) to print in scrollbar. Defaut to NULL.
boolean, enabled or not cursor ? Defaut to TRUE.
boolean, if cursor, enabled or not balloons on cursor ? Defaut to TRUE.
character, credits position. Possible values are :
"top-right", "top-left", "bottom-right", "bottom-left"
character, like in dygraphs, for synchronization in shiny or rmarkdown.
character Data date format. Default to 'YYYY-MM-DD JJ:NN:ss'. See amTimeSeries.
list Date format objects for chart cursor. See amTimeSeries.
list Date format objects for x-axis. See amTimeSeries.
character, default set to " "
character, default set to ".",
numeric, text font size on balloon. Default : 10.
numeric. Default : 400.
if (FALSE) {
library(shiny)
library(rAmCharts)
library(data.table)
# number of points
n <- 1000000
data <- data.frame(date = seq(c(ISOdate(1999,12,31)), by = "5 min", length.out = n),
value = rnorm(n, 100, 50), check.names = FALSE)
# maximun of points in javascript
max_points <- 1000
# Call module in UI
ui <- fluidPage(
rAmChartsTimeSeriesUI("ts_1", height = "600px"),
h4(textOutput("ts"))
)
# Define server
server <- function(input, output) {
# Call module in server
res <- callModule(rAmChartsTimeSeriesServer, "ts_1", reactive(data), reactive("date"),
reactive("value"), maxPoints = shiny::reactive(max_points),
main = reactive("Example of rAmChartsTimeSeries module"),
color = reactive("red"), periodFieldsSelection = reactive(TRUE)
)
# show module return and print ts
output$ts <- renderText({
print(res())
paste0("Current ts : ", res()$ts)
})
}
# Run the application
shinyApp(ui = ui, server = server)
}
Run the code above in your browser using DataLab