Title: | `Data-UI` Interactive Visualizations |
---|---|
Description: | Create sparklines and histograms with 'data-ui'. |
Authors: | Chris Williams [aut, cph] (data-ui library, https://williaster.github.io/data-ui), Kent Russell [aut, cre] (R interface), Ken Steif [ctb] (R interface) |
Maintainer: | Kent Russell <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.1 |
Built: | 2024-11-11 06:16:10 UTC |
Source: | https://github.com/timelyportfolio/dataui |
Output and render functions for using dataui within Shiny applications and interactive Rmd documents.
datauiOutput(outputId, width = "100%", height = "400px") renderDataui(expr, env = parent.frame(), quoted = FALSE) dataui_html(id, style, class, ...)
datauiOutput(outputId, width = "100%", height = "400px") renderDataui(expr, env = parent.frame(), quoted = FALSE) dataui_html(id, style, class, ...)
outputId |
output variable to read from |
width , height
|
Must be a valid CSS unit (like |
expr |
An expression that generates a dataui |
env |
The environment in which to evaluate |
quoted |
Is |
Add 'data-ui' Dependencies to Tag or 'htmlwidget'
dui_add_deps(tag_htmlwidget = NULL)
dui_add_deps(tag_htmlwidget = NULL)
tag_htmlwidget |
|
shiny.tag
or htmlwidget
library(htmltools) # not that you would want to do this but illustrates the usage of helper functions browsable( dui_add_deps( tags$div( dui_chr( dui_sparkline( data = runif(100), components = list( dui_sparklineseries(strokeDasharray = "3,3"), dui_sparkpointseries(points = list("min","max")) ) ) ) ) ) ) library(dplyr) library(htmltools) library(dataui) data("diamonds", package="ggplot2") hist_all <- hist(diamonds$price, breaks = 20, plot = FALSE) dat <- diamonds %>% group_by(cut) %>% summarize( n = n(), mean_price = mean(price), hist = list(unclass(hist(price, breaks = hist_all$breaks, plot = FALSE))) ) # calculate max so all sparklines use same scale max_density <- max(unlist(lapply(dat$hist, function(x) {x$density}))) # convert hist column to character version of data-ui htmlwidget dat <- dat %>% mutate(hist = lapply(hist, function(h){ dui_chr( dui_sparkline( data = h$density, max = max_density, height = 80, margin = list(top = 0, bottom = 0, left = 20, right = 0), components = list( dui_sparkbarseries(), dui_tooltip( list( dui_sparkhorizontalrefline( stroke = "#ccc", strokeDasharray = "4,4" ), dui_sparkpointseries( renderLabel = htmlwidgets::JS("(d) => d ? (d*100000).toFixed(2) + '%' : null"), labelPosition = "right" ) ) ) ) ) ) })) # datatable library(DT) DT::datatable( dat, escape = FALSE, options = list( fnDrawCallback = htmlwidgets::JS('function(){ HTMLWidgets.staticRender()}') ) ) %>% formatCurrency("mean_price", digits = 0) %>% formatRound("n", digits = 0) %>% dui_add_deps()
library(htmltools) # not that you would want to do this but illustrates the usage of helper functions browsable( dui_add_deps( tags$div( dui_chr( dui_sparkline( data = runif(100), components = list( dui_sparklineseries(strokeDasharray = "3,3"), dui_sparkpointseries(points = list("min","max")) ) ) ) ) ) ) library(dplyr) library(htmltools) library(dataui) data("diamonds", package="ggplot2") hist_all <- hist(diamonds$price, breaks = 20, plot = FALSE) dat <- diamonds %>% group_by(cut) %>% summarize( n = n(), mean_price = mean(price), hist = list(unclass(hist(price, breaks = hist_all$breaks, plot = FALSE))) ) # calculate max so all sparklines use same scale max_density <- max(unlist(lapply(dat$hist, function(x) {x$density}))) # convert hist column to character version of data-ui htmlwidget dat <- dat %>% mutate(hist = lapply(hist, function(h){ dui_chr( dui_sparkline( data = h$density, max = max_density, height = 80, margin = list(top = 0, bottom = 0, left = 20, right = 0), components = list( dui_sparkbarseries(), dui_tooltip( list( dui_sparkhorizontalrefline( stroke = "#ccc", strokeDasharray = "4,4" ), dui_sparkpointseries( renderLabel = htmlwidgets::JS("(d) => d ? (d*100000).toFixed(2) + '%' : null"), labelPosition = "right" ) ) ) ) ) ) })) # datatable library(DT) DT::datatable( dat, escape = FALSE, options = list( fnDrawCallback = htmlwidgets::JS('function(){ HTMLWidgets.staticRender()}') ) ) %>% formatCurrency("mean_price", digits = 0) %>% formatRound("n", digits = 0) %>% dui_add_deps()
Add 'dataui' Dependency to 'reactable'
dui_add_reactable_dep(rt)
dui_add_reactable_dep(rt)
rt |
|
reactable
htmlwidget with 'dataui' dependency attached
if(requireNamespace("reactable")) { dui_add_reactable_dep(reactable::reactable(mtcars)) }
if(requireNamespace("reactable")) { dui_add_reactable_dep(reactable::reactable(mtcars)) }
Bars for Histograms
dui_barseries(rawData = NULL, ...)
dui_barseries(rawData = NULL, ...)
rawData |
|
... |
see data-ui series |
reactR component
which is a special form of htmltools::tag
Create a character version of interactive 'data-ui' for use with other 'htmlwidgets', such as tables or tags.
dui_chr(dui)
dui_chr(dui)
dui |
|
library(htmltools) # not that you would want to do this but illustrates the usage of helper functions browsable( dui_add_deps( tags$div( dui_chr( dui_sparkline( data = runif(100), components = list( dui_sparklineseries(strokeDasharray = "3,3"), dui_sparkpointseries(points = list("min","max")) ) ) ) ) ) ) library(dplyr) library(htmltools) library(dataui) data("diamonds", package="ggplot2") hist_all <- hist(diamonds$price, breaks = 20, plot = FALSE) dat <- diamonds %>% group_by(cut) %>% summarize( n = n(), mean_price = mean(price), hist = list(unclass(hist(price, breaks = hist_all$breaks, plot = FALSE))) ) # calculate max so all sparklines use same scale max_density <- max(unlist(lapply(dat$hist, function(x) {x$density}))) # convert hist column to character version of data-ui htmlwidget dat <- dat %>% mutate(hist = lapply(hist, function(h){ dui_chr( dui_sparkline( data = h$density, max = max_density, height = 80, margin = list(top = 0, bottom = 0, left = 20, right = 0), components = list( dui_sparkbarseries(), dui_tooltip( list( dui_sparkhorizontalrefline( stroke = "#ccc", strokeDasharray = "4,4" ), dui_sparkpointseries( renderLabel = htmlwidgets::JS("(d) => d ? (d*100000).toFixed(2) + '%' : null"), labelPosition = "right" ) ) ) ) ) ) })) # datatable library(DT) DT::datatable( dat, escape = FALSE, options = list( fnDrawCallback = htmlwidgets::JS('function(){ HTMLWidgets.staticRender()}') ) ) %>% formatCurrency("mean_price", digits = 0) %>% formatRound("n", digits = 0) %>% dui_add_deps()
library(htmltools) # not that you would want to do this but illustrates the usage of helper functions browsable( dui_add_deps( tags$div( dui_chr( dui_sparkline( data = runif(100), components = list( dui_sparklineseries(strokeDasharray = "3,3"), dui_sparkpointseries(points = list("min","max")) ) ) ) ) ) ) library(dplyr) library(htmltools) library(dataui) data("diamonds", package="ggplot2") hist_all <- hist(diamonds$price, breaks = 20, plot = FALSE) dat <- diamonds %>% group_by(cut) %>% summarize( n = n(), mean_price = mean(price), hist = list(unclass(hist(price, breaks = hist_all$breaks, plot = FALSE))) ) # calculate max so all sparklines use same scale max_density <- max(unlist(lapply(dat$hist, function(x) {x$density}))) # convert hist column to character version of data-ui htmlwidget dat <- dat %>% mutate(hist = lapply(hist, function(h){ dui_chr( dui_sparkline( data = h$density, max = max_density, height = 80, margin = list(top = 0, bottom = 0, left = 20, right = 0), components = list( dui_sparkbarseries(), dui_tooltip( list( dui_sparkhorizontalrefline( stroke = "#ccc", strokeDasharray = "4,4" ), dui_sparkpointseries( renderLabel = htmlwidgets::JS("(d) => d ? (d*100000).toFixed(2) + '%' : null"), labelPosition = "right" ) ) ) ) ) ) })) # datatable library(DT) DT::datatable( dat, escape = FALSE, options = list( fnDrawCallback = htmlwidgets::JS('function(){ HTMLWidgets.staticRender()}') ) ) %>% formatCurrency("mean_price", digits = 0) %>% formatRound("n", digits = 0) %>% dui_add_deps()
Density Line Series for Histograms
dui_densityseries(rawData = NULL, ...)
dui_densityseries(rawData = NULL, ...)
rawData |
|
... |
see data-ui series |
reactR component
which is a special form of htmltools::tag
Convert 'dataui' to 'reactable' Custom Render Function
dui_for_reactable(dui, jsarg = "cellInfo")
dui_for_reactable(dui, jsarg = "cellInfo")
dui |
|
htmlwidgets::JS
of class JS_EVAL
if(requireNamespace("reactable")) { # create data frame with a list column df <- data.frame(x=1) df$x[1] <- list(1:10) dui_add_reactable_dep( reactable::reactable( df, columns = list( x = reactable::colDef( cell = dui_for_reactable(dui_sparkline( data = htmlwidgets::JS("cellInfo.value"), components=list( dui_sparklineseries() ) ) )) ) ) ) }
if(requireNamespace("reactable")) { # create data frame with a list column df <- data.frame(x=1) df$x[1] <- list(1:10) dui_add_reactable_dep( reactable::reactable( df, columns = list( x = reactable::colDef( cell = dui_for_reactable(dui_sparkline( data = htmlwidgets::JS("cellInfo.value"), components=list( dui_sparklineseries() ) ) )) ) ) ) }
Create interactive histogram visualizations with 'data-ui'. The histogram will
perform the calculations in 'JavaScript' if the data is raw. If you would like
more control over the calculation, then you can pass pre-binned values
with help from hist_to_binned_data
. dui_histogram
works well
as a full-featured visualization or can also be used as a 'sparkline' in smaller
contexts.
dui_histogram( rawData = NULL, binCount = NULL, binType = NULL, binValues = NULL, cumulative = NULL, horizontal = NULL, limits = NULL, margin = NULL, normalized = NULL, renderTooltip = NULL, valueAccessor = NULL, onMouseMove = NULL, onMouseLeave = NULL, tooltipData = NULL, ariaLabel = NULL, components = NULL, width = 600, height = 400, elementId = NULL )
dui_histogram( rawData = NULL, binCount = NULL, binType = NULL, binValues = NULL, cumulative = NULL, horizontal = NULL, limits = NULL, margin = NULL, normalized = NULL, renderTooltip = NULL, valueAccessor = NULL, onMouseMove = NULL, onMouseLeave = NULL, tooltipData = NULL, ariaLabel = NULL, components = NULL, width = 600, height = 400, elementId = NULL )
binCount |
|
binType |
|
binValues |
|
cumulative |
|
horizontal |
|
limits |
|
margin |
|
normalized |
|
renderTooltip |
|
valueAccessor |
|
onMouseMove , onMouseLeave
|
|
tooltipData |
currently not supported. |
ariaLabel |
|
components |
|
width , height
|
|
elementId |
|
react htmlwidget
Band Lines for 'Sparklines'
dui_sparkbandline(...)
dui_sparkbandline(...)
... |
reactR component
which is a special form of htmltools::tag
Bar Series for 'Sparklines'
dui_sparkbarseries(...)
dui_sparkbarseries(...)
... |
see data-ui series |
reactR component
which is a special form of htmltools::tag
Horizontal Reference Line for 'Sparklines'
dui_sparkhorizontalrefline(...)
dui_sparkhorizontalrefline(...)
... |
reactR component
which is a special form of htmltools::tag
Labels for 'Sparklines'
dui_sparklabel(...)
dui_sparklabel(...)
... |
see data-ui label |
reactR component
which is a special form of htmltools::tag
Create interactive 'sparklines' visualizations for use as a standalone component or
in combination with 'reactable' or other table libraries. There are a wide variety of
components for use with dui_sparkline
, including lines, bars, points, reference
lines, and labels. These 'sparklines' automatically include responsive and tooltip
behaviors.
dui_sparkline( data = NULL, className = NULL, margin = NULL, max = NULL, min = NULL, onMouseMove = NULL, onMouseLeave = NULL, renderTooltip = NULL, preserveAspectRatio = NULL, valueAccessor = NULL, viewBox = NULL, ariaLabel = NULL, components = list(), responsive = TRUE, width = "100%", height = 100, elementId = NULL )
dui_sparkline( data = NULL, className = NULL, margin = NULL, max = NULL, min = NULL, onMouseMove = NULL, onMouseLeave = NULL, renderTooltip = NULL, preserveAspectRatio = NULL, valueAccessor = NULL, viewBox = NULL, ariaLabel = NULL, components = list(), responsive = TRUE, width = "100%", height = 100, elementId = NULL )
data |
|
className |
|
margin |
|
max , min
|
|
onMouseMove , onMouseLeave
|
|
renderTooltip |
|
preserveAspectRatio |
|
valueAccessor |
|
viewBox |
|
ariaLabel |
|
components |
|
responsive |
|
width , height
|
|
elementId |
|
react htmlwidget
Linear Gradient for 'Sparklines'
dui_sparklineargradient(...)
dui_sparklineargradient(...)
... |
reactR component
which is a special form of htmltools::tag
Line Series for 'Sparklines'
dui_sparklineseries(...)
dui_sparklineseries(...)
... |
see data-ui series |
reactR component
which is a special form of htmltools::tag
Pattern Fill for 'Sparklines'
dui_sparkpatternlines(...)
dui_sparkpatternlines(...)
... |
reactR component
which is a special form of htmltools::tag
Point Series for 'Sparklines'
dui_sparkpointseries(...)
dui_sparkpointseries(...)
... |
see data-ui series |
reactR component
which is a special form of htmltools::tag
Horizontal Reference Line for 'Sparklines'
dui_sparkrefline(...)
dui_sparkrefline(...)
... |
reactR component
which is a special form of htmltools::tag
Vertical Reference Line for 'Sparklines'
dui_sparkverticalrefline(...)
dui_sparkverticalrefline(...)
... |
reactR component
which is a special form of htmltools::tag
Tooltip Container for 'Sparklines'
dui_tooltip(components)
dui_tooltip(components)
components |
|
reactR component
which is a special form of htmltools::tag
'X' Axis for Histograms
dui_xaxis(...)
dui_xaxis(...)
... |
see data-ui axis |
reactR component
which is a special form of htmltools::tag
'Y' Axis for Histograms
dui_yaxis(...)
dui_yaxis(...)
... |
see data-ui axis |
reactR component
which is a special form of htmltools::tag
This is a convenience function to convert a histogram object from hist
into the binned value format expected by 'data-ui' dui_barseries
and dui_densityseries
argument binnedData
.
hist_to_binned_data(h = NULL, density = TRUE)
hist_to_binned_data(h = NULL, density = TRUE)
h |
histogram object or list from |
density |
|
list
library(dataui) h <- graphics::hist(stats::rnorm(100), plot = FALSE) dui_histogram( components = list( dui_barseries(binnedData = hist_to_binned_data(h)) ) )
library(dataui) h <- graphics::hist(stats::rnorm(100), plot = FALSE) dui_histogram( components = list( dui_barseries(binnedData = hist_to_binned_data(h)) ) )
'dataui' also ships a standalone version of the 'JavaScript' allowing
for usage outside of a traditional htmlwidget
context. The variable dataui
is added to window
object. Usage of html_dependency_dataui
should likely
be accompanied with html_dependency_react
along with
html_dependency_reacttools
for hydrate
.
html_dependency_dataui()
html_dependency_dataui()
htmltools::htmlDependency
library(htmltools) library(dataui) browsable( tagList( reactR::html_dependency_react(), reactR::html_dependency_reacttools(), html_dependency_dataui(), tags$div(id = "chart"), tags$div(id = "chart-hydrate"), tags$div(id = "chart-hydrate-fromwidget"), tags$script(HTML( sprintf( " const sparklineProps = { ariaLabel: 'This is a Sparkline of...', width: 500, height: 100, margin: { top: 24, right: 80, bottom: 24, left: 8 }, }; const data = {data: [1,2,3,4,5,6]} const renderTooltip = function (_ref) { var datum = _ref.datum; return React.createElement( 'div', null, datum.x && React.createElement( 'div', null, datum.x ), React.createElement( 'div', null, datum.y ? datum.y.toFixed(2) : \"--\" ) ); } const spk = React.createElement( dataui.SparklineWithTooltip, Object.assign( data, sparklineProps, {renderTooltip: renderTooltip} ), React.createElement(dataui.SparklineLineSeries) ) ReactDOM.render(spk, document.getElementById('chart')) const spk_hydrate = window.reactR.hydrate( dataui, { name: 'SparklineWithTooltip', attribs: {...sparklineProps, ...data, ...renderTooltip}, children: [ {name: 'SparklineLineSeries', attribs: {stroke: 'purple'}, children: []} ] } ) ReactDOM.render(spk_hydrate, document.getElementById('chart-hydrate')) const spk_hydrate_from_widget = window.reactR.hydrate( dataui, %s ) ReactDOM.render(spk_hydrate_from_widget, document.getElementById('chart-hydrate-fromwidget')) ", jsonlite::toJSON( dui_sparkline(data = 1:6, components = list(dui_sparklineseries(stroke="gray")))$x$tag, auto_unbox = TRUE, force = TRUE ) ) )) ) )
library(htmltools) library(dataui) browsable( tagList( reactR::html_dependency_react(), reactR::html_dependency_reacttools(), html_dependency_dataui(), tags$div(id = "chart"), tags$div(id = "chart-hydrate"), tags$div(id = "chart-hydrate-fromwidget"), tags$script(HTML( sprintf( " const sparklineProps = { ariaLabel: 'This is a Sparkline of...', width: 500, height: 100, margin: { top: 24, right: 80, bottom: 24, left: 8 }, }; const data = {data: [1,2,3,4,5,6]} const renderTooltip = function (_ref) { var datum = _ref.datum; return React.createElement( 'div', null, datum.x && React.createElement( 'div', null, datum.x ), React.createElement( 'div', null, datum.y ? datum.y.toFixed(2) : \"--\" ) ); } const spk = React.createElement( dataui.SparklineWithTooltip, Object.assign( data, sparklineProps, {renderTooltip: renderTooltip} ), React.createElement(dataui.SparklineLineSeries) ) ReactDOM.render(spk, document.getElementById('chart')) const spk_hydrate = window.reactR.hydrate( dataui, { name: 'SparklineWithTooltip', attribs: {...sparklineProps, ...data, ...renderTooltip}, children: [ {name: 'SparklineLineSeries', attribs: {stroke: 'purple'}, children: []} ] } ) ReactDOM.render(spk_hydrate, document.getElementById('chart-hydrate')) const spk_hydrate_from_widget = window.reactR.hydrate( dataui, %s ) ReactDOM.render(spk_hydrate_from_widget, document.getElementById('chart-hydrate-fromwidget')) ", jsonlite::toJSON( dui_sparkline(data = 1:6, components = list(dui_sparklineseries(stroke="gray")))$x$tag, auto_unbox = TRUE, force = TRUE ) ) )) ) )