如何使用Rhsonsontable根据另一个像元值​​格式化像元

时间:2019-01-22 18:03:30

标签: rhandsontable

我是rhansontable包的新手,我试图根据另一个单元格中的值突出显示一个单元格。我想在mpg == 21时突出显示mtcars数据集中的“齿轮”列中的值。提示该怎么做。感谢一些在rhansontable方面的帮助

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';
  }
             }")

我不确定如何编写以上代码来达到我的结果

1 个答案:

答案 0 :(得分:0)

以下代码可以完成工作:

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)
相关问题