Skip to content

Instantly share code, notes, and snippets.

@hypebright
Last active May 18, 2022 14:38
Show Gist options
  • Save hypebright/e3e44498d097484990f870ffeec597c2 to your computer and use it in GitHub Desktop.
Save hypebright/e3e44498d097484990f870ffeec597c2 to your computer and use it in GitHub Desktop.
Demo of the reactable package in Shiny using UK pharmaceutical data
# libraries
library(reactable) # version 0.2.3.9000
library(data.table)
library(shiny)
# Read in the data
# Data contains prescribing quantities for Aciclovir (limited range) and Pregabalin in February 2022
# in England, Scotland, Northern Ireland and Wales
# Made available under the Open Government License, retrieved from Pharmly Cloud Data by Analytic Health
prescribing_data <- read.delim('https://gist.githubusercontent.com/hypebright/6d78ad3645ec3eb82e31fbe85cb41547/raw/af7fbfc4f0b46e564549b957c62d8b81dbcd185e/reactable_demo_example_data.csv',
sep = ';')
setDT(prescribing_data)
prescribing_data <- prescribing_data[Country == 'England', .(Units = sum(Units)),
by = .(`Active.Ingredients`, Strength, Dosage, Brand)]
# Color palette function
color_pal <- function(x) {
rgb(colorRamp(c('#cfcfcf', '#26E7A4'))(x), maxColorValue = 255)
}
ui <- fluidPage(
titlePanel('Reactable in Shiny'),
reactableOutput('table')
)
server <- function(input, output, session) {
output$table <- renderReactable({
reactable(data = prescribing_data,
searchable = TRUE,
wrap = FALSE,
resizable = TRUE,
bordered = TRUE,
fullWidth = TRUE,
showPageSizeOptions = TRUE,
pageSizeOptions = c(10, 15, 25, 50),
sortable = TRUE,
highlight = TRUE,
compact = TRUE,
striped = FALSE,
minRows = 10,
defaultColDef = colDef(
headerStyle = list(position = "sticky", left = 0, background = "#f2f2f2", zIndex = 1)
),
groupBy = c('Active.Ingredients', 'Dosage', 'Brand'),
theme = reactableTheme(
searchInputStyle = list(width = "20%", marginRight = '80%', height = '38px')
),
columns = list(
`Active.Ingredients` = colDef(name = 'Active Ingredients',
minWidth = 200),
Dosage = colDef(name = 'Dosage',
minWidth = 200,
aggregate = JS("function(values, rows) {
if (values.length === 1) {
return values
} else {
return new Set(values).size + ' dosage(s)'
}
}")),
Strength = colDef(name = 'Strength',
minWidth = 250,
aggregate = JS("function(values, rows) {
if (values.length === 1) {
return values
} else {
return new Set(values).size + ' strength(s)'
}
}")),
Brand = colDef(name = 'Brand',
minWidth = 200,
aggregate = JS("function(values, rows) {
if (values.length === 1) {
return values
} else {
return new Set(values).size + ' brand(s)'
}
}")),
Units = colDef(name = 'Units',
aggregate = "sum",
minWidth = 150,
format = colFormat(digits = 0, separators = TRUE),
footer = JS("
function(colInfo) {
let total = 0
colInfo.data.forEach(function(row) {
total += row[colInfo.column.id]
})
return total.toLocaleString(0)
}"))))
})
}
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment