Home > OS >  What would be the best practice to use JavaScript function in reactable?
What would be the best practice to use JavaScript function in reactable?

Time:05-26

In reactable it is possible to use JavaScript functions, like in this example:

reactable(iris, columns = list(
  Species = colDef(
    cell = JS("function(cellInfo) {
      return '<b>'   cellInfo.value   '</b>'
    }")
  )
))

However, all examples I saw in the documentation use string passed to the JS() function. I think that taking into account the readability and conveniences, it would be better to store the JS function in separate .js file. Is it true? And if yes, how to achieve this? I mean - how to call then this function? Based on the example above.

CodePudding user response:

You can create a separate file (script.js), and then read in your JavaScript using readLines():

script.js

function(cellInfo) {
      return '<b>'   cellInfo.value   '</b>'
}

reactable.r

library(reactable)

js <- readLines('script.js')

reactable(iris, columns = list(
  Species = colDef(
    cell = JS(js)
  )
))
  • Related