From 47199dba250ce370d785e75833c18de75ae80e28 Mon Sep 17 00:00:00 2001 From: pvictor Date: Wed, 7 Feb 2024 16:06:31 +0100 Subject: [PATCH] edit data: added add_default_values arg --- R/edit-data-utils.R | 7 +++++++ R/edit-data.R | 7 +++++++ examples/edit_data.R | 4 ++++ inst/i18n/fr.csv | 2 +- man/edit-data.Rd | 7 +++++++ 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/R/edit-data-utils.R b/R/edit-data-utils.R index 6a6b55f..d797c00 100644 --- a/R/edit-data-utils.R +++ b/R/edit-data-utils.R @@ -322,6 +322,13 @@ get_variables_labels <- function(labels, column_names, internal_names) { return(labels) } +get_variables_default <- function(default, column_names, internal_names) { + default <- default[column_names] + idx <- match(names(default), column_names, nomatch = 0L) + names(default)[idx > 0] <- internal_names[idx] + default +} + #' @title The update column definition #' diff --git a/R/edit-data.R b/R/edit-data.R index 9844ee0..e99cd5d 100644 --- a/R/edit-data.R +++ b/R/edit-data.R @@ -55,6 +55,7 @@ edit_data_ui <- function(id) { #' @param var_edit vector of `character` which allows to choose the names of the editable columns. #' @param var_mandatory vector of `character` which allows to choose obligatory fields to fill. #' @param var_labels named list, where names are colnames and values are labels to be used in edit modal. +#' @param add_default_values Default values to use for input control when adding new data, e.g. `list(my_var_text = "Default text to display")`. #' @param n_column Number of column in the edit modal window, must be a number that divide 12 since it use Bootstrap grid system with [shiny::column()]. #' @param return_class Class of returned data: `data.frame`, `data.table`, `tbl_df` (tibble) or `raw`. #' @param reactable_options Options passed to [reactable::reactable()]. @@ -97,6 +98,7 @@ edit_data_server <- function(id, var_edit = NULL, var_mandatory = NULL, var_labels = NULL, + add_default_values = list(), n_column = 1, return_class = c("data.frame", "data.table", "tbl_df", "raw"), reactable_options = NULL, @@ -212,6 +214,11 @@ edit_data_server <- function(id, observeEvent(input$add, { req(data_r()) edit_modal( + default = get_variables_default( + add_default_values, + data_rv$colnames, + data_rv$internal_colnames + ), id_validate = "add_row", data = data_rv$data, var_edit = data_rv$edit, diff --git a/examples/edit_data.R b/examples/edit_data.R index 6c97c3c..2a9073e 100644 --- a/examples/edit_data.R +++ b/examples/edit_data.R @@ -33,6 +33,10 @@ server <- function(input, output, session) { contactless_card = "Contactless Card", credit_card_provider = "Credit card provider" ), + add_default_values = list( + name = "Please enter your name here", + date_obtained = Sys.Date() + ), n_column = 2, modal_size = "l", modal_easy_close = TRUE, diff --git a/inst/i18n/fr.csv b/inst/i18n/fr.csv index e9cfa00..7f42227 100644 --- a/inst/i18n/fr.csv +++ b/inst/i18n/fr.csv @@ -61,7 +61,7 @@ label,translation,comment "Close","Fermer" "Add a label to data","Ajouter un libellé aux données" "URL","URL" -"Add a row","Ajouter une ligne" +"Add a row","Ajouter" "Required field","Champs requis" "Please fill in the required fields","Merci de remplir les champs obligatoires" "Unable to add the row, contact the platform administrator","Impossible d'ajouter la ligne, contactez l'administrateur de la plateforme" diff --git a/man/edit-data.Rd b/man/edit-data.Rd index 4dcd656..000387b 100644 --- a/man/edit-data.Rd +++ b/man/edit-data.Rd @@ -20,6 +20,7 @@ edit_data_server( var_edit = NULL, var_mandatory = NULL, var_labels = NULL, + add_default_values = list(), n_column = 1, return_class = c("data.frame", "data.table", "tbl_df", "raw"), reactable_options = NULL, @@ -55,6 +56,8 @@ edit_data_server( \item{var_labels}{named list, where names are colnames and values are labels to be used in edit modal.} +\item{add_default_values}{Default values to use for input control when adding new data, e.g. \code{list(my_var_text = "Default text to display")}.} + \item{n_column}{Number of column in the edit modal window, must be a number that divide 12 since it use Bootstrap grid system with \code{\link[shiny:column]{shiny::column()}}.} \item{return_class}{Class of returned data: \code{data.frame}, \code{data.table}, \code{tbl_df} (tibble) or \code{raw}.} @@ -121,6 +124,10 @@ server <- function(input, output, session) { contactless_card = "Contactless Card", credit_card_provider = "Credit card provider" ), + add_default_values = list( + name = "Please enter your name here", + date_obtained = Sys.Date() + ), n_column = 2, modal_size = "l", modal_easy_close = TRUE,