Malinda Samaratunga
Malinda Samaratunga

Reputation: 47

Change font color of a column in rhandsontable

I have a table rendered using rhandsontable in R. I want to change the font color to red of a specific column. How can I do it ? I tried the following code, but it does not work

output$hot=renderRHandsontable({
rhandontable (table)%>%
hot_col("colum1", color = "red") 

})

Upvotes: 3

Views: 3723

Answers (1)

Tobias Krabel
Tobias Krabel

Reputation: 686

If you want to change the style of elements inside of your table (in your case it is the font color of each cell of a given column), you will need to use a bit of Javascript and write a renderer function that will do the job, like so:

# Toy data frame
table <- data.frame(a = 1:10, b = letters[1:10])

# Custom renderer function
color_renderer <- "
  function(instance, td) {
    Handsontable.renderers.TextRenderer.apply(this, arguments);
    td.style.color = 'red';
  }
"

rhandsontable(table) %>%
  hot_col("b", renderer = color_renderer)

The function color_renderer() is saved as a string and will be used as the renderer argument of the hot_col()-function. Note the argument td I am using refers to the cell object of your table. td has several attributes, one is the style, which in turn has the attribute color. Also make certain that you are using the correct Handsontable renderer. In my case, it is a TextRenderer but you may use different renderers based on the data type your column has.

For more information, refer to the Handsontable documentation.

I hope this helps. Cheers

Upvotes: 7

Related Questions