From 5be88c24c21029ebae3bdb6783221bfbeba35d8c Mon Sep 17 00:00:00 2001 From: Stefan Kuethe Date: Thu, 12 Sep 2024 08:17:01 +0200 Subject: [PATCH] Add default spreadsheet options --- R/js_dependencies.R | 2 +- R/tabulator.R | 5 ++++- R/tabulator_options.R | 14 ++++++++++++-- examples/shiny/titanic/app.R | 3 +-- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/R/js_dependencies.R b/R/js_dependencies.R index 224e0f8..cabc9b8 100644 --- a/R/js_dependencies.R +++ b/R/js_dependencies.R @@ -3,7 +3,7 @@ SHEETJS_VERSION <- "0.20.1" # Needed to support xlsx downloads sheetjs_dependency <- htmltools::htmlDependency( name = "sheetjs", - version =SHEETJS_VERSION, + version = SHEETJS_VERSION, src = list( href = glue::glue("https://cdn.sheetjs.com/xlsx-{SHEETJS_VERSION}/package/dist/") ), diff --git a/R/tabulator.R b/R/tabulator.R index 268cc67..15286f8 100644 --- a/R/tabulator.R +++ b/R/tabulator.R @@ -25,13 +25,16 @@ tabulator <- function( ...) { if (is.null(options)) options <- list() - if (class(data) == "character") { + if (is.character(data)) { data <- readr::read_csv(data, show_col_types = FALSE) } + # TODO: Use Pipe, but then we need to set required R Version to > 4.1 options <- utils::modifyList(options, list(...)) + options <- keys_to_camel_case(compact(options)) if (isTRUE(options$spreadsheet)) { # ... + options <- utils::modifyList(default_spreadsheet_options, options) } else { data <- fix_colnames(data) if (getOption("rtabulator.auto_columns", TRUE) && is.null(options$columns)) { diff --git a/R/tabulator_options.R b/R/tabulator_options.R index eeaa39a..d1cf9e3 100644 --- a/R/tabulator_options.R +++ b/R/tabulator_options.R @@ -1,4 +1,4 @@ -#' Table Options +#' Tabulator Setup Options #' @param height (character) The height of the table in px. #' @param history (bool): description #' @param colummns (list): description @@ -45,7 +45,7 @@ tabulator_options <- function( spreadsheet = FALSE, spreadsheet_rows = NULL, spreadsheet_columns = NULL, - spreadsheet_column_definition = list(editor = "input"), + spreadsheet_column_definition = NULL, spreadsheet_sheets = NULL, spreadsheet_sheet_tabs = NULL, ...) { @@ -54,3 +54,13 @@ tabulator_options <- function( params$add_row_pos <- match.arg(add_row_pos) return(c(params, list(...))) } + +default_spreadsheet_options <- list( + row_header = list( + field = "_id", + hozAlign = "center", + headerSort = FALSE, + frozen = TRUE + ), + spreadsheet_column_definition = list(editor = "input") +) diff --git a/examples/shiny/titanic/app.R b/examples/shiny/titanic/app.R index 789ae7c..d5d1f83 100644 --- a/examples/shiny/titanic/app.R +++ b/examples/shiny/titanic/app.R @@ -1,7 +1,6 @@ library(shiny) data_url <- "https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv" -titanic_tibble <- readr::read_csv(data_url) ui <- fluidPage( titlePanel("Titanic Data"), @@ -10,7 +9,7 @@ ui <- fluidPage( server <- function(input, output) { output$titanic <- renderTabulator({ - tabulator(titanic_tibble, editable = TRUE) |> + tabulator(data_url, editable = TRUE) |> set_progress_formatter("Fare") |> set_tick_cross_formatter("Survived") |> set_star_formatter("Pclass", number_of_stars = 3)