Row Numeric (Percent) Formatting in RHandsontable

I need to format rows of a pivoted table using RHandsontable. Column formatting is super straightforward, but hot_row() does not provide the same flexibility as hot_col().

Has anyone figured out a way to format this...

library(rhandsontable)
df <- data.frame(q1=c(1,0.2),q2=c(2,0.3),q3=c(3,0.4),q4=c(4,0.5))
rownames(df) <- c('Amt','Pct')
rhandsontable(df)

image

...to display ints (1,2,3,4) for the first row (Amt) and percentages (20%,30%,40%,50%) for the second row (Pct)?
image

SOLVED

For anyone interested - pct_row and int_row are arbitrarily-named arguments. Those args are exposed in instance.params. I can then check if the row is a part of that argument and treat it accordingly in td.innerHTML.

library(rhandsontable)
df <- data.frame(q1=c(1,0.2),q2=c(2,0.3),q3=c(3,0.4),q4=c(4,0.5))
rownames(df) <- c('Amt','Pct')  
rhandsontable(df,pct_row=1,int_row=0) %>%
       hot_cols(renderer = "function(instance, td, row, col, prop, value, cellProperties) {
           Handsontable.renderers.NumericRenderer.apply(this, arguments);
           if (instance.params && instance.params.pct_row === row) {
               td.innerHTML = `${Number.parseFloat(value*100)}%`
           } else if (instance.params && instance.params.int_row === row) {
               td.innerHTML = `${value}`
           }
         }")

Many thanks to this post for connecting some dots for me...

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.