How to save output from module that is within observeEvent?

Hi there,

I wonder if there is a way to save the edited cell from module that is within observeEvent. The purpose of this edit is to pass the edited cell to further calculation.

The current issue is that the table can be edited but once I change the filter, the edited cell can't be stored and it will go back to the original number. For example, I make the rate of 30K B R 2021 from 0.00% to 1.00%, once I filter it to 30K, B, D, the edited 1% will be lost. I wonder how to store the edited value within the observeEevent.

Any help is greatly appreciated!

library(shiny)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
library(tidyr)
library(DT)
#> 
#> Attaching package: 'DT'
#> The following objects are masked from 'package:shiny':
#> 
#>     dataTableOutput, renderDataTable

df1<-data.frame("Network"=c("50K", "45K", "40K","30K"),"Year"=c(2021,2022,2023,2024),"Rate"=c(0.1,0.2,0.3,0.4))
df2<-data.frame("BG"=c("B","G"))
df3<-data.frame("RD"=c("R","D"))


df<-df1 %>% merge(df2) %>% merge(df3) 



raw<-function(data,input){

  data<-data %>% filter(BG %in% input$BG, RD %in% input$RD)

  if ('Select All' %in% input$Network | is.null(input$Network)) {

  } else {
    data <- data %>% filter(Network %in% input$Network)
  }
  
  data<-data %>% spread(key=Year, value=Rate, fill = FALSE)
  return(data)
}



### Module
modFunction <- function(input, output, session, data,reset) {

  v <- reactiveValues(data = NULL)
  
  observeEvent(data(), {
    v$data <- data()
  })

  proxy =  DT::dataTableProxy("mod_table")

  observeEvent(input$mod_table_cell_edit, {
    info = input$mod_table_cell_edit
    str(info)
    i = info$row
    j = info$col
    k = info$value
    str(info)
    v$data[i, j] <<- DT::coerceValue(k, v$data[i, j])
    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) %>% 
      formatPercentage(c(4:ncol(v$data)),2)

  })
}

modFunctionUI <- function(id) {
  ns <- NS(id)
  DT::dataTableOutput(ns("mod_table"))

}



ui <- fluidPage(
  titlePanel("Input"),
  sidebarLayout(
    sidebarPanel(
      selectInput("Network","Choose a network:",
                  choices = c("Select All",unique(toupper(df$Network))),
                  selected = 'Select All'),
      selectInput("BG","Choose B or G:",
                  choices = c(unique(df$BG)),
                  selected = "B"),
      selectInput("RD","Choose R or D:",
                  choices = c(unique(df$RD)),
                  selected = "R")),

    mainPanel(
      actionButton("reset", "Reset"),
      tags$hr(),
      modFunctionUI("editable")
    )
  )
)



# Define server logic for random distribution app ----
server <- function(input, output) {
  
  data1<-reactive({
    raw(df,input)
  })
  
  
  demodata<-data1
  
  callModule(modFunction,"editable", demodata,
             reset = reactive(input$reset))

}

shinyApp(ui=ui, server = server)

Shiny applications not supported in static R Markdown documents

Created on 2020-11-03 by the reprex package (v0.3.0)

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.