Datatables row editable with CellEdit dynamic options in R Shiny

32 Views Asked by At

This is really a general question, so I do not have a reprex. Using CellEdit, does anyone know how to replace the dropdown options of an 'list' input with an R dataframe/vector that contains a single column. This link is example of how this can be done, using sprintf() -- inserting R code in Javascript. I am unsure how to do this since my JS skills are not that good.

  "table.MakeCellsEditable({",
  "  onUpdate: onUpdate,",
  "  inputCss: 'my-input-class',",
  "  confirmationButton: {",
  "    confirmCss: 'my-confirm-class',",
  "    cancelCss: 'my-cancel-class'",
  "  },",
  "  inputTypes: [",
  "    {",
  "      column: 0,",
  "      type: 'list',",
  "      options: [",
  "        {value: 'Keep data', display: 'Keep data'},",
  "        {value: 'Pass', display: 'Pass'},",
  "        {value: 'Delete', display: 'Delete'}",
  ## PASS THE ABOVE DROPDOWN OPTIONS FROM R INSTEAD OF HARDCODED IN JS
  "      ]",
  "    }",
  "  ]",
  "});"

I am referencing the code from this question on SO.

1

There are 1 best solutions below

1
Stéphane Laurent On BEST ANSWER

Assuming your options are in the R vector opts, you can do:

opts <- c("Keep data", "Pass", "Delete")

jsOpts <- paste0(
  sprintf("{value: '%s', display: '%s'}", opts, opts), 
  collapse = ", "
)

js <- c(
  "table.MakeCellsEditable({",
  "  onUpdate: onUpdate,",
  "  inputCss: 'my-input-class',",
  "  confirmationButton: {",
  "    confirmCss: 'my-confirm-class',",
  "    cancelCss: 'my-cancel-class'",
  "  },",
  "  inputTypes: [",
  "    {",
  "      column: 0,",
  "      type: 'list',",
  "      options: [",
  jsOpts,
  "      ]",
  "    }",
  "  ]",
  "});"
)