首页 > 解决方案 > 如何使用 rhandsontable 根据另一个单元格值格式化单元格

问题描述

我是 rhandsontable 包的新手,我正在尝试根据另一个单元格中的值突出显示一个单元格。当 mpg==21 时,我想突出显示 mtcars 数据集中“齿轮”列中的值。我不知道如何do.Apreciate some help in rhandsontable

rhandsontable(mtcars, readOnly = TRUE, width = 750, height = 
300)%>%hot_cols(renderer = "
             function (instance, td, row, col       , prop, value, 
cellProperties) {
             Handsontable.renderers.NumericRenderer.apply(this, arguments);
var col_value = instance.getData()[XXXXXXXXXXX][XXXXXXXXX]
             if (col_value ==21) {
             td.style.background = 'pink';
             }else if (col_value !=21) {
  td.style.background = 'green';
  }
             }")

我不确定如何编写上面的代码来实现我的结果

标签: rhandsontable

解决方案


以下代码完成了工作:

library(rhandsontable)

ui <- fluidPage(
  rHandsontableOutput('hot')
)

server <- function(input, output, session) {
  output$hot <- renderRHandsontable({
    rhandsontable(mtcars, readOnly = TRUE, width = 750, height = 300) %>%
    hot_cols(renderer = myrenderer)
  })

  myrenderer <- "function (instance, td, row, col, prop, value, cellProperties) {

  Handsontable.renderers.TextRenderer.apply(this, arguments);

  if (col == 9 && instance.getData()[row][0] == 21 ) {  
    td.style.background = 'pink';
  }
  }"
}

shinyApp(ui = ui, server = server)

推荐阅读