Plugging edited Datatable values in a shiny app

I am trying to understand how to use the values from an edited data table and do some calculation. So I have a data frame which loads by default and when you click on run it updates the tables based on the input value.
I want the user to edit the values manually in the table and then click on run. So it would take the edited values in the data table and run the calculations and update the table.

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

#### Module 1 renders the first table
tableMod <- function(input, output, session, modelRun,modelData,budget){
  
  output$x1 <- DT::renderDataTable({
    modelRun()
    isolate(
      datatable(
        modelData %>% 
          mutate(New_Membership  = as.numeric(Modified * 0.01)*(budget())),
        selection = 'none', editable = TRUE
      )
    )
  })
  observeEvent(input$x1_cell_edit, {
    df[input$x1_cell_edit$row,input$x1_cell_edit$col] <<- input$x1_cell_edit$value
  })
}
tableUI <- function(id) {
  ns <- NS(id)
  dataTableOutput(ns("x1"))
}

ui <- function(request) {
  fluidPage(
    tableUI("opfun"),
    numericInput("budget_input", "Total Forecast", value = 2),
    actionButton("opt_run", "Run")
  )
}
server <- function(input, output, session) {
  
  df <- data.frame(Channel = c("A", "B","C"),
                   Current = c(2000, 3000, 4000),
                   Modified = c(2500, 3500,3000),
                   New_Membership = c(450, 650,700),
                   stringsAsFactors = FALSE)
  
  callModule( tableMod,"opfun",
              modelRun = reactive(input$opt_run),
              modelData = df,
              budget = reactive(input$budget_input))
  
  observeEvent(input$opt_run, {
    cat('HJE')
  })
}

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

Posted in Stack Overflow

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.