Edit DT in R shiny app to update second DT table

I am trying to work on a shiny app where if the user edit the first table it modifies the value on the second table. If you see the code I have two tables x1 and x2 in the tableMod function.The values in table x1 are based off values from table x2 and some calculations. So what I want to do is if the user modifes the values using edit in table x1 it should update the values by reverse calculation in table x2. Below is the code except for how to do the edit and reverse calculations :

I am trying to work on a shiny app where if the user edit the first table it modifies the value on the second table. If you see the code I have two tables x1 and x2 in the tableMod function.The values in table x1 are based off values from table x2 and some calculations. So what I want to do is if the user modifes the values using edit in table x1 it should update the values by reverse calculation in table x2. Below is the code except for how to do the edit and reverse calculations :

library(shiny)
library(DT)
library(dplyr)

df <- data.frame(Channel = c("A", "B","C"),
                 Current = c(2000, 3000, 4000),
                 Modified = c(2500, 3500,3000),
                 New_Membership = c(362, 577,575),
                 stringsAsFactors = FALSE)
rates <- data.frame(
                 low = c(1000, 2000, 3000),
                 high = c(4000, 6000,8000),
                 stringsAsFactors = FALSE)

#### Module 1 renders the first table
tableMod <- function(input, output, session, modelRun,modelData,ratesData,budget){
  
  
  observeEvent( input$x1_cell_edit, {
    cat ('input$x1_cell_edit \n')
    info = input$x1_cell_edit
    str(info)
    i = info$row
    j = info$col
    v = info$value
    df[i, j] <- DT:::coerceValue(v, df[i, j])
    assign("df", df, envir = .GlobalEnv)
    
  })

  
  output$x1 <- DT::renderDataTable({
    modelRun()
   modelData <- df
   ratesData <- rates
    isolate(
      datatable(
        modelData %>% 
          mutate(Current = (ratesData$low * budget())) %>%
          mutate(Modified = (ratesData$high * budget())),
         #mutate(New_Membership  = round((Current*ratesData$low + Modified*ratesData$high)*budget(),0) ),
        selection = 'none', editable = TRUE
      )
    )
  })
  output$x2<- DT::renderDataTable({
    ratesData
  })
  
  
}
firstTableUI <- function(id) {
  ns <- NS(id)
  dataTableOutput(ns("x1"))
}
secondTableUI <- function(id) {
  ns <- NS(id)
  dataTableOutput(ns("x2"))
}

ui <- function(request) {
  fluidPage(
    firstTableUI("opfun"),
    numericInput("budget_input", "Total Forecast", value = 2),
    actionButton("opt_run", "Run"),
    secondTableUI("opfun")
  )
}
server <- function(input, output, session) {
  
  callModule( tableMod,"opfun",
              modelRun = reactive(input$opt_run),
              modelData = df,
              ratesData = rates,
              budget = reactive(input$budget_input))
  
  observeEvent(input$opt_run, {
    cat('HJE')
  })
}

shinyApp(ui, server, enableBookmarking = "url")

Also posted this on stackoverflow

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.