Skip to content

Commit

Permalink
edit data: added add_default_values arg
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Feb 7, 2024
1 parent bf9885d commit 47199db
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions R/edit-data-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
Expand Down
7 changes: 7 additions & 0 deletions R/edit-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()].
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions examples/edit_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion inst/i18n/fr.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions man/edit-data.Rd

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

0 comments on commit 47199db

Please sign in to comment.