2016-02-26 69 views
2

我正在R RASH中使用rhandsontable,我想在第一列中使用「Sum」更改行的顏色。由於行數不固定,因此根據行號選擇「總和」行不起作用。我試過附加的代碼,但不幸的是它不工作。很多感謝您的幫助!rhandsontable更改特定行的背景

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")) 
    )) 
    ) 
)) 
)) 

回答

1

看起來像你有一個JavaScript錯誤。嘗試,因爲instance作爲參數傳遞給你的渲染函數變化

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

instance.getData()[row][0] 

enter image description here

+0

非常感謝!解決了這個問題。其實,我認爲我也嘗試過。但是,現在它起作用了。非常感謝你。 – ChrisW