Skip to content

Commit

Permalink
Add some helper funcs and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kuethe committed Sep 16, 2024
1 parent 8b464d0 commit 12640cb
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 18 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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_tooltip)
export(spreadsheet_def)
export(tabulator)
export(tabulatorContext)
export(tabulatorOutput)
export(tabulator_options)
export(titanic)
export(trigger_download)
export(trigger_get_data)
export(trigger_get_spreadsheet_data)
Expand Down
6 changes: 4 additions & 2 deletions R/columns.R
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,10 @@ set_column_editor <- function(widget, columns, type = c("input", "number")) {
set_editor <- function(
widget,
column,
editor = c("input", "textarea", "number", "range",
"tickCross", "star", "progress", "date", "time", "datetime", "list"),
editor = c(
"input", "textarea", "number", "range",
"tickCross", "star", "progress", "date", "time", "datetime", "list"
),
validator = NULL,
...) {
# Body
Expand Down
10 changes: 10 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Data ####

#' Titanic data set
#' @param col_select (character vector): Columns to select.
#' @export
titanic <- function(col_select = NULL) {
titanic_data_url <- "https://raw.githubusercontent.com/eodaGmbH/rtabulator/main/data-raw/titanic.csv"

readr::read_csv(titanic_data_url, col_select = !!col_select, show_col_types = FALSE)
}
8 changes: 0 additions & 8 deletions R/experimental.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,3 @@ get_theme_dependeny <- function(
all_files = FALSE
)
}

# Data ####

titanic <- function(col_select = NULL) {
titanic_data_url <- "https://raw.githubusercontent.com/eodaGmbH/rtabulator/main/data-raw/titanic.csv"

readr::read_csv(titanic_data_url, col_select = !!col_select, show_col_types = FALSE)
}
48 changes: 47 additions & 1 deletion R/tabulator_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
#' @param resizable_rows (bool): Allow user to resize rows.
#' @param frozen_rows (numeric): Number of frozen rows.
#' @param index (character): Field to be used as the unique index for each row.
#' @param group_by (character): Field to group rows by.
#' @param group_by (character vector): Field to group rows by. Pass multiple values
#' for multi level grouping.
#' @param group_start_open (bool vector): Open state of groups when grouping is enabled.
#' The length of the vector depends on the number of levels set with \code{group_by}.
#' @param group_toggle_element (character, bool): One of \code{arrow} or \code{header}.
#' Set to \code{FALSE} to disable toggling at all.
#' @param edit_trigger_event (character): Event that triggers a cell edit.
#' @param selectable_rows (character, bool, integer) Set to \code{FALSE} to disble row selection.
#' If set to \code{TRUE} you can select as many rows as you want.
Expand Down Expand Up @@ -54,6 +59,8 @@ tabulator_options <- function(
index = "id",
# Row Grouping
group_by = NULL,
group_start_open = TRUE,
group_toggle_element = NULL,
# Editing
edit_trigger_event = c("dblclick", "click", "focus"),
# Selection
Expand Down Expand Up @@ -88,3 +95,42 @@ default_spreadsheet_options <- list(
),
spreadsheet_column_definition = list(editor = "input")
)

# TODO: Helper function to set pagination
set_option_pagination <- function(
widget,
pagination = FALSE,
pagination_size = 10,
pagination_size_selector = FALSE,
pagination_add_row = c("page", "table"),
...) {
return(widget)
}

#' Set group by option
#' @inheritParams set_formatter_html
#' @inheritParams tabulator_options
#' @export
set_option_group_by <- function(
widget,
group_by,
group_start_open = TRUE,
group_toggle_element = "header",
...) {
# return(widget)
options_update <- list(
groupBy = group_by,
groupStartOpen = group_start_open,
groupToggleElement = group_toggle_element,
...
)
modify_tabulator_options(widget, options_update)
}

modify_tabulator_options <- function(widget, options) {
widget$x$options <- utils::modifyList(
widget$x$options,
keys_to_camel_case(compact(options))
)
return(widget)
}
19 changes: 13 additions & 6 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ reference:
contents:
- tabulator
- tabulator_options
- set_option_group_by

- title: Column Settings
desc: >
Functions to update the default column settings
contents:
- set_column_defaults
- set_editor
- set_header_filter
- set_multi_column_header
- set_column_defaults
- set_tooltip
- for_each_col

- title: Column Formatters
desc: >
Expand All @@ -24,11 +32,6 @@ reference:
contents:
- set_calculation

- title: Misc
contents:
- set_tooltip
- for_each_col

- title: Shiny Integration
contents:
- rtabulator-shiny
Expand All @@ -48,3 +51,7 @@ reference:
- spreadsheet_def
- list_to_data_frame
- create_columns

- title: Data Sets
contents:
- titanic
31 changes: 31 additions & 0 deletions man/set_option_group_by.Rd

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

11 changes: 10 additions & 1 deletion man/tabulator_options.Rd

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

14 changes: 14 additions & 0 deletions man/titanic.Rd

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

45 changes: 45 additions & 0 deletions vignettes/articles/rtabulator.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,57 @@ tabulator(iris) |>
```

***

**Note**

If further functions update the column definitions, `set_multi_column_header()` must be the last one. Therefore, in the example above `set_header_filter()` is called before we set the multi column header.

***

## Groupings

```{r}
setup <- tabulator_options(
group_by = c("Pclass"),
group_start_open = FALSE,
group_toggle_element = "header"
)
titanic_df <- titanic(c("Pclass", "Sex", "Age", "Survived"))
tabulator(titanic_df, setup) |>
set_formatter_traffic_light("Survived", color = c("red", "green"))
```

Multi level grouping is done by passing a vector as `group_by` parameter:

```{r}
setup <- tabulator_options(
group_by = c("Sex", "Pclass"),
group_start_open = c(TRUE, FALSE)
)
tabulator(titanic_df, setup)
```

***

**Note**

You can also use `set_option_group_by()` to set the group by options.

***

## Calculations

With `set_calculation()` you can add calculations to the top or bottom of columns:

```{r}
tabulator(iris[4:5]) |>
set_calculation("Petal_Width", "avg", pos = "top") |>
set_calculation("Petal_Width", "max", precision = 1, pos = "bottom")
```

## Editors

Set `editable = TRUE` to make all columns of the table editable. In this case, the editor for numeric columns is set to `number` and all others to `input`.
Expand Down

0 comments on commit 12640cb

Please sign in to comment.