'R shiny, Editable DT table Module with reactive data as input

I am trying to create a editable shiny DT table module. It works well when I pass in iris data. However, when I tried to pass a reactive value, the module does not work. Does anyone had similar experience before ? Could you share your thought?

library(shiny)
library(DT)

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

editableUIServer<-function(id,data,disable_col,change){
  moduleServer(id,
               function(input,output,session){
                 print('hi')
                 v<-reactiveValues(data = data)
                 print('here we got v')
                 print(v$data)

                 output$mod_table <- renderDT(v$data,
                                             editable = list(target = 'cell',
                                                             disable = list(columns=c(disable_col))))
                 proxy = dataTableProxy('mod_table')
                 observeEvent(input$mod_table_cell_edit, {
                  info = input$mod_table_cell_edit
                  v$data <<- editData(v$data, info)
                  replaceData(proxy, v$data, resetPaging = FALSE)
                })
                 observeEvent(change(),{
                   v$data<<-data
                 })
                 
    return(v)
}
)}

# Define UI for application that draws a histogram
ui <- fluidPage(
      fluidPage(
        fluidRow(pickerInput('spec',label = 'Species',choices = unique(as.character(iris$Species)),selected = "versicolor")
                 ),
        fluidRow(editableUI("test")))

)

# Define server logic required to draw a histogram
server <- function(input, output) {
  data<-reactive({
    iris %>% filter(Species %in% c(input$spec))
    })
  observe({print(data())})
  
  v<-reactive({editableUIServer("test",data(),c(1), change_ppg = reactive(input$spec))})
}

# Run the application 
shinyApp(ui = ui, server = server)



Solution 1:[1]

I changed a couple of lines and now it works. Not sure why it was wrong in the first place, though


library(shiny)
library(DT)



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

editableUIServer<-function(id,data,disable_col){
  moduleServer(id,
               function(input,output,session){
                 print('hi')
                 v<-reactiveValues(data = data)
                 print('here we got v')
                

                 output$mod_table <- renderDT(v$data,
                                             editable = list(target = 'cell',
                                                             disable = list(columns=c(disable_col))))
                 proxy = dataTableProxy('mod_table')
                 observeEvent(input$mod_table_cell_edit, {
                  info = input$mod_table_cell_edit
                  v$data <<- editData(v$data, info)
                  replaceData(proxy, v$data, resetPaging = FALSE)
                })
             
                 
    return(v)
}
)}

# Define UI for application that draws a histogram
ui <- fluidPage(
      fluidPage(
        fluidRow(pickerInput('spec',label = 'Species',choices = unique(as.character(iris$Species)),selected = "versicolor")
                 ),
        fluidRow(editableUI("test")))

)

# Define server logic required to draw a histogram
server <- function(input, output) {
  data<-reactive({
    iris %>% filter(Species %in% c(input$spec))
    })

 # v<-reactive({editableUIServer("test",data =iris,c(1), change_ppg = reactive(input$spec))})
  v<- reactive(editableUIServer("test",data(),c(1)))
  observe(print(v()$data))
}

# Run the application 
shinyApp(ui = ui, server = server)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wenxuan Zhang