Skip to content

Instantly share code, notes, and snippets.

Created April 18, 2019 17:18
Show Gist options
  • Save senthilthyagarajan/a714446c9acd8127e4cbef6ede09b1fc to your computer and use it in GitHub Desktop.
Save senthilthyagarajan/a714446c9acd8127e4cbef6ede09b1fc to your computer and use it in GitHub Desktop.
Edit DT with modules
### Libraries
### Data
input_data <- data.frame(Brand = c("Brand1", "Brand2","Brand3"),
ratio = c (.5, .5, .5),
cost = c(2000, 3000, 4000),
stringsAsFactors = FALSE) %>%
mutate(updated_price = cost * ratio)
### Module
modFunction <- function(input, output, session, data,reset) {
v <- reactiveValues(data = data)
proxy = dataTableProxy("mod_table")
observeEvent(input$mod_table_cell_edit, {
info = input$mod_table_cell_edit
i = info$row
j = info$col
k = info$value
if (j %in% match(c("ratio","cost","updated_price"), names(v$data))) {
print(match(c("ratio","cost", "updated_price"), names(v$data)))
v$data[i, j] <<- DT::coerceValue(k, v$data[i, j])
if (j %in% match("cost", names(v$data))) {
v$data$updated_price <<- v$data$cost * v$data$ratio
if (j %in% match("ratio", names(v$data))) {
v$data$updated_price <<- v$data$cost * v$data$ratio
} else {
stop("You are not supposed to change this column.") # check to stop the user from editing only few columns
replaceData(proxy, v$data, resetPaging = FALSE) # replaces data displayed by the updated table
### Reset Table
observeEvent(reset(), {
v$data <- data # your default data
output$mod_table <- DT::renderDataTable({
DT::datatable(v$data, editable = TRUE)
modFunctionUI <- function(id) {
ns <- NS(id)
### Shiny App
ui = basicPage(
actionButton("reset", "Reset"),
server = function(input, output) {
callModule(modFunction,"editable", demodata,
reset = reactive(input$reset))
Copy link

I have tried
v<- reactiveValues(data = data)
however it is throwing the below error
Error in reactiveValues(data = data) :
argument "data" is missing, with no default

I am unable to understand the argument myself, could you please help me with this?

Copy link

SzarR commented Jun 12, 2020

I couldn't get this code to work for me either, same issues.

Copy link

This code has been super helpful! I need to incorporate additional input filters, but when I try to filter the initial data frame to allow for this and run the code I get:
"Error in dplyr::filter(): caused by error in mask$eval_all_filter() object 'input' not found"

Do you know how I could incorporate other drop down filters into this app?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment