【问题标题】:rhandsontable change background of specific rowrhandsontable 更改特定行的背景
【发布时间】:2016-06-10 07:44:02
【问题描述】:

我在 R shiny 中使用 rhandsontable,我想在第一列中使用“Sum”更改行的颜色。由于行数不固定,因此根据行数选择“Sum”行不起作用。我已经尝试了附加的代码,不幸的是它不起作用。非常感谢您的帮助!

library(shiny)
library(rhandsontable)
runApp(list(server=
  shinyServer(function(input, output, session) {

     CF_amt <- as.data.frame(matrix(0.0, nrow=5, ncol=10));
     CF_type <- data.frame(source = c("Row1","Row2","Row3","Row4","Sum"),
                            stringsAsFactors = FALSE);
     CF_names <- c("source","C1","C2","C3","C4","C5","C6","C7","C8","C9","C10");
     CF_tbl <- cbind(CF_type,CF_amt);

     values <- reactiveValues(data = CF_tbl)

     output$table <- renderRHandsontable({ 
       rhandsontable(values$data,rowHeaders = NULL,colHeaders=CF_names) %>% 
         hot_cols(fixedColumnsLeft = 1, renderer = "
             function (instance, td, row, col, prop, value, cellProperties) {
               Handsontable.renderers.TextRenderer.apply(this, arguments);
               if (col == 0) {
                td.style.background = '#F0F0F0';
               }
               else if(this.instance.getData()[row][0] == 'Sum'){
                td.style.background = '#F00000';
               }
            }"
       ) 
    })
  })
,ui=
  shinyUI(navbarPage("Test",
    tabPanel("HOT",
       fluidPage(fluidRow(
         column(12,rHandsontableOutput("table"))
      ))
   )
  ))
))

【问题讨论】:

    标签: r shiny handsontable


    【解决方案1】:

    看起来您遇到了 javascript 错误。尝试改变

    this.instance.getData()[row][0]
    

    instance.getData()[row][0]
    

    因为instance 作为参数传递给您的渲染函数。

    【讨论】:

    • 非常感谢!这样就解决了问题。实际上,我认为我也尝试过。但是,现在它可以工作了。非常感谢。
    • @ChrisW 您应该将此答案标记为您问题的答案。
    猜你喜欢
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 2017-04-24
    • 2017-11-21
    • 2013-11-22
    • 1970-01-01
    相关资源
    最近更新 更多