edit-data {datamods}R Documentation

Shiny module to interactively edit a data.frame

Description

The module generates different options to edit a data.frame: adding, deleting and modifying rows, exporting data (csv and excel), choosing editable columns, choosing mandatory columns. This module returns the edited table with the user modifications.

Usage

edit_data_ui(id)

edit_data_server(
  id,
  data_r = reactive(NULL),
  add = TRUE,
  update = TRUE,
  delete = TRUE,
  download_csv = TRUE,
  download_excel = TRUE,
  file_name_export = "data",
  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,
  modal_size = c("m", "s", "l", "xl"),
  modal_easy_close = TRUE,
  callback_add = NULL,
  callback_update = NULL,
  callback_delete = NULL,
  only_callback = FALSE,
  use_notify = TRUE
)

Arguments

id

Module ID

data_r

data_r reactive function containing a data.frame to use in the module.

add

boolean, if TRUE, allows you to add a row in the table via a button at the top right.

update

boolean, if TRUE, allows you to modify a row of the table via a button located in the table on the row you want to edit.

delete

boolean, if TRUE, allows a row to be deleted from the table via a button in the table.

download_csv

if TRUE, allows to export the table in csv format via a download button.

download_excel

if TRUE, allows to export the table in excel format via a download button.

file_name_export

character that allows you to choose the export name of the downloaded file.

var_edit

vector of character which allows to choose the names of the editable columns.

var_mandatory

vector of character which allows to choose obligatory fields to fill.

var_labels

named list, where names are colnames and values are labels to be used in edit modal.

add_default_values

Default values to use for input control when adding new data, e.g. list(my_var_text = "Default text to display").

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().

return_class

Class of returned data: data.frame, data.table, tbl_df (tibble) or raw.

reactable_options

Options passed to reactable::reactable().

modal_size

character which allows to choose the size of the modalDialog. One of "s" for small, "m" (the default) for medium, "l" for large, or "xl" for extra large.

modal_easy_close

boolean If TRUE, modalDialog can be dismissed by clicking outside the dialog box, or be pressing the Escape key. If FALSE (the default), modalDialog can't be dismissed in those ways; instead it must be dismissed by clicking on a modalButton(), or from a call to removeModal() on the server.

callback_add, callback_update, callback_delete

Functions to be executed just before an action (add, update or delete) is performed on the data. Functions used must be like function(data, row) {...} where :

  • data will be the data in the table at the moment the function is called

  • row will contain either a new row of data (add), an updated row (update) or the row that will be deleted (delete).

If the return value of a callback function is not truthy (see shiny::isTruthy()) then the action is cancelled.

only_callback

Only use callbacks, don't alter data within the module.

use_notify

Display information or not to user through shinybusy::notify().

Value

the edited data.frame in reactable format with the user modifications

Examples

library(shiny)
library(datamods)
library(bslib)
library(reactable)

ui <- fluidPage(
  theme = bs_theme(
    version = 5
  ),
  tags$h2(i18n("Edit data"), align = "center"),
  edit_data_ui(id = "id"),
  verbatimTextOutput("result")
)


server <- function(input, output, session) {

  edited_r <- edit_data_server(
    id = "id",
    data_r = reactive(demo_edit),
    add = TRUE,
    update = TRUE,
    delete = TRUE,
    download_csv = TRUE,
    download_excel = TRUE,
    file_name_export = "datas",
    # var_edit = c("name", "job", "credit_card_provider", "credit_card_security_code"),
    var_mandatory = c("name", "job"),
    var_labels = list(
      name = "Name",
      credit_card_security_code = "Credit card security code",
      date_obtained = "Date obtained",
      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,
    reactable_options = list(
      defaultColDef = colDef(filterable = TRUE),
      selection = "single",
      columns = list(
        name = colDef(name = "Name", style = list(fontWeight = "bold")),
        credit_card_security_code = colDef(name = "Credit card security code"),
        date_obtained = colDef(name = "Date obtained", format = colFormat(date = TRUE)),
        contactless_card = colDef(
          name = "Contactless Card",
          cell = function(value) {
            # Render as an X mark or check mark
            if (value == FALSE) "\u274c No" else "\u2714\ufe0f Yes"
          }),
        credit_card_provider = colDef(
          name = "Credit card provider",
          style = function(value) {
            if (value == "Mastercard") {
              color <- "#e06631"
            } else if (value == "VISA 16 digit") {
              color <- "#0c13cf"
            } else if (value == "American Express") {
              color <- "#4d8be8"
            } else if (value == "JCB 16 digit") {
              color <- "#23c45e"
            } else {
              color <- "#777"
            }
            list(color = color, fontWeight = "bold")
          }
        )
      ),
      bordered = TRUE,
      compact = TRUE,
      searchable = TRUE,
      highlight = TRUE
    )
  )

  output$result <- renderPrint({
    str(edited_r())
  })

}

if (interactive())
  shinyApp(ui, server)

[Package datamods version 1.5.0 Index]