Skip to content

Commit

Permalink
add wrapper function for reactable
Browse files Browse the repository at this point in the history
  • Loading branch information
oadetayo committed Jan 13, 2025
1 parent 2de5915 commit 6d06dc3
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Imports:
shinyGovstyle,
shinyjs,
stringr,
styler
styler,
reactable
Suggests:
devtools,
diffviewer,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(cookies_panel_server)
export(cookies_panel_ui)
export(custom_disconnect_message)
export(dfe_cookies_script)
export(dfe_reactable)
export(external_link)
export(header)
export(init_analytics)
Expand Down
105 changes: 105 additions & 0 deletions R/dfe_reactable.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#' Department for Education Reactable Wrapper
#'
#' A wrapper around the `reactable` function for creating styled, accessible,
#' and user-friendly tables tailored to the Department for Education's requirements.
#'
#' @param data A data frame to display in the table.
#' @param columns A named list of column definitions created with `reactable::colDef()`.
#' @param columnGroups A list of column groups created with `reactable::colGroup()`.
#' @param rownames Logical or character. If `TRUE`, show row names. If a string, the row names
#' will be displayed in a column with that name. Defaults to `NULL`.
#' @param groupBy A character vector of column names to group rows by.
#' @param sortable Logical. Enable sorting for all columns by default.
#' @param filterable Logical. Enable column filters.
#' @param searchable Logical. Add a global search box.
#' @param searchMethod Custom search function for global search.
#' @param defaultColDef Default column settings created with `reactable::colDef()`.
#' @param defaultColGroup Default column group settings created with `reactable::colGroup()`.
#' @param defaultSortOrder The default sort order for sortable columns. One of `"asc"` or `"desc"`.
#' @param defaultSorted A named list or character vector of columns to sort by default.
#' @param pagination Logical. Enable pagination.
#' @param defaultPageSize Number of rows to show per page by default.
#' @param showPageSizeOptions Logical. Allow users to change the page size.
#' @param pageSizeOptions A vector of page size options for the page size dropdown.
#' @param paginationType Pagination control type. One of `"numbers"` or `"jump"`.
#' @param showPagination Logical. Show pagination controls.
#' @param showPageInfo Logical. Show page info (e.g., "1–10 of 100 rows").
#' @param minRows Minimum number of rows to show, even when the data has fewer rows.
#' @param paginateSubRows Logical. Paginate sub rows when rows are grouped.
#' @param details A function or formula for creating expandable row details.
#' @param defaultExpanded Logical or character vector. Expand rows by default.
#' @param selection Selection mode. One of `"multiple"`, `"single"`, or `"none"`.
#' @param defaultSelected A character vector of row names or indices to select by default.
#' @param onClick Callback function for row click events.
#' @param striped Logical. Add zebra-striping to rows.
#' @param compact Logical. Make rows compact.
#' @param wrap Logical. Allow cell text to wrap.
#' @param class CSS class to apply to the table.
#' @param style Inline styles to apply to the table.
#' @param rowClass CSS class to apply to each row.
#' @param rowStyle Inline styles to apply to each row.
#' @param width Table width in pixels or as a CSS value.
#' @param height Table height in pixels or as a CSS value.
#' @param meta Arbitrary metadata to include with the table.
#' @param elementId Element ID for the table widget.
#' @param static Logical. Render a static (non-interactive) table.
#' @param selectionId Element ID to store selected row indices.
#' @param ... Additional arguments passed to `reactable::reactable`.
#'
#' @details
#' The `dfe_reactable` function provides a pre-configured version of the `reactable` function with:
#' \itemize{
#' \item **Highlighting**: Row highlighting enabled.
#' \item **Borderless Table**: Removes borders for a clean look.
#' \item **Sort Icons Hidden**: Sort icons are not displayed by default.
#' \item **Resizable Columns**: Users can resize columns.
#' \item **Full Width**: Table expands to the full width of the container.
#' \item **Default Column Definition**: Custom column header styles, NA value handling,
#' and alignment.
#' \item **Custom Search Input**: A search bar styled to the Department for Education's specifications.

Check notice

Code scanning / lintr

Lines should not be more than 100 characters. This line is 105 characters. Note

Lines should not be more than 100 characters. This line is 105 characters.
#' \item **Custom Language**: Provides a user-friendly search placeholder text.
#' }
#'
#' @return A `reactable` HTML widget object.
#'
#' @examples
#' if (interactive()) {
#' library(reactable)
#' dfe_reactable(mtcars)
#' }
#'
#' @export


dfe_reactable <- function(data, ...) {
reactable::reactable(
data,
highlight = TRUE,
borderless = TRUE,
showSortIcon = FALSE,
resizable = TRUE,
fullWidth = TRUE,
defaultColDef = reactable::colDef(
headerClass = "bar-sort-header",
html = TRUE,
na = "NA",
minWidth = 65,
align = "left"
),
language = reactable::reactableLang(
searchPlaceholder = "Search table..."
),
theme = reactable::reactableTheme(
searchInputStyle = list(
float = "right",
width = "25%",
marginBottom = "10px",
padding = "5px",
fontSize = "14px",
border = "1px solid #ccc",
borderRadius = "5px"
)
),
...
)
}
120 changes: 120 additions & 0 deletions man/dfe_reactable.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions tests/testthat/test-dfe_reactable.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
test_that("dfe_reactable produces a properly configured reactable object", {
# Generate a small sample dataset
sample_data <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35),
Score = c(85.5, 90.3, 88.7)
)

# Run the function
table_output <- dfe_reactable(sample_data)

# Test if the output is a reactable object
expect_s3_class(table_output, "reactable")
expect_s3_class(table_output, "htmlwidget")

# Check the main attributes
expect_type(table_output$x, "list")
expect_true("tag" %in% names(table_output$x))

# Check the tag attribs
attribs <- table_output$x$tag$attribs
expect_type(attribs, "list")

# Verify key preconfigured attributes
expect_equal(attribs$resizable, TRUE)
expect_equal(attribs$highlight, TRUE)
expect_equal(attribs$borderless, TRUE)
expect_equal(attribs$showSortIcon, FALSE)

# Verify column definitions
columns <- attribs$columns
expect_equal(length(columns), 3) # Ensure 3 columns are defined

# Validate the first column's attributes
col1 <- columns[[1]]
expect_equal(col1$id, "Name")
expect_equal(col1$name, "Name")
expect_equal(col1$type, "character")
expect_equal(col1$html, TRUE)
expect_equal(col1$headerClassName, "bar-sort-header")

# Validate language configuration
language <- attribs$language
expect_type(language, "list")
expect_equal(language$searchPlaceholder, "Search table...")

# Validate theme configuration
theme <- attribs$theme
expect_type(theme, "list")
expect_equal(theme$searchInputStyle$float, "right")
expect_equal(theme$searchInputStyle$width, "25%")
expect_equal(theme$searchInputStyle$border, "1px solid #ccc")

# Validate the widget's overall class and package
expect_equal(attr(table_output, "class"), c("reactable", "htmlwidget"))
expect_equal(attr(table_output, "package"), "reactable")
})

0 comments on commit 6d06dc3

Please sign in to comment.