Skip to content

Instantly share code, notes, and snippets.

@shannonpileggi
Last active March 26, 2021 11:20
Show Gist options
  • Save shannonpileggi/54545cb294294ca879bd2040ebf3f816 to your computer and use it in GitHub Desktop.
Save shannonpileggi/54545cb294294ca879bd2040ebf3f816 to your computer and use it in GitHub Desktop.
Demo of DT::datatable column indexing
# libraries
library(tidyverse)
library(DT)
set.seed(1234)
# generate data
dat <- tibble(
A = runif(5, 0, 1),
B = runif(5, 0, 1),
C = runif(5, 0, 1),
)
# initial table with no formatting
DT::datatable(dat,rownames = FALSE)
# formatted table
# use columnDefs to center columns; columns are indexed at 0
# use formatRound to round values; columns are indexed at 1
DT::datatable(dat,
rownames = FALSE,
options = list(columnDefs = list(
list(className = 'dt-center',
targets = 0:2)
))) %>%
DT::formatRound(1:3, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment