删除 rhandsontable 热图中的 NA 值

问题描述 投票:0回答:2

我想让除 NA 值之外的所有值都使用 rhandsontable 显示颜色。因此,例如,如果您在安装了 rhandsontable 的 R 控制台中运行以下代码,则第一列将为灰色,因为我相信 rhandsontable 无法处理 NA 和数值。但是,如果您删除一个 NA,使单元格空白,其余单元格将恢复其颜色。

> MAT = matrix(rnorm(50), nrow = 10, dimnames = list(LETTERS[1:10],
+                                                    letters[1:5]))
> MAT[1,1] <- NA
> rhandsontable(MAT) %>% hot_heatmap()

在我的实际数据集中,我有很多 NA 值,我不希望这些列中的每一列都显示为灰色。如何告诉 rhandsontable 仅将 NA 值灰显,而不灰显列中的其余值?

r shiny heatmap rhandsontable
2个回答
0
投票

我有一个技巧...

我用非常具体的 0.001 替换了 NA 值。

然后我调整了渲染器,使其使用 0.001 浅灰色为单元格着色:

# Used by hot_heatmap
renderer_heatmap = function(color_scale) {
  renderer = gsub("\n", "", "
                  function (instance, td, row, col, prop, value, cellProperties) {
                  Handsontable.renderers.TextRenderer.apply(this, arguments);
                  heatmapScale  = chroma.scale(['%s1', '%s2']);
                  if (instance.heatmap[col]) {
                    mn = instance.heatmap[col].min;
                    mx = instance.heatmap[col].max;
                    pt = (parseInt(value, 10) - mn) / (mx - mn);
                    if (value == 0.001) {
                      td.style.backgroundColor = 'lightgrey';
                    } else {
                      td.style.backgroundColor = heatmapScale(pt).hex();
                    }
                  }
                  }
                  ")
  renderer = gsub("%s1", color_scale[1], renderer)
  renderer = gsub("%s2", color_scale[2], renderer)
  renderer
}

这不是我最好的时刻,欢迎更好的答案!


0
投票

这个问题很老了,但无论如何..现代版本的

rhandsontable
(我有0.3.7)通过将这些单元格显示为灰色而不影响其他单元格的颜色来正确处理NA。

© www.soinside.com 2019 - 2024. All rights reserved.