Skip to content

Instantly share code, notes, and snippets.

@DeepanshKhurana
Created March 31, 2023 16:11
Show Gist options
  • Save DeepanshKhurana/5900db484089057b1ea9c8857e1eec76 to your computer and use it in GitHub Desktop.
Save DeepanshKhurana/5900db484089057b1ea9c8857e1eec76 to your computer and use it in GitHub Desktop.
Reactable Multi-row Example
#' @export
#'
true_round <- function(number, digits) {
number <- as.numeric(number)
posneg <- sign(number)
number <- abs(number) * 10^digits
number <- number + 0.5 + sqrt(.Machine$double.eps)
number <- trunc(number)
number <- number / 10 ^ digits
number * posneg
}
#' @export
#'
#' @description function to format price/change values
#' @param value the value to format
#' @param round the digits to round till
#' @param format the number format "d" or "f"
#' @param big the separator for big values, default ","
#' @return character url for the sheet for the user
#'
format_price <- function(value, round = 2, format = "f", big = ",") {
formatC(
as.numeric(
as.character(true_round(value, round))),
digits = round,
format = format,
big.mark = big
)
}
#' @export
#'
shorten_price <- function(value) {
value <- as.numeric(format_price(value = value, big = ""))
dplyr::case_when(
value < 1e3 ~ as.character(value),
value < 1e6 ~ paste0(as.character(true_round(value / 1e3, 1)), "K"),
value < 1e9 ~ paste0(as.character(true_round(value / 1e6, 1)), "M"),
TRUE ~ paste0(as.character(true_round(value / 1e9, 1)), "B")
)
}
source("functions.R")
library(reactable)
ui <- fluidPage(
reactableOutput("table")
)
server <- function(input, output, session) {
data <- mtcars |>
mutate(disp = disp * 10000) |>
select(disp, hp) |>
mutate(disp_ = paste(disp, hp, sep = "_"))
reactable_reactive <- reactive({
reactable(data,
columns = list(
disp_ = colDef(
cell = function(value) {
value <- strsplit(value, "_")[[1]]
div(class = "row_container",
div(class = "amount",
shorten_price(value[1])),
div(class = "year",
value[2]))
}
),
disp = colDef(
show = FALSE
),
hp = colDef(
show = FALSE
)
))
})
output$table <- renderReactable({
reactable_reactive()
})
}
shiny::shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment