Title: | Efficient Plotting of Large-Sized Data |
---|---|
Description: | A tool to plot data with a large sample size using 'shiny' and 'plotly'. Relatively small samples are obtained from the original data using a specific algorithm. The samples are updated according to a user-defined x range. Jonas Van Der Donckt, Jeroen Van Der Donckt, Emiel Deprost (2022) <https://github.com/predict-idlab/plotly-resampler>. |
Authors: | Junta Tagusari [aut, cre, cph], Jonas Van Der Donckt [cph], Jeroen Van Der Donckt [cph], Emiel Deprost [cph] |
Maintainer: | Junta Tagusari <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.3.0 |
Built: | 2025-03-01 04:21:40 UTC |
Source: | https://github.com/cran/shinyHugePlot |
A base class for the aggregation, which defines the structure of the class and is not available on a stand-alone basis.
An R6::R6Class
object
parameters
Parameters for the aggregation, returned as a named list. Generate a matrix using x and n_out Apply function for nanotime
new()
Constructor of aggregator
aggregator$new( ..., interleave_gaps = FALSE, NA_position = "begin", coef_gap = 3 )
...
Not used.
interleave_gaps, NA_position, coef_gap
Arguments passed to self$set_parameters
, optional.
aggregate()
Aggregates the given input and returns samples.
aggregator$aggregate(x, y, n_out, db = NULL)
x, y
Indexes and values that has to be aggregated.
n_out
Integer or numeric.
db
Character. The duck-db that contains the x-y data. The number of samples that the aggregated data contains.
set_parameters()
Setting of the parameters for the aggregation
aggregator$set_parameters(..., interleave_gaps, NA_position, coef_gap)
...
Not used.
interleave_gaps
Boolean, optional.
Whether NA
values should be added
when there are gaps / irregularly sampled data.
Irregular gaps between samples are determined whether the gap is larger than
the median of the sample gaps times the coefficient for detecting irregular gaps.
By default, FALSE
.
NA_position
Character, optional.
Indicates where NA
s are placed when gaps are detected.
If "end"
, the first point after a gap will be replaced.
If "begin"
, the last point before a gap will be replaced.
If "both"
, both the encompassing gap data points are replaced.
This parameter is only effective when interleave_gaps == TRUE
.
By default, "begin"
.
coef_gap
Numeric, optional. The coefficient to detect irregular gaps. By default, 3.0.
accepted_datatype
Character, optional.
This parameter indicates the supported data classes.
If all data classes are accepted, set it to NULL
.
clone()
The objects of this class are cloneable with this method.
aggregator$clone(deep = FALSE)
deep
Whether to make a deep clone.
This aggregator divides the data into no-overlapping intervals and calculate the first, maximum, minimum, and last values of the data, which represents candle sticks.
An R6::R6Class
object
shinyHugePlot::aggregator
-> candlestick_aggregator
new()
Constructor of the aggregator.
candlestick_aggregator$new(..., interleave_gaps, coef_gap, NA_position)
interleave_gaps, coef_gap, NA_position, ...
Arguments pass to the constructor of aggregator
object.
yupr, y, ylwr
Functions.
Statistical values are calculated using this function.
By default, max, mean, min
, respectively.
Note that the NA values are omitted automatically.
clone()
The objects of this class are cloneable with this method.
candlestick_aggregator$clone(deep = FALSE)
deep
Whether to make a deep clone.
data(noise_fluct) agg <- candlestick_aggregator$new(interleave_gaps = TRUE) d_agg <- agg$aggregate(nanotime::as.nanotime(noise_fluct$time), noise_fluct$f500, 100) fig <- plotly::plot_ly( x = d_agg$x, open = d_agg$open, high = d_agg$high, low = d_agg$low, close = d_agg$close, type = "candlestick" )
data(noise_fluct) agg <- candlestick_aggregator$new(interleave_gaps = TRUE) d_agg <- agg$aggregate(nanotime::as.nanotime(noise_fluct$time), noise_fluct$f500, 100) fig <- plotly::plot_ly( x = d_agg$x, open = d_agg$open, high = d_agg$high, low = d_agg$low, close = d_agg$close, type = "candlestick" )
Arbitrary function can be applied using this aggregation class.
An R6::R6Class
object
shinyHugePlot::aggregator
-> custom_func_aggregator
new()
Constructor of the Aggregator.
custom_func_aggregator$new( ..., aggregation_func, interleave_gaps, coef_gap, NA_position )
aggregation_func
Function.
User-defined function to aggregate data,
of which arguments are x
, y
and n_out
.
interleave_gaps, coef_gap, NA_position, ...
Arguments pass to the constructor of aggregator
object.
set_aggregation_func()
Set a function to aggregate the data
custom_func_aggregator$set_aggregation_func(aggregation_func)
aggregation_func
Function.
User-defined function to aggregate data,
of which arguments are x
, y
and n_out
.
clone()
The objects of this class are cloneable with this method.
custom_func_aggregator$clone(deep = FALSE)
deep
Whether to make a deep clone.
custom_agg_func <- function(x, y, n_out) { bin_width <- floor(length(x)/n_out) x_idx <- seq(floor(bin_width / 2), bin_width * n_out, bin_width) y_mat <- y[1:(bin_width * n_out)] %>% matrix(nrow = bin_width) y_agg <- apply(y_mat, 2, quantile, probs = 0.25) return(list(x = x[x_idx], y = y_agg)) } data(noise_fluct) agg <- custom_func_aggregator$new( aggregation_func = custom_agg_func, interleave_gaps = TRUE ) d_agg <- agg$aggregate( x = noise_fluct$time, y = noise_fluct$f500, n_out = 1000 ) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines")
custom_agg_func <- function(x, y, n_out) { bin_width <- floor(length(x)/n_out) x_idx <- seq(floor(bin_width / 2), bin_width * n_out, bin_width) y_mat <- y[1:(bin_width * n_out)] %>% matrix(nrow = bin_width) y_agg <- apply(y_mat, 2, quantile, probs = 0.25) return(list(x = x[x_idx], y = y_agg)) } data(noise_fluct) agg <- custom_func_aggregator$new( aggregation_func = custom_agg_func, interleave_gaps = TRUE ) d_agg <- agg$aggregate( x = noise_fluct$time, y = noise_fluct$f500, n_out = 1000 ) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines")
This aggregator divides the data into no-overlapping intervals and calculate specific statistical values such as the mean.
An R6::R6Class
object
shinyHugePlot::aggregator
-> custom_stat_aggregator
new()
Constructor of the Aggregator.
Constructor of the Aggregator.
custom_stat_aggregator$new( ..., y_func = mean, x_mean = TRUE, interleave_gaps, coef_gap, NA_position )
y_func
Function.
Statistical values are calculated using this function.
By default, mean
.
x_mean
Boolean.
Whether using the mean values or not for the x values.
If not, the x values that give the specific y values are used.
E.g., if you use max
as the aggregation_func
and
set this argument to FALSE
, x values that give the maximum
y values are used.
By default, TRUE
.
interleave_gaps, coef_gap, NA_position, ...
Arguments pass to the constructor of aggregator
object.
clone()
The objects of this class are cloneable with this method.
custom_stat_aggregator$clone(deep = FALSE)
deep
Whether to make a deep clone.
data(noise_fluct) agg <- custom_stat_aggregator$new(y_func = mean, interleave_gaps = TRUE) d_agg <- agg$aggregate(noise_fluct$time, noise_fluct$f500, 1000) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines")
data(noise_fluct) agg <- custom_stat_aggregator$new(y_func = mean, interleave_gaps = TRUE) d_agg <- agg$aggregate(noise_fluct$time, noise_fluct$f500, 1000) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines")
A class for down-sampling data with a large number of samples.
An instance contains (the reference of) original data, layout of the figure,
and options for aggregating the original data.
An interactive plot for displaying large-sized data can be obtained using
the figure, down-sampler and its options included in the instance,
while making the plot using shiny_hugeplot
function is easier (see examples).
See the super class (plotly_datahandler
) to find more members
to handle the data in plotly
.
An R6::R6Class
object
shinyHugePlot::plotly_datahandler
-> downsampler
downsample_options
Options for aggregating (down-sampling) data registered in this instance.
n_out_default
Default sample size.
aggregator_default
Default aggregator instance.
new()
To construct an instance, original data, layout of the figure, and options
for aggregating the original data are necessary.
The original data and the layout of the figure can be given by providing
a plotly
object (figure
argument).
The options for aggregating the original data can be given by providing
an aggregator (aggregator
argument) and the number of samples
(n_out
argument).
See the constructor of the plotly_datahandler
class for more
information on other arguments.
downsampler$new( figure = NULL, formula = NULL, srcs = NULL, srcs_ext = list(), n_out = 1000L, aggregator = min_max_aggregator$new(), tz = Sys.timezone(), use_light_build = TRUE, legend_options = list(name_prefix = "<b style=\"color:sandybrown\">[S]</b> ", name_suffix = "", xdiff_prefix = "<i style=\"color:#fc9944\"> ~", xdiff_suffix = "</i>"), verbose = F )
figure, srcs, srcs_ext, formula, legend_options, tz, use_light_build
Arguments passed to plotly_datahandler$new
.
n_out
Integer or numeric. The number of samples shown after down-sampling. By default 1000.
aggregator
An instance of an R6 class for aggregation.
Select an aggregation function. The list of the functions are obtained
using list_aggregators
.
By default, min_max_aggregator$new()
.
verbose
Boolean.
Whether detailed messages to check the procedures are shown. By default, FALSE
.
add_trace()
Add a new series to the data registered in the instance.
If a data frame (traces_df
argument) compliant with
self$orig_data
is given, it will be added to self$orig_data
.
If attributes to construct a plotly
object (...
argument)
are given, a data frame is constructed and added.
Options for aggregating data can be set using
aggregator
and n_out
arguments.
It is a wrapper of self$set_trace_data
and
self$set_downsample_options
. See these methods for more information.
Note that the traces of the figure are not updated with this method and
self$update_trace
is necessary.
downsampler$add_trace(..., traces_df = NULL, n_out = NULL, aggregator = NULL)
..., traces_df
Arguments passed to self$set_trace_data
(see the super class of plotly_datahandler
)
n_out, aggregator
Arguments passed to self$set_downsample_options
.
update_trace()
Update traces of the figure registered in the instance
(self$figure$x$data
) according to
re-layout order (relayout_order
argument).
Using reset
and reload
arguments, traces are updated
without re-layout orders.
It just registers the new traces and returns nothing by default.
It returns the new traces if send_trace
is TRUE
.
downsampler$update_trace( relayout_order = list(NULL), reset = FALSE, reload = FALSE, send_trace = FALSE )
relayout_order
Named list.
A list generated by plotlyjs_relayout
,
which is obtained using plotly::event_data
.
e.g.,
If you would like set the range of the 2nd x axis to [10.0, 21.5],
list(`xaxis2.range[0]` = 10.0, `xaxis2.range[1]` = 21.5)
.
If you would like reset the range of the 1st x axis,
list(xaxis.autorange = TRUE, xaxis.showspike = TRUE)
.
reset
Boolean.
If it is TRUE
, all other arguments are neglected and
the figure will be reset (all the ranges of x axes are initialized).
By default, FALSE
.
reload
Boolean.
If it is TRUE
, the ranges of the figure are preserved but
the aggregation will be conducted with the current settings.
By default, FALSE
.
send_trace
Boolean.
If it is TRUE
, a named list will be returned,
which contains the indexes of the traces that will be updated
(trace_idx_update
) and the updated traces (new_trace
).
By default, FALSE
.
set_downsample_options()
In the instance, options for aggregating data are registered as data frame.
(see self$downsample_options
.)
Using this method, the options can be set.
downsampler$set_downsample_options(uid = NULL, n_out = NULL, aggregator = NULL)
uid
Character, optional.
The unique id of the trace.
If NULL
, all the options registered in this instance are updated.
By default, NULL
.
n_out
Numeric or integer, optional.
The number of samples output by the aggregator.
If NULL
, the default value registered in this instance is used.
By default, NULL
.
aggregator
aggregator
object, optional.
An instance that aggregate the data.
If NULL
, the default value registered in this instance is used.
clone()
The objects of this class are cloneable with this method.
downsampler$clone(deep = FALSE)
deep
Whether to make a deep clone.
data(noise_fluct) # example 1 : Easy method using shiny_hugeplot shiny_hugeplot(noise_fluct$time, noise_fluct$f500) # example 2 : Manual method using a downsampler object fig <- plot_ly( x = noise_fluct$time, y = noise_fluct$f500, type = "scatter", mode = "lines" ) %>% layout(xaxis = list(type = "date")) %>% shinyHugePlot::plotly_build_light() ds <- downsampler$new( figure = fig, aggregator = min_max_aggregator$new(interleave_gaps = TRUE) ) ui <- fluidPage( plotlyOutput(outputId = "hp", width = "800px", height = "600px") ) server <- function(input, output, session) { output$hp <- renderPlotly(ds$figure) observeEvent(plotly::event_data("plotly_relayout"),{ updatePlotlyH(session, "hp", plotly::event_data("plotly_relayout"), ds) }) } shinyApp(ui = ui, server = server) # example 3 : Add another series of which aggregator is different noise_events <- tibble( time = c("2022-11-09 12:25:50", "2022-11-09 12:26:14"), level = c(60, 60) ) ds$add_trace( x = noise_events$time, y = noise_events$level, name = "event", type = "scatter", mode = "markers", aggregator = null_aggregator$new() ) ds$update_trace(reset = TRUE) server <- function(input, output, session) { output$hp <- renderPlotly(ds$figure) observeEvent(plotly::event_data("plotly_relayout"),{ updatePlotlyH(session, "hp", plotly::event_data("plotly_relayout"), ds) }) } shinyApp(ui = ui, server = server)
data(noise_fluct) # example 1 : Easy method using shiny_hugeplot shiny_hugeplot(noise_fluct$time, noise_fluct$f500) # example 2 : Manual method using a downsampler object fig <- plot_ly( x = noise_fluct$time, y = noise_fluct$f500, type = "scatter", mode = "lines" ) %>% layout(xaxis = list(type = "date")) %>% shinyHugePlot::plotly_build_light() ds <- downsampler$new( figure = fig, aggregator = min_max_aggregator$new(interleave_gaps = TRUE) ) ui <- fluidPage( plotlyOutput(outputId = "hp", width = "800px", height = "600px") ) server <- function(input, output, session) { output$hp <- renderPlotly(ds$figure) observeEvent(plotly::event_data("plotly_relayout"),{ updatePlotlyH(session, "hp", plotly::event_data("plotly_relayout"), ds) }) } shinyApp(ui = ui, server = server) # example 3 : Add another series of which aggregator is different noise_events <- tibble( time = c("2022-11-09 12:25:50", "2022-11-09 12:26:14"), level = c(60, 60) ) ds$add_trace( x = noise_events$time, y = noise_events$level, name = "event", type = "scatter", mode = "markers", aggregator = null_aggregator$new() ) ds$update_trace(reset = TRUE) server <- function(input, output, session) { output$hp <- renderPlotly(ds$figure) observeEvent(plotly::event_data("plotly_relayout"),{ updatePlotlyH(session, "hp", plotly::event_data("plotly_relayout"), ds) }) } shinyApp(ui = ui, server = server)
Efficient version off LTTB
by first reducing really large data with the min_max_ovlp_aggregator
and then further aggregating the reduced result with LTTB_aggregator
.
An R6::R6Class
object
shinyHugePlot::aggregator
-> eLTTB_aggregator
LTTB
An R6 LTTB_aggregator instance
minmax
An R6 min_max_ovlp_aggregator
instance
new()
Constructor of the aggregator.
eLTTB_aggregator$new(..., interleave_gaps, coef_gap, NA_position)
...
Arguments pass to the constructor of aggregator
,
LTTB_aggregator
and min_max_oblp_aggregator
objects.
interleave_gaps, coef_gap, NA_position
Arguments pass to the constructor of aggregator
object.
clone()
The objects of this class are cloneable with this method.
eLTTB_aggregator$clone(deep = FALSE)
deep
Whether to make a deep clone.
data(noise_fluct) agg <- eLTTB_aggregator$new(interleave_gaps = TRUE) d_agg <- agg$aggregate(noise_fluct$time, noise_fluct$f500, 1000) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines")
data(noise_fluct) agg <- eLTTB_aggregator$new(interleave_gaps = TRUE) d_agg <- agg$aggregate(noise_fluct$time, noise_fluct$f500, 1000) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines")
It displays all the aggregators registered in the package. No arguments are necessary.
list_aggregators()
list_aggregators()
list_aggregators()
list_aggregators()
The LTTB method aggregates the huge samples using the areas of the triangles formed by the samples. Numerical distances are employed in this class, which requires the ratio between x and y values. When the x is datetime, nanosecond is a unit. When the x is factor or character, it will be encoded into numeric codes.
An R6::R6Class
object
shinyHugePlot::aggregator
-> LTTB_aggregator
new()
Constructor of the aggregator.
LTTB_aggregator$new( ..., nt_y_ratio = 1e+09, x_y_ratio = 1, interleave_gaps, coef_gap, NA_position )
x_y_ratio, nt_y_ratio
Numeric.
These parameters set the unit length of the numeric x
and nanotime
x.
For example, setting x_y_ratio
to 2 is equivalent to
assuming 2 is the unit length of x
(and 1 is always the unit length of y
).
The unit length is employed to calculate the area of the triangles.
interleave_gaps, coef_gap, NA_position, ...
Arguments pass to the constructor of aggregator
object.
Note that accepted_datatype
has default value.
Downsample with the Largest Triangle Three Buckets (LTTB) aggregation method
clone()
The objects of this class are cloneable with this method.
LTTB_aggregator$clone(deep = FALSE)
deep
Whether to make a deep clone.
data(noise_fluct) agg <- LTTB_aggregator$new(interleave_gaps = TRUE) d_agg <- agg$aggregate( x = noise_fluct$time, y = noise_fluct$f500, n_out = 1000 ) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines")
data(noise_fluct) agg <- LTTB_aggregator$new(interleave_gaps = TRUE) d_agg <- agg$aggregate( x = noise_fluct$time, y = noise_fluct$f500, n_out = 1000 ) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines")
Divide the data into small data ranges and find the maximum (absolute) value of each. It may be useful for the waveform data.
An R6::R6Class
object
shinyHugePlot::aggregator
-> max_aggregator
new()
Constructor of the Aggregator.
max_aggregator$new(..., interleave_gaps, coef_gap, NA_position, use_abs = TRUE)
interleave_gaps, coef_gap, NA_position, ...
Arguments pass to the constructor of aggregator
object.
use_abs
Logical. If TRUE
, the absolute value is used.
clone()
The objects of this class are cloneable with this method.
max_aggregator$clone(deep = FALSE)
deep
Whether to make a deep clone.
data(noise_fluct) agg <- max_aggregator$new(interleave_gaps = TRUE) d_agg <- agg$aggregate(noise_fluct$time, noise_fluct$f500, 1000) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines")
data(noise_fluct) agg <- max_aggregator$new(interleave_gaps = TRUE) d_agg <- agg$aggregate(noise_fluct$time, noise_fluct$f500, 1000) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines")
Divide the data into small data ranges
and find the maximum and minimum values of each.
Note that many samples may be replaced with NA
,
if interleave_gaps = TRUE
and the original data is increased or decreased
monotonically. Use min_max_ovlp_aggregator
instead in that case.
n_out
must be even number.
An R6::R6Class
object
shinyHugePlot::aggregator
-> min_max_aggregator
new()
Constructor of the Aggregator.
min_max_aggregator$new(..., interleave_gaps, coef_gap, NA_position)
interleave_gaps, coef_gap, NA_position, ...
Arguments pass to the constructor of aggregator
object.
clone()
The objects of this class are cloneable with this method.
min_max_aggregator$clone(deep = FALSE)
deep
Whether to make a deep clone.
data(noise_fluct) agg <- min_max_aggregator$new(interleave_gaps = TRUE) d_agg <- agg$aggregate(noise_fluct$time, noise_fluct$f500, 1000) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines")
data(noise_fluct) agg <- min_max_aggregator$new(interleave_gaps = TRUE) d_agg <- agg$aggregate(noise_fluct$time, noise_fluct$f500, 1000) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines")
Divide the data into 50% overlapping intervals
and find the maximum and minimum values of each.
n_out
must be even number.
An R6::R6Class
object
shinyHugePlot::aggregator
-> min_max_ovlp_aggregator
new()
Constructor of the Aggregator.
min_max_ovlp_aggregator$new(..., interleave_gaps, coef_gap, NA_position)
interleave_gaps, coef_gap, NA_position, ...
Arguments pass to the constructor of aggregator
object.
clone()
The objects of this class are cloneable with this method.
min_max_ovlp_aggregator$clone(deep = FALSE)
deep
Whether to make a deep clone.
data(noise_fluct) agg <- min_max_ovlp_aggregator$new(interleave_gaps = TRUE) d_agg <- agg$aggregate(noise_fluct$time, noise_fluct$f500, 1000) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines")
data(noise_fluct) agg <- min_max_ovlp_aggregator$new(interleave_gaps = TRUE) d_agg <- agg$aggregate(noise_fluct$time, noise_fluct$f500, 1000) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines")
Results of the measurement of the sound level, where peaks due to road traffic are observed.
noise_fluct
noise_fluct
A data frame with 32,001 rows and 4 columns:
time
Octave-band sound levels whose center frequencies are 500, 1000 and 2000 Hz.
Junta Tagusari [email protected]
Aggregation by extracting every Nth data.
An R6::R6Class
object
shinyHugePlot::aggregator
-> nth_pnt_aggregator
new()
Constructor of the Aggregator.
nth_pnt_aggregator$new(..., interleave_gaps, coef_gap, NA_position)
interleave_gaps, coef_gap, NA_position, ...
Arguments pass to the constructor of aggregator
object.
clone()
The objects of this class are cloneable with this method.
nth_pnt_aggregator$clone(deep = FALSE)
deep
Whether to make a deep clone.
data(noise_fluct) agg <- nth_pnt_aggregator$new(interleave_gaps = TRUE) d_agg <- agg$aggregate(noise_fluct$time, noise_fluct$f500, 1000) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines")
data(noise_fluct) agg <- nth_pnt_aggregator$new(interleave_gaps = TRUE) d_agg <- agg$aggregate(noise_fluct$time, noise_fluct$f500, 1000) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines")
It does not aggregate the data but returns the full samples within the range.
An R6::R6Class
object
shinyHugePlot::aggregator
-> null_aggregator
new()
Constructor of the Aggregator.
null_aggregator$new(..., interleave_gaps, coef_gap, NA_position)
interleave_gaps, coef_gap, NA_position, ...
Arguments pass to the constructor of aggregator
object.
aggregate()
A function that does nothing other than inserting NAs.
null_aggregator$aggregate(...)
...
Arguments passed to super$aggregate
.
clone()
The objects of this class are cloneable with this method.
null_aggregator$clone(deep = FALSE)
deep
Whether to make a deep clone.
data(noise_fluct) agg <- null_aggregator$new(interleave_gaps = TRUE) d_agg <- agg$aggregate(noise_fluct$time, noise_fluct$f500) plotly::plot_ly( x = d_agg$x[1:100], y = d_agg$y[1:100], type = "scatter", mode = "lines" )
data(noise_fluct) agg <- null_aggregator$new(interleave_gaps = TRUE) d_agg <- agg$aggregate(noise_fluct$time, noise_fluct$f500) plotly::plot_ly( x = d_agg$x[1:100], y = d_agg$y[1:100], type = "scatter", mode = "lines" )
plotly
data with low computation costBefore illustrating data using plotly
, it must be built
(figure$x$data
are need to be made using figure$x$attrs
).
However, because a lot of procedures are necessary,
the computation cost is relatively high.
With this function, the data is built in quite short time by omitting
several procedures for high-frequency data.
Note that this function is not universally applicable to all plotly
objects but made for high-frequency scatter data.
plotly::plotly_build
function may return better results in
specific cases although it takes more time.
plotly_build_light(fig, vars_hf = c("x", "y", "text", "hovertext"))
plotly_build_light(fig, vars_hf = c("x", "y", "text", "hovertext"))
fig |
|
vars_hf |
Character, optional.
Variable names where high frequency data is included.
It must include |
built plotly
object
data(noise_fluct) plotly_build_light( plotly::plot_ly( x = noise_fluct$time, y = noise_fluct$f500, name = "level", type = "scatter" ) ) plotly_build_light( plotly::plot_ly( data = noise_fluct, x = ~time, y = ~f500, name = "level", type = "scatter" ) )
data(noise_fluct) plotly_build_light( plotly::plot_ly( x = noise_fluct$time, y = noise_fluct$f500, name = "level", type = "scatter" ) ) plotly_build_light( plotly::plot_ly( data = noise_fluct, x = ~time, y = ~f500, name = "level", type = "scatter" ) )
A class for handling plotly
data,
which defines functions used in the downsampler
class
An R6::R6Class
object
figure
plotly
object.
orig_data
Data frame representing plotly
traces.
trace_df_default
Data frame representing default values
of plotly
traces.
name
column represents the names of the attributes.
required
column represents whether the attributes are necessary
to construct a data frame of a trace.
data
column represents whether the attributes are the data.
default
attributes represents default values of the attributes.
When constructing a data frame of a trace, default values are used
if no values are assigned.
class
column represents the acceptable classes of the attributes.
new()
Constructing an instance.
The data contained in a plotly
object (figure
argument)
will be included in the instance (as a reference).
plotly_datahandler$new( figure = NULL, srcs = NULL, formula = NULL, srcs_ext = NULL, legend_options = list(name_prefix = "<b style=\"color:sandybrown\">[S]</b> ", name_suffix = "", xdiff_prefix = "<i style=\"color:#fc9944\"> ~", xdiff_suffix = "</i>"), tz = Sys.timezone(), use_light_build = TRUE )
figure
plotly
object.
The traces of this object will be down-sampled.
srcs, srcs_ext, formula
Character and formula, optional.
srcs
is the path of the source data (or directory).
When a directory is specified, srcs_ext
is the extension of the source file.
formula
is the formula to extract the data from the source data.
legend_options
Named list, optional.
Names of the elements are name_prefix
,
name_suffix
, xdiff_prefix
,
and xdiff_suffix
.
name_prefix
and name_suffix
will be added to the name of the trace when the down-sampling is applied.
By default, prefix is a bold orange [S]
and suffix is none.
xdiff_prefix
and xdiff_suffix
are employed to show
the mean aggregation size of the down-sampling.
tz
Character, optional.
Time zone used to display time-series data.
By default Sys.timezone()
.
use_light_build
Boolean, optional.
Whether plotly_build_light
is used.
It quickly build scatter-type plotly
data.
By default, TRUE
.
set_trace_data()
In the instance, data is contained as a data frame
(see self$orig_data
for detailed information).
Using this method, the data can be added or overwritten.
If a data frame (traces_df
argument) is given, it will be
added to self$orig_data
or reassigned as self$orig_data
.
If attributes to construct a plotly
object (...
argument)
are given, a data frame is constructed and used.
plotly_datahandler$set_trace_data(..., traces_df = NULL, append = FALSE)
...
Arguments to constitute a plotly
attributes, optional.
For instance, x
, y
, type
, and mode
are applicable. See plotly::plot_ly
.
traces_df
Data frame, optional.
Data frame whose format is agreed with self$orig_data
.
If traces_df
is given, arguments in ...
are neglected.
append
Boolean, optional.
Whether the data is append or overwrite. By default, FALSE
(the traces are overwritten).
srcs_to_df()
Covert the data contained in srcs
file(s) to a duck-db.
A minimum data and the path of the database will be returned.
plotly_datahandler$srcs_to_df(fml, srcs, srcs_ext = NULL)
fml
Formula. The formula to extract the data from the source data.
srcs
Character. The name of the source file (e.g. data.parquet) or the directory can be specified.
srcs_ext
Character, optional.
The extension of the source file, if srcs
is a directory.
plotly_data_to_df()
Covert the data contained in plotly
object to a data frame.
A unique id (uid
) is granted to each data.
The data frame will be returned.
plotly_datahandler$plotly_data_to_df(plotly_data, use_datatable = TRUE)
plotly_data
List.
The list whose elements are named list representing plotly
traces.
All elements must have elements named type
.
use_datatable
Boolean.
If it is TRUE
, data such as x
and y
are nested
in a data.table
, of which key column is x
.
By default, TRUE
.
clone()
The objects of this class are cloneable with this method.
plotly_datahandler$clone(deep = FALSE)
deep
Whether to make a deep clone.
This aggregator divides the data into no-overlapping intervals and calculate specific statistics that represents the range and nominal values of the data, such as the max, min and mean.
An R6::R6Class
object
shinyHugePlot::aggregator
-> shinyHugePlot::rng_aggregator
-> range_stat_aggregator
new()
Constructor of the aggregator.
range_stat_aggregator$new( ..., ylwr = min, y = mean, yupr = max, interleave_gaps, coef_gap, NA_position )
yupr, y, ylwr
Functions.
Statistical values are calculated using this function.
By default, max, mean, min
, respectively.
Note that the NA values are omitted automatically.
interleave_gaps, coef_gap, NA_position, ...
Arguments pass to the constructor of aggregator
object.
clone()
The objects of this class are cloneable with this method.
range_stat_aggregator$clone(deep = FALSE)
deep
Whether to make a deep clone.
data(noise_fluct) agg <- range_stat_aggregator$new( ylwr = min, y = mean, yupr = max, interleave_gaps = TRUE ) d_agg <- agg$aggregate(nanotime::as.nanotime(noise_fluct$time), noise_fluct$f500, 100) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines") %>% plotly::add_trace(x = d_agg$x, y = d_agg$ylwr, type = "scatter", mode = "lines")%>% plotly::add_trace(x = d_agg$x, y = d_agg$yupr, type = "scatter", mode = "lines")
data(noise_fluct) agg <- range_stat_aggregator$new( ylwr = min, y = mean, yupr = max, interleave_gaps = TRUE ) d_agg <- agg$aggregate(nanotime::as.nanotime(noise_fluct$time), noise_fluct$f500, 100) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines") %>% plotly::add_trace(x = d_agg$x, y = d_agg$ylwr, type = "scatter", mode = "lines")%>% plotly::add_trace(x = d_agg$x, y = d_agg$yupr, type = "scatter", mode = "lines")
This aggregator divides the data into no-overlapping intervals and calculate the root-mean-square and the maximum absolute values of the data, which may be helpful to understand the waveforms.
An R6::R6Class
object
shinyHugePlot::aggregator
-> shinyHugePlot::rng_aggregator
-> rms_max_aggregator
new()
Constructor of the aggregator.
rms_max_aggregator$new( ..., interleave_gaps, coef_gap, NA_position, use_abs = TRUE )
interleave_gaps, coef_gap, NA_position, ...
Arguments pass to the constructor of aggregator
object.
use_abs
Boolean. If 'TRUE', the maximum absolute values are calculated.
clone()
The objects of this class are cloneable with this method.
rms_max_aggregator$clone(deep = FALSE)
deep
Whether to make a deep clone.
data(noise_fluct) agg <- rms_max_aggregator$new( ylwr = min, y = mean, yupr = max, interleave_gaps = TRUE ) d_agg <- agg$aggregate(nanotime::as.nanotime(noise_fluct$time), noise_fluct$f500, 100) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines") %>% plotly::add_trace(x = d_agg$x, y = d_agg$ylwr, type = "scatter", mode = "lines")%>% plotly::add_trace(x = d_agg$x, y = d_agg$yupr, type = "scatter", mode = "lines")
data(noise_fluct) agg <- rms_max_aggregator$new( ylwr = min, y = mean, yupr = max, interleave_gaps = TRUE ) d_agg <- agg$aggregate(nanotime::as.nanotime(noise_fluct$time), noise_fluct$f500, 100) plotly::plot_ly(x = d_agg$x, y = d_agg$y, type = "scatter", mode = "lines") %>% plotly::add_trace(x = d_agg$x, y = d_agg$ylwr, type = "scatter", mode = "lines")%>% plotly::add_trace(x = d_agg$x, y = d_agg$yupr, type = "scatter", mode = "lines")
A super class for describing aggregator
that returns x
, y
,
ylwr
and yupr
values based on given x
and y
data.
An R6::R6Class
object
List of which elements represent the ranges.
If there are no NA
s, the length of the list is 1;
multiple lists are obtained if there are NA
s.
Each element of list has x
and y
values that surround
the range of values.
shinyHugePlot::aggregator
-> rng_aggregator
new()
Constructor of the Aggregator.
rng_aggregator$new(interleave_gaps, coef_gap, NA_position, ...)
interleave_gaps, coef_gap, NA_position, ...
Arguments pass to the constructor of aggregator
object.
as_plotly_range()
Compute a plotly
trace to illustrate the range of the data.
rng_aggregator$as_plotly_range(x, y, ylwr, yupr, opacity = 0.5)
x, y, ylwr, yupr
Outputs of the sub class of rng_aggregator
.
opacity
Numeric, optional. Opacity of the range fill. By default, 0.5.
as_range()
Compute x
, ylwr
and yupr
from a plotly
trace
made by self$as_plotly_range
.
rng_aggregator$as_range(prng)
prng
List that represents range values, which
must contains x
, y
.
Note that the list may be an element of a list generated by
self$as_plotly_range
.
clone()
The objects of this class are cloneable with this method.
rng_aggregator$clone(deep = FALSE)
deep
Whether to make a deep clone.
shinyHugePlot
This is a S3 class function to easily plot large-sized data using
downsampler
object including plotly
and
shiny
application.
Using data that is given as a first argument,
shiny
application will be constructed and (by default,) executed.
As the first argument, many classes are applicable,
ranging from a numeric vector representing y values
to a downsampler
object containing
original data, layout of the figure and
options for aggregating the original data.
shiny_hugeplot(obj, ...) ## Default S3 method: shiny_hugeplot( obj = NULL, y = NULL, tz = Sys.timezone(), use_light_build = TRUE, plotly_options = list(type = "scatter", mode = "lines"), plotly_layout_options = list(), aggregator = min_max_aggregator$new(), n_out = 1000L, run_shiny = TRUE, downsampler_options = list(), shiny_options = list(), width = "100%", height = "600px", verbose = FALSE, ... ) ## S3 method for class 'formula' shiny_hugeplot( obj = NULL, srcs = NULL, n_out = 1000L, aggregator = min_max_aggregator$new(), run_shiny = TRUE, use_light_build = TRUE, fread_options = list(), downsampler_options = list(), plotly_options = list(type = "scatter", mode = "lines"), plotly_layout_options = list(), shiny_options = list(), width = "100%", height = "600px", verbose = FALSE, ... ) ## S3 method for class 'character' shiny_hugeplot( obj = NULL, n_out = 1000L, aggregator = min_max_aggregator$new(), run_shiny = TRUE, use_light_build = TRUE, fread_options = list(), downsampler_options = list(), plotly_options = list(type = "scatter", mode = "lines"), plotly_layout_options = list(), shiny_options = list(), width = "100%", height = "600px", verbose = FALSE, ... ) ## S3 method for class 'matrix' shiny_hugeplot( obj = NULL, n_out = 1000L, aggregator = min_max_aggregator$new(), run_shiny = TRUE, use_light_build = TRUE, downsampler_options = list(), plotly_options = list(type = "scatter", mode = "lines"), plotly_layout_options = list(), shiny_options = list(), width = "100%", height = "600px", verbose = FALSE, ... ) ## S3 method for class 'data.frame' shiny_hugeplot( obj = NULL, tz = Sys.timezone(), n_out = 1000L, aggregator = min_max_aggregator$new(), run_shiny = TRUE, use_light_build = TRUE, downsampler_options = list(), plotly_options = list(type = "scatter", mode = "lines"), plotly_layout_options = list(), shiny_options = list(), width = "100%", height = "600px", verbose = FALSE, ... ) ## S3 method for class 'plotly' shiny_hugeplot( obj, n_out = 1000L, aggregator = min_max_aggregator$new(), run_shiny = TRUE, use_light_build = TRUE, downsampler_options = list(), shiny_options = list(), width = "100%", height = "600px", verbose = FALSE, ... ) ## S3 method for class 'downsampler' shiny_hugeplot( obj, run_shiny = TRUE, shiny_options = list(), width = "100%", height = "600px", verbose = FALSE, ... )
shiny_hugeplot(obj, ...) ## Default S3 method: shiny_hugeplot( obj = NULL, y = NULL, tz = Sys.timezone(), use_light_build = TRUE, plotly_options = list(type = "scatter", mode = "lines"), plotly_layout_options = list(), aggregator = min_max_aggregator$new(), n_out = 1000L, run_shiny = TRUE, downsampler_options = list(), shiny_options = list(), width = "100%", height = "600px", verbose = FALSE, ... ) ## S3 method for class 'formula' shiny_hugeplot( obj = NULL, srcs = NULL, n_out = 1000L, aggregator = min_max_aggregator$new(), run_shiny = TRUE, use_light_build = TRUE, fread_options = list(), downsampler_options = list(), plotly_options = list(type = "scatter", mode = "lines"), plotly_layout_options = list(), shiny_options = list(), width = "100%", height = "600px", verbose = FALSE, ... ) ## S3 method for class 'character' shiny_hugeplot( obj = NULL, n_out = 1000L, aggregator = min_max_aggregator$new(), run_shiny = TRUE, use_light_build = TRUE, fread_options = list(), downsampler_options = list(), plotly_options = list(type = "scatter", mode = "lines"), plotly_layout_options = list(), shiny_options = list(), width = "100%", height = "600px", verbose = FALSE, ... ) ## S3 method for class 'matrix' shiny_hugeplot( obj = NULL, n_out = 1000L, aggregator = min_max_aggregator$new(), run_shiny = TRUE, use_light_build = TRUE, downsampler_options = list(), plotly_options = list(type = "scatter", mode = "lines"), plotly_layout_options = list(), shiny_options = list(), width = "100%", height = "600px", verbose = FALSE, ... ) ## S3 method for class 'data.frame' shiny_hugeplot( obj = NULL, tz = Sys.timezone(), n_out = 1000L, aggregator = min_max_aggregator$new(), run_shiny = TRUE, use_light_build = TRUE, downsampler_options = list(), plotly_options = list(type = "scatter", mode = "lines"), plotly_layout_options = list(), shiny_options = list(), width = "100%", height = "600px", verbose = FALSE, ... ) ## S3 method for class 'plotly' shiny_hugeplot( obj, n_out = 1000L, aggregator = min_max_aggregator$new(), run_shiny = TRUE, use_light_build = TRUE, downsampler_options = list(), shiny_options = list(), width = "100%", height = "600px", verbose = FALSE, ... ) ## S3 method for class 'downsampler' shiny_hugeplot( obj, run_shiny = TRUE, shiny_options = list(), width = "100%", height = "600px", verbose = FALSE, ... )
obj |
Numeric/ |
... |
Not used. |
y |
Numeric vector, optional.
y values of the figure of |
tz |
Timezone, optional.
It is used to convert the |
use_light_build |
Boolean, optional.
Whether |
plotly_options |
Named list, optional.
Arguments passed to |
plotly_layout_options |
Named list, optional.
Arguments passed to |
aggregator |
Instance of R6 classes for aggregating data, optional.
The classes can be listed using |
n_out |
Integer, optional. Number of samples get by the down-sampling. By default, 1000. |
run_shiny |
Boolean, optional.
whether a generated |
downsampler_options |
Named list, optional.
Arguments passed to |
shiny_options |
Named list, optional.
Arguments passed to |
width , height
|
Character, optional.
Arguments passed to |
verbose |
Boolean.
Whether detailed messages to check the procedures are shown. By default, |
srcs |
Character, used when a |
fread_options |
Named list, optional.
Arguments passed to |
data(noise_fluct) shiny_hugeplot(noise_fluct$f500) shiny_hugeplot(noise_fluct$time, noise_fluct$f500)
data(noise_fluct) shiny_hugeplot(noise_fluct$f500) shiny_hugeplot(noise_fluct$time, noise_fluct$f500)
An interactive plot for data with a large sample size using shiny
andplotly
can be obtained.
For an easy application, see shiny_hugeplot
function.
For a manual application, see downsampler
class.
Maintainer: Junta Tagusari [email protected] [copyright holder]
Other contributors:
Jonas Van Der Donckt [copyright holder]
Jeroen Van Der Donckt [copyright holder]
Emiel Deprost [copyright holder]
plotly
tracesIt is used by registering in a shiny
application.
It receives events in plotly
figure and update it using a method of
a downsampler
instance.
See the examples in downsampler
class.
updatePlotlyH( session, outputId, relayout_order, ds_obj, reset = FALSE, reload = FALSE, verbose = FALSE )
updatePlotlyH( session, outputId, relayout_order, ds_obj, reset = FALSE, reload = FALSE, verbose = FALSE )
session |
|
outputId |
Character.
The |
relayout_order |
Named list.
The list generated by |
ds_obj |
|
reset |
Boolean.
It it is |
reload |
Boolean.
It it is |
verbose |
Boolean.
Whether detailed messages to check the procedures are shown. By default, |