Viz data table in R

Basic

reactable::reactable(df,
                     defaultSorted = list(cut="asc",
                                          avg_price = "desc"
                                          ),
                     searchable = TRUE,
                     bordered = TRUE,
                     highlight = TRUE)
df %>% DT::datatable() %>% 
  formatCurrency(columns="avg_price",
                 currency = "$",
                 digits = 2)

Groupby in the table

reactable::reactable(df, groupBy = "cut",
                     columns = list(
                       avg_price = colDef(
                         aggregate = "mean", 
                         format=colFormat(prefix = "$", 
                                          separators = TRUE,
                                          digits = 2)
                         ),
                       color = colDef(aggregate = "frequency")
                     ),
                     defaultSortOrder = "desc",
                     searchable = TRUE,
                     bordered = TRUE,
                     highlight = TRUE)
data <- data.frame(
  States = state.name,
  Region = state.region,
  Area = state.area
)

reactable(data, groupBy = "Region", columns = list(
  States = colDef(
    aggregate = "count",
    format = list(
      aggregated = colFormat(suffix = " states")
    )
  ),
  Area = colDef(
    aggregate = "sum",
    format = colFormat(suffix = " mi²", separators = TRUE)
  )
))

Embedding HTML widgets

library(sparkline)

data <- chickwts %>%
  group_by(feed) %>%
  summarise(weight = list(weight)) %>%
  mutate(boxplot = NA, sparkline = NA)

reactable(data, columns = list(
  weight = colDef(cell = function(values) {
    sparkline(values, type = "bar", chartRangeMin = 0, chartRangeMax = max(chickwts$weight))
  }),
  boxplot = colDef(cell = function(value, index) {
    sparkline(data$weight[[index]], type = "box")
  }),
  sparkline = colDef(cell = function(value, index) {
    sparkline(data$weight[[index]])
  })
))
Avatar
Ray Sun
Data Analytics Professional

My interests include AI/ML and data analytics.

Related