Search code examples
rshinyreactivedt

When I try to edit a DT table, it returns an error with 'dimnames'


I'm a beginner in shiny...

I'm trying to make my shiny app to have an editable table with two calculated columns (the first and the last).

The user defines some parameters, including the number of columns of the table.

The app creates a matrix with normal samples using some parameters directly stablished by the user and others calculated in the code (calculations that I don't show here).

It renders this matrix as a table, and it should accept editions in it, but when I press ctrl+enter after editing somehting, it shows me the error:

"Warning: Error in <-: length of 'dimnames' [1] not equal to array extent".

The code:

library(shiny)
library(DT)


dt_output = function(title, id, button_id) {
  fluidRow(
    column(
      12, 
      h1(title),
      hr(), 
      DTOutput(id),
      actionButton(button_id, "Delete data")
    )
  )
}


render_dt = function(data, editable = 'cell', server = TRUE, ...) {
  renderDT(data, selection = 'none', server = server, editable = editable, ...)
}


generate_normal_data <- function(n, mi, sigma, LI, LS) {

result_matrix <- matrix(NA, nrow = 100, ncol = n + 2)
#Code that returns a matrix with normal samples in "n" columns + two calculated columns (the first and the last)

return(result_matrix)
}

ui <- fluidPage(
  
  
  theme = bs_theme(version = 4, bootswatch = "sandstone"),
  
  titlePanel("Simulador B-ATTRIVAR SS S2"),
  
  sidebarLayout(
    sidebarPanel(
      numericInput("n", "n"),
      numericInput("mi","mi"),
      numericInput("sigma","sigma"),
      numericInput("S2p","S2p"),
      numericInput("alpha","alpha"),
      numericInput("lambda","lambda"),
      numericInput("beta","beta"),
      actionButton("runButton", "Execute")
    ),
    
    mainPanel(
      tabsetPanel(
        tabPanel("Results and Chart",
                 
                 tableOutput("results"),
                 plotOutput("chart")),
        tabPanel("Insert data", 
                 dt_output('Double-click to edit table cells', 'x1', 'deleteButton'),
                 actionButton("plotDataButton", "Plot Data")),
        
      )
    )
  )
)

server <- function(input, output) {
  
  d1 <- reactiveValues(ults = NULL)
  
  observe({
  d1$ults <- generate_normal_data(input$n, input$mi, input$sigma, req(res$ults[[6]]), req(res$ults[[7]]))
  names(d1$ults[,1]) <- "Yd"
  for (i in 2:(req(input$n)+1)) {
    names(d1$ults[,i]) <- cbind("X",as.character(i))
    
  }
  names(d1$ults[,req(input$n)+2]) <- "S2"
  })
  
 #Edition
  observeEvent(input$x1_cell_edit, {
    
    d1$ults <<- editData(d1$ults, input$x1_cell_edit, 'x1')
})

  res <- reactiveValues(ults = NULL)
  res2 <- reactiveValues(ults = NULL)

  #observeEvent(input$runButton, {

  #code that calculates a vector

  #res$ults <- calculated_vector
#})

  output$x1 = render_dt(
    req(d1$ults),list(target = 'row', disable = list(columns = (req(input$n) + 2)))
    )

}

shinyApp(ui, server)


Solution

  • I solved it... just had to add "rownames = FALSE" into "editData":

    
    observeEvent(input$x1_cell_edit, {
        
        d1$ults <<- editData(d1$ults, input$x1_cell_edit, 'x1', rownames = FALSE)
    })