带rhandontable的数字和条件格式

Numeric and Conditional Formatting with rhandsontable

本文关键字:条件 格式 数字 rhandontable      更新时间:2023-09-26

我发现我可以用数字格式格式化rhandinstantable,也可以有条件地格式化rhandontable的样式,但不能两者兼而有之。使用以下代码,似乎只使用了代码的javascript部分。如果能提供任何帮助来合并这两种格式类型,我将不胜感激。

DF = data.frame(int = 1:10, float = rnorm(10), cur = rnorm(10) * 1E5,
                lrg = rnorm(10) * 1E8, pct = rnorm(10))
rhandsontable(DF, width = 550, height = 300) %>%
  hot_cols(renderer = "
           function (instance, td, row, col, prop, value, cellProperties) {
             Handsontable.renderers.TextRenderer.apply(this, arguments);
            if (row == 2){
              td.style.background = 'lightyellow';
           }}") %>%
  hot_col("float", format = "0.0") %>%
  hot_col("cur", format = "$0,0.00") %>%
  hot_col("lrg", format = "0a") %>%
  hot_col("pct", format = "0%")

您需要引用NumericRenderer而不是TextRenderer

DF = data.frame(int = 1:10, float = rnorm(10), cur = rnorm(10) * 1E5,
            lrg = rnorm(10) * 1E8, pct = rnorm(10))
rhandsontable(DF, width = 550, height = 300) %>%
  hot_cols(renderer = "
       function (instance, td, row, col, prop, value, cellProperties) {
         Handsontable.renderers.NumericRenderer.apply(this, arguments);
        if (row == 2){
          td.style.background = 'lightyellow';
       }}") %>%
  hot_col("float", format = "0.0") %>%
  hot_col("cur", format = "$0,0.00") %>%
  hot_col("lrg", format = "0a") %>%
  hot_col("pct", format = "0%")

此处讨论问题https://github.com/jrowen/rhandsontable/issues/45