R/kpiwidget.R
kpiwidget.Rd
This function computes and displays a key performance indicator (KPI) based on a
variety of statistics. The data can be filtered using formulas.
In addition, a comparison mode can be applied by specifying the comparison
parameter as either "ratio"
or "share"
. For example, if
comparison = "ratio"
and kpi = "sum"
(with a column indicating sales),
the widget will calculate the ratio of sales between two groups defined by group1
and group2
.
kpiwidget(
data,
kpi = c("count", "distinctCount", "duplicates", "sum", "mean", "min", "max"),
comparison = NULL,
column = NULL,
selection = NULL,
group1 = NULL,
group2 = NULL,
decimals = 1,
big_mark = " ",
prefix = NULL,
suffix = NULL,
width = "auto",
height = "auto",
elementId = NULL,
group = NULL
)
A crosstalk::SharedData
object.
A character string specifying the metric to compute.
Options are: "sum"
, "mean"
, "min"
, "max"
,
"count"
, "distinctCount"
, "duplicates"
. The default is count.
Optional. A character string indicating a comparison mode.
Options are "ratio"
or "share"
. If not provided (NULL), no comparison is performed.
A column name (as a string) to be used for numeric aggregation. In standard mode this is required. In comparison mode, if provided it is used for both groups; if omitted, counts are used.
A one-sided formula to filter rows.
For comparison mode: a one-sided formula defining group 1. This is required in comparison mode.
For comparison mode: a one-sided formula defining group 2.
For comparison = "ratio"
, if not provided, it defaults to the complement of group1.
For comparison = "share"
, if not provided, it defaults to all rows.
Number of decimals to round the computed result. Default: 1.
Character to be used as the thousands separator. Default: " ".
A string to be prepended to the displayed value.
A string to be appended to the displayed value.
Widget width (passed to htmlwidgets::createWidget
). Default: "auto".
Widget height (passed to htmlwidgets::createWidget
). Default: "auto".
Optional element ID for the widget.
crosstalk group name. Typically provided by the SharedData object.
An object of class htmlwidget
that will print itself into an HTML page.
# Standard KPI example:
mtcars_shared <- crosstalk::SharedData$new(mtcars, key = ~ 1:nrow(mtcars), group = "mtcars_group")
kpiwidget(mtcars_shared, kpi = "mean", column = "mpg", decimals = 1,
suffix = " mpg", height = "25px"
)
# Comparison (ratio) example: ratio of mean mpg between two groups.
kpiwidget(mtcars_shared, kpi = "mean", comparison = "ratio", column = "mpg",
group1 = ~ cyl == 4, group2 = ~ cyl == 6, height = "25px"
)