knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
The kpiwidget
package provides an easy way to create KPI (Key Performance Indicator) widgets for dashboards using crosstalk
shared data. This vignette demonstrates different options and functionalities available in kpiwidget
.
Before using kpiwidget
, ensure that it is installed along with crosstalk
:
# install.packages("crosstalk") # Install kpiwidget from CRAN or GitHub # install.packages("kpiwidget") # devtools::install_github("your_github/kpiwidget")
library(kpiwidget) library(crosstalk) library(DT) library(dplyr) library(htmltools)
To enable interactivity, we first wrap our dataset in SharedData
from crosstalk
. This allows filtering across multiple widgets using the same dataset.
# Create a shared data object with row numbers as keys df_shared <- crosstalk::SharedData$new(mtcars, key = ~ 1:nrow(mtcars), group = "mtcars_group")
crosstalk
allows dynamic filtering of data. Here, we add a filter to select vehicles based on the number of gears:
crosstalk::filter_checkbox("gear", "Gear", df_shared, ~gear, inline = TRUE)
kpiwidget
The kpiwidget
function provides a simple way to display key performance indicators. The column
parameter is required, and by default, it calculates the count (number of rows in the dataset).
kpiwidget( data = df_shared, column = "mpg", height = "25px" )
The kpi
parameter allows different types of calculations. Below, we demonstrate various options available in kpiwidget
.
Calculates the sum of the selected column.
kpiwidget( data = df_shared, column = "mpg", kpi = "sum", height = "25px" )
Computes the average (mean) of the selected column.
kpiwidget( data = df_shared, column = "mpg", kpi = "mean", height = "25px" )
Finds the minimum value in the selected column.
kpiwidget( data = df_shared, column = "mpg", kpi = "min", height = "25px" )
Finds the maximum value in the selected column.
kpiwidget( data = df_shared, column = "mpg", kpi = "max", height = "25px" )
Counts the number of rows in the dataset.
kpiwidget( data = df_shared, column = "mpg", kpi = "count", height = "25px" )
Counts the number of unique values in the selected column.
kpiwidget( data = df_shared, column = "cyl", kpi = "distinctCount", height = "25px" )
Calculates the ratio of a subset defined with group1
parameter (e.g., cars with 4 cylinders) compared to the complement of group1 filter (default setting for "ratio") or to the subset defined with group2
parameter.
default:
kpiwidget( data = df_shared, column = "mpg", kpi = "mean", comparison = "ratio", group1 = ~ cyl == 4, height = "25px" )
group2:
kpiwidget( data = df_shared, column = "mpg", kpi = "mean", comparison = "ratio", group1 = ~ cyl == 4, group2 = ~ cyl == 6, height = "25px" )
Computes the share between two groups (e.g., cars with 4 cylinders and full dataset or subset defined with group2
pamarater).
default:
kpiwidget( data = df_shared, column = "mpg", kpi = "count", comparison = "share", group1 = ~ cyl == 4, height = "25px" )
group2:
kpiwidget( data = df_shared, column = "mpg", kpi = "count", comparison = "share", group1 = ~ cyl == 4, group2 = ~ cyl %in% c(4, 6), height = "25px" )
using selection:
kpiwidget( data = df_shared, column = "mpg", kpi = "count", selection = ~ cyl %in% c(4, 6), comparison = "share", group1 = ~ cyl == 4, height = "25px" )
kpiwidget( data = df_shared, column = "mpg", kpi = "mean", decimals = 3, height = "25px" )
kpiwidget( data = df_shared, column = "disp", kpi = "sum", big_mark = " ,", height = "25px" )
kpiwidget( data = df_shared, column = "mpg", kpi = "mean", prefix = "mean mpg: ", height = "25px" )
kpiwidget( data = df_shared, column = "mpg", kpi = "count", comparison = "share", group1 = ~ cyl == 4, suffix = " %", height = "25px" )
crosstalk::filter_slider("wt", "Weight", df_shared, ~wt, width = "100%") DT::datatable(df_shared, extensions = "Scroller", style = "bootstrap", class = "compact", width = "100%", options = list(deferRender = TRUE, scrollY = 300, scroller = TRUE) )
There are 32 records in unfiltered dataset. r kpiwidget(data = df_shared, kpi = "count", column = "mpg", decimals = 0, suffix = " car(s) are currently selected.")
On average they have r kpiwidget(data = df_shared, kpi = 'mean', column = 'hp', decimals = 0, suffix = " HP")
and can drive r kpiwidget(data = df_shared, column = "mpg", kpi = "mean", suffix = " mpg.")
8 cylinder cars have r kpiwidget(data = df_shared, column = "hp", kpi = "mean", comparison = "ratio", group1 = ~ cyl == 8)
times more HP than the rest of cars in selection.
The kpiwidget
package provides a flexible way to display KPIs in interactive dashboards. Using crosstalk
, it enables real-time filtering and comparison of data.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.