2016-05-23 71 views

回答

0

下面是一個例子,根據特定列中給定的值對整行進行着色。用instance.getData()您可以檢索rhandsontable對象中的完整數據。您可以使用索引訪問特定的單元格。

library(rhandsontable) 

DF = data.frame(bool = TRUE,val = 1:10, big = LETTERS[1:10], 
       small = letters[1:10], 
       stringsAsFactors = FALSE) 

text_renderer <- " 
    function (instance, td, row, col, prop, value, cellProperties) { 
    Handsontable.renderers.TextRenderer.apply(this, arguments); 
    var col_value = instance.getData()[row][2] 
    if (col_value == 'C') { 
     td.style.background = 'pink'; 
    } else if (col_value == 'D') { 
     td.style.background = 'green'; 
    } 
    }" 

bool_renderer <- " 
    function (instance, td, row, col, prop, value, cellProperties) { 
    Handsontable.renderers.CheckboxRenderer.apply(this, arguments); 
    var col_value = instance.getData()[row][2] 
    if (col_value == 'C') { 
     td.style.background = 'pink'; 
    } else if (col_value == 'D') { 
     td.style.background = 'green'; 
    } 
    } 
" 

rhandsontable(DF, readOnly = FALSE, width = 750, height = 300) %>% 
    hot_col(col = c(2, 3, 4), renderer = text_renderer) %>% 
    hot_col("bool", renderer = bool_renderer) 
相關問題