Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kuethe committed Sep 17, 2024
1 parent 0d30f09 commit 0fa61e7
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 22 deletions.
4 changes: 3 additions & 1 deletion 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_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)
Expand Down
4 changes: 4 additions & 0 deletions R/input_handlers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# https://book.javascript-for-r.com/shiny-complete.html
input_handler_data <- function(data, ...) {
return(tabulator_data_as_df(data))
}
26 changes: 19 additions & 7 deletions R/tabulator_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ 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) {
# jsonlite::toJSON(x, auto_unbox = TRUE) |> jsonlite::fromJSON()
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)))
3 changes: 2 additions & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ reference:
contents:
- tabulator
- tabulator_options
- set_option_group_by
- set_options_group_by
- set_options_pagination

- title: Column Settings
desc: >
Expand Down
2 changes: 2 additions & 0 deletions examples/options/group_by_options.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tabulator(iris) |>
set_options_group_by("Species", group_start_open = FALSE)
2 changes: 2 additions & 0 deletions examples/options/pagination_options.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tabulator(iris) |>
set_options_pagination(pagination_size_selector = c(10, 20, 50))
4 changes: 2 additions & 2 deletions man/list_to_data_frame.Rd

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

10 changes: 5 additions & 5 deletions man/set_option_group_by.Rd → man/set_options_group_by.Rd

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

35 changes: 35 additions & 0 deletions man/set_options_pagination.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/tabulator_data_as_df.Rd

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

14 changes: 11 additions & 3 deletions vignettes/articles/rtabulator.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand All @@ -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")
Expand Down Expand Up @@ -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.

***

Expand Down

0 comments on commit 0fa61e7

Please sign in to comment.