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 |
add |
|
update |
|
delete |
|
download_csv |
if |
download_excel |
if |
file_name_export |
|
var_edit |
vector of |
var_mandatory |
vector of |
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. |
n_column |
Number of column in the edit modal window, must be a number that divide 12 since it use Bootstrap grid system with |
return_class |
Class of returned data: |
reactable_options |
Options passed to |
modal_size |
|
modal_easy_close |
|
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
If the return value of a callback function is not truthy (see |
only_callback |
Only use callbacks, don't alter data within the module. |
use_notify |
Display information or not to user through |
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("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)