Skip to content

Problem with editable DT  #813

@Andryas

Description

@Andryas

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")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions