mazu
mazu

Reputation: 327

R rhandsontable pass column index as variable to renderer

For conditional formatting purposes, I am trying to pass column index to javascript renderer in R shiny app that uses rhandsontable. But I am not able to achieve it with instance.params... Could anyone help ? I have tried the following:

library(shiny)
library(rhandsontable)

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

server <- function(input, output) {
  
  df <- data.frame(alphabet = letters[1:10],
                   include = TRUE)
  
  output$table <- rhandsontable::renderRHandsontable({
    
    rhandsontable(df, height = 500, colidx=1) %>%
      hot_col(col = "include",
              renderer = "
                function (instance, td, row, col, prop, value, cellProperties) {
                    Handsontable.renderers.CheckboxRenderer.apply(this, arguments);
                    var col_value = instance.getData()[row][instance.params.colidx]
                    if (col_value === false) {
                        td.style.background = 'pink';
                    }
                }
            ") %>%
      hot_col(col = "alphabet",
              renderer = "
                function (instance, td, row, col, prop, value, cellProperties) {
                    Handsontable.renderers.TextRenderer.apply(this, arguments);
                    var col_value = instance.getData()[row][instance.params.colidx]
                    if (col_value == false) {                    
                        td.style.background = 'pink';
                    }
                }
            ")
  })
}

shinyApp(ui, server)

Upvotes: 0

Views: 174

Answers (1)

Saurabh Rd
Saurabh Rd

Reputation: 36

I have modified your code to get what you were trying to achieve. Hope this is what you want.

    library(shiny)
    library(rhandsontable)
    
    ui <- fluidPage(
      rHandsontableOutput('table')
    )
    
    server <- function(input, output) {
      
      df <- data.frame(alphabet = letters[1:10],
                       include = TRUE)
      
      output$table <- rhandsontable::renderRHandsontable({
        
        rhandsontable(df, height = 500)%>%
          hot_col(col = "include",
                  renderer = "
                                          function (instance, td, row, col, prop, value, cellProperties) {
                                             Handsontable.renderers.CheckboxRenderer.apply(this, arguments);
          
                                             if (instance.params) {
                                              var col_value = instance.getData()
                                                            if (col_value[row][col]== false) {
                                                                td.style.background = 'pink';
                                                            }
                                              }
          }")%>%
          hot_col(col = "alphabet",
                  renderer = "
                    function (instance, td, row, col, prop, value, cellProperties) {
                        Handsontable.renderers.TextRenderer.apply(this, arguments);
                        
                        if (instance.params) {
                                              var col_value = instance.getData()
                                                            if (col_value[row][col+1]== false) {
                                                                td.style.background = 'pink';
                                                            }
                                              }
                    }")
        
      })
    }
    
    shinyApp(ui, server

)

Screenshot of table

Upvotes: 1

Related Questions