From 0fa61e756c0da13e953d13ada309aa52fd0b5dd6 Mon Sep 17 00:00:00 2001 From: Stefan Kuethe Date: Tue, 17 Sep 2024 08:36:43 +0200 Subject: [PATCH] Refactor --- NAMESPACE | 4 ++- R/input_handlers.R | 4 +++ R/tabulator_options.R | 26 ++++++++++---- R/utils.R | 8 +++-- _pkgdown.yml | 3 +- examples/options/group_by_options.R | 2 ++ examples/options/pagination_options.R | 2 ++ man/list_to_data_frame.Rd | 4 +-- ...on_group_by.Rd => set_options_group_by.Rd} | 10 +++--- man/set_options_pagination.Rd | 35 +++++++++++++++++++ man/tabulator_data_as_df.Rd | 14 ++++++++ vignettes/articles/rtabulator.Rmd | 14 ++++++-- 12 files changed, 104 insertions(+), 22 deletions(-) create mode 100644 R/input_handlers.R create mode 100644 examples/options/group_by_options.R create mode 100644 examples/options/pagination_options.R rename man/{set_option_group_by.Rd => set_options_group_by.Rd} (85%) create mode 100644 man/set_options_pagination.Rd create mode 100644 man/tabulator_data_as_df.Rd diff --git a/NAMESPACE b/NAMESPACE index 135544f..75fa6ac 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,12 +25,14 @@ export(set_formatter_toggle_switch) export(set_formatter_traffic_light) export(set_header_filter) export(set_multi_column_header) -export(set_option_group_by) +export(set_options_group_by) +export(set_options_pagination) export(set_tooltip) export(spreadsheet_def) export(tabulator) export(tabulatorContext) export(tabulatorOutput) +export(tabulator_data_as_df) export(tabulator_options) export(titanic) export(trigger_download) diff --git a/R/input_handlers.R b/R/input_handlers.R new file mode 100644 index 0000000..1cc94d3 --- /dev/null +++ b/R/input_handlers.R @@ -0,0 +1,4 @@ +# https://book.javascript-for-r.com/shiny-complete.html +input_handler_data <- function(data, ...) { + return(tabulator_data_as_df(data)) +} diff --git a/R/tabulator_options.R b/R/tabulator_options.R index 8fff97c..3ea29e5 100644 --- a/R/tabulator_options.R +++ b/R/tabulator_options.R @@ -96,28 +96,40 @@ default_spreadsheet_options <- list( spreadsheet_column_definition = list(editor = "input") ) -# TODO: Helper function to set pagination -set_option_pagination <- function( +#' Set pagination options +#' @inheritParams set_formatter_html +#' @inheritParams tabulator_options +#' @example examples/options/pagination_options.R +#' @export +set_options_pagination <- function( widget, - pagination = FALSE, + pagination = TRUE, pagination_size = 10, pagination_size_selector = FALSE, pagination_add_row = c("page", "table"), ...) { - return(widget) + # Body + options_update <- list( + pagination = pagination, + paginationSize = pagination_size, + paginationSizeSelector = pagination_size_selector, + paginationAddRow = match.arg(pagination_add_row), + ... + ) + modify_tabulator_options(widget, options_update) } -#' Set group by option +#' Set group by options #' @inheritParams set_formatter_html #' @inheritParams tabulator_options #' @export -set_option_group_by <- function( +set_options_group_by <- function( widget, group_by, group_start_open = TRUE, group_toggle_element = "header", ...) { - # return(widget) + # Body options_update <- list( groupBy = group_by, groupStartOpen = group_start_open, diff --git a/R/utils.R b/R/utils.R index aef4ced..0a5872f 100644 --- a/R/utils.R +++ b/R/utils.R @@ -11,7 +11,7 @@ compact <- function(x) { x[!sapply(x, is.null)] } -#' Parse List to Data Frame +#' Convert a list of names lists to data frame #' @param x (list): A list of named lists. #' @export list_to_data_frame <- function(x) { @@ -19,8 +19,10 @@ list_to_data_frame <- function(x) { return(do.call(rbind.data.frame, x)) } -tabulator_data_as_data_frame <- function(data) { +#' Convert input data to data frame +#' @param data (list): The input data returned in a Shiny app. +#' @export +tabulator_data_as_df <- function(data) { return(as.data.frame(purrr::map(data, ~ unlist(.x)))) } -# as.data.frame(purrr::map(input$data, ~ unlist(.x))) diff --git a/_pkgdown.yml b/_pkgdown.yml index 768d2fd..42308f9 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -9,7 +9,8 @@ reference: contents: - tabulator - tabulator_options - - set_option_group_by + - set_options_group_by + - set_options_pagination - title: Column Settings desc: > diff --git a/examples/options/group_by_options.R b/examples/options/group_by_options.R new file mode 100644 index 0000000..3a8f719 --- /dev/null +++ b/examples/options/group_by_options.R @@ -0,0 +1,2 @@ +tabulator(iris) |> + set_options_group_by("Species", group_start_open = FALSE) diff --git a/examples/options/pagination_options.R b/examples/options/pagination_options.R new file mode 100644 index 0000000..208d742 --- /dev/null +++ b/examples/options/pagination_options.R @@ -0,0 +1,2 @@ +tabulator(iris) |> + set_options_pagination(pagination_size_selector = c(10, 20, 50)) diff --git a/man/list_to_data_frame.Rd b/man/list_to_data_frame.Rd index dc586a4..fb0e2ce 100644 --- a/man/list_to_data_frame.Rd +++ b/man/list_to_data_frame.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/utils.R \name{list_to_data_frame} \alias{list_to_data_frame} -\title{Parse List to Data Frame} +\title{Convert a list of names lists to data frame} \usage{ list_to_data_frame(x) } @@ -10,5 +10,5 @@ list_to_data_frame(x) \item{x}{(list): A list of named lists.} } \description{ -Parse List to Data Frame +Convert a list of names lists to data frame } diff --git a/man/set_option_group_by.Rd b/man/set_options_group_by.Rd similarity index 85% rename from man/set_option_group_by.Rd rename to man/set_options_group_by.Rd index 888a0b4..116d789 100644 --- a/man/set_option_group_by.Rd +++ b/man/set_options_group_by.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/tabulator_options.R -\name{set_option_group_by} -\alias{set_option_group_by} -\title{Set group by option} +\name{set_options_group_by} +\alias{set_options_group_by} +\title{Set group by options} \usage{ -set_option_group_by( +set_options_group_by( widget, group_by, group_start_open = TRUE, @@ -27,5 +27,5 @@ Set to \code{FALSE} to disable toggling at all.} \item{...}{Further options.} } \description{ -Set group by option +Set group by options } diff --git a/man/set_options_pagination.Rd b/man/set_options_pagination.Rd new file mode 100644 index 0000000..6e59cd9 --- /dev/null +++ b/man/set_options_pagination.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tabulator_options.R +\name{set_options_pagination} +\alias{set_options_pagination} +\title{Set pagination options} +\usage{ +set_options_pagination( + widget, + pagination = TRUE, + pagination_size = 10, + pagination_size_selector = FALSE, + pagination_add_row = c("page", "table"), + ... +) +} +\arguments{ +\item{widget}{(\code{\link{tabulator}}) A tabulator widget.} + +\item{pagination}{(bool): Whether to enable pagination.} + +\item{pagination_size}{(integer): Number of rows on each page.} + +\item{pagination_size_selector}{(list): Add pagination size selector.} + +\item{pagination_add_row}{(character): Where to add rows to the table when pagination is enabled.} + +\item{...}{Further options.} +} +\description{ +Set pagination options +} +\examples{ +tabulator(iris) |> + set_options_pagination(pagination_size_selector = c(10, 20, 50)) +} diff --git a/man/tabulator_data_as_df.Rd b/man/tabulator_data_as_df.Rd new file mode 100644 index 0000000..ecc31df --- /dev/null +++ b/man/tabulator_data_as_df.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{tabulator_data_as_df} +\alias{tabulator_data_as_df} +\title{Convert input data to data frame} +\usage{ +tabulator_data_as_df(data) +} +\arguments{ +\item{data}{(list): The input data returned in a Shiny app.} +} +\description{ +Convert input data to data frame +} diff --git a/vignettes/articles/rtabulator.Rmd b/vignettes/articles/rtabulator.Rmd index dbd5cc3..32ff7ac 100644 --- a/vignettes/articles/rtabulator.Rmd +++ b/vignettes/articles/rtabulator.Rmd @@ -27,7 +27,7 @@ tabulator(airquality) In this case, the column definitions are automatically created for you. The horizontal alignment for character columns is set to _left_ while numeric columns are aligned _right_. -It is also possible to pass pass a data URL: +It is also possible to pass a data URL: ```{r} data_url <- "https://raw.githubusercontent.com/eodaGmbH/rtabulator/main/data-raw/titanic.csv" @@ -64,6 +64,14 @@ setup <- tabulator_options( tabulator(USArrests, setup) ``` +*** + +**Note** + +If you prefer the pipe style, you can also use the helper function `set_options_pagination()`. + +*** + ## Column Formatters To customize your columns, the easiest way is to use the `set_formatter_*()` functions: @@ -75,7 +83,7 @@ tabulator(airquality) |> set_tooltip("Ozone") ``` -With `for_each_col()` you can apply a formatter to multiple columns: +With `for_each_col()` you can apply a formatter and any other function that updates the column settings to multiple columns: ```{r} numeric_cols <- c("Sepal_Length", "Sepal_Width", "Petal_Length", "Petal_Width") @@ -146,7 +154,7 @@ tabulator(titanic_df, setup) **Note** -You can also use `set_option_group_by()` to set the group by options. +You can also use `set_options_group_by()` to set the group by options. ***