2

I have a dataframe with two columns: Condition & Value. Based on this dataframe I would like to create a reactable object. The Value column should have a prefix. It should be preceded by “Check” if the Condition column is TRUE. Unfortunately, I could not think of a solution to include a condition in the following code.

library(reactable)

df <- data.frame(
  Condition = c(TRUE, FALSE, TRUE, FALSE),
  Value = c(100, 200, 300, 400)
)

reactable(
  df,
  columns = list(
    Value = colDef(format = colFormat(prefix = "Check"))
  )
)

Here is the desired output:
enter image description here

2
  • Why don't you first format the dataframe before making it a reactable object? Commented Dec 18, 2024 at 13:59
  • @IfeanyiIdiaye Unfortunately, this is not possible in my use case. That's why I prefer a solution within colDef(). Commented Dec 18, 2024 at 14:01

1 Answer 1

1

You could use js for custom cell render:

reactable::reactable(
  df,
  columns = list(
    Value = colDef(
      cell = reactable::JS("function(data) {
        if (data.row.Condition) {
          return 'Check ' + data.value;
        }
        return data.value;
      }")
    )
  )
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.