golem icon indicating copy to clipboard operation
golem copied to clipboard

Problem with editable DT

Open Andryas opened this issue 3 years ago • 2 comments

Hi, I am trying to create a editable DT in R using the golem framework with no success. I think that the problem is relate to ns() function when DT create the new input with _cell_edit.

Here a producible example.

#' dt UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd 
#'
#' @importFrom shiny NS tagList 
mod_dt_ui <- function(id){
  ns <- NS(id)
  tagList(
    DT::DTOutput(ns("example"))
  )
}
    
#' dt Server Functions
#'
#' @noRd 
mod_dt_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    aux <- shiny::reactiveValues(dt = iris)

    output$example <- DT::renderDT({
      aux$dt |> 
        DT::datatable(
          rownames = FALSE,
          options = list(),
          editable = list(
            target = "row",
            disable = list(columns = c(0,1,2))
          )
        )
    })

    proxy <- DT::dataTableProxy(ns("example"))
    shiny::observeEvent(input$example_cell_edit, {
      info <- input$example_cell_edit
      str(info) # check what info looks like (a data frame of 3 columns)
      aux$dt <- DT::editData(aux$dt, info)
      print(aux$dt)
      DT::replaceData(proxy, aux$dt, resetPaging = FALSE) # important
      # the above steps can be merged into a single editData() call; see examples below
    })
 
  })
}
    
## To be copied in the UI
# mod_dt_ui("dt_ui_1")
    
## To be copied in the server
# mod_dt_server("dt_ui_1")

Andryas avatar Dec 20 '21 18:12 Andryas

Hi Andryas,

You have a typo when you collect the DT cell edit into info. You have input$ twice, which causes shiny to look for a slot called input in the input reactive:

info <- input$input$example_cell_edit

removing this solves the immediate problem in that you can edit the DT.

teofiln avatar Dec 20 '21 19:12 teofiln

Sorry, I type wrong. I fix now and the problem persist.

Andryas avatar Dec 20 '21 20:12 Andryas