r handsontable:自定义渲染器可以否定列格式化命令吗?

r handsontable: Can a custom renderer negate column formatting commands?

我在 Shiny 中渲染的 rhandsontable 效果很好。除其他事项外,列 'Pat. Owes' 和 'Ins. Owes' 显示为美元格式(请参阅下面代码部分底部的第 4 行和第 5 行):

renderRHandsontable({
      sessions_reactive$sessions[[patient_nr]], 
      row_highlight = row_highlight, col_highlight = col_highlight,
      width = 1000, height = 500) %>% 
      hot_rows(fixedRowsTop = 1) %>%
      hot_col("Pat. Owes", format = "[=10=],000.00", language = "en-US") %>%
      hot_col("Ins. Owes", format = "[=10=],000.00", language = "en-US") %>% 
      hot_table(highlightCol = TRUE, highlightRow = TRUE) %>% 
      hot_validate_numeric(cols = c(3, 5), min = 0, max = 500) %>% 
      hot_col(c(1, 3, 5, 6, 8), valign = 'htCenter')
})

下面的代码——但是注释掉了那 2 行美元格式的代码——也可以正常工作。底部的渲染器做了两件事:使第 5 列和第 8 列的字体变为粗体和红色。此外,如果最后一列包含某个字符串,则整行的背景变为黄色。

    rhandsontable(
      sessions_reactive$sessions[[patient_nr]], 
      row_highlight = row_highlight, col_highlight = col_highlight,
      width = 1000, height = 500) %>% 
      hot_rows(fixedRowsTop = 1) %>%
    # hot_col("Pat. Owes", format = "[=11=],000.00", language = "en-US") %>%
    # hot_col("Ins. Owes", format = "[=11=],000.00", language = "en-US") %>% 
      hot_table(highlightCol = TRUE, highlightRow = TRUE) %>% 
      hot_validate_numeric(cols = c(3, 5), min = 0, max = 500) %>% 
      hot_col(c(1, 3, 5, 6, 8), valign = 'htCenter') %>%
      hot_cols(renderer = "
            function (instance, td, row, col, prop, value, cellProperties) {
                     Handsontable.renderers.TextRenderer.apply(this, arguments);

                     if (instance.params) {
                       hrows = instance.params.row_highlight
                       hrows = hrows instanceof Array ? hrows : [hrows]
                       hcols = instance.params.col_highlight
                       hcols = hcols instanceof Array ? hcols : [hcols]

                       if (hrows.includes(row)) {
                         td.style.background = 'yellow'
                       }

                       if (hcols.includes(col)) {
                         td.style.fontWeight = 'bold'
                         td.style.color = '#fc0f03'
                       }
                     }
            }")
  })

我的问题:如果我取消注释专门用于美元格式化的 2 条注释 hot_col 行 - 它们不能与底部的渲染器结合使用。我的意思是,没有错误,但那两列只是没有显示为美元格式——它们只是显示为数字。显然,我在底部的渲染器(尽管它没有具体指代那些列)以某种方式否定了 US$ 格式。 将这两行移动到 rhandsontable 定义的底部也无济于事。渲染内容正常,但美元格式无效。

有什么建议吗? 非常感谢!

将使用 NumericRenderer 而不是 TextRenderer。我的理解是 TextRenderer 是没有验证的默认值。 NumericRenderer 可以帮助格式化数字。

这是 (具有行和列格式),另外 hot_col 指定美元格式:

library(shiny)
library(rhandsontable)

DF = data.frame(a=1:10, b=3:12, c=c("Dog", "Cat", "Mouse", 5:11), d=3:12, e=1:10, f=1:10, g=1:10, h=2:11, Comments = c("missed etc", rep("", 7), "missed", ""))

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

server = function(input, output, session) {

  output$table <- renderRHandsontable({
    row_highlight = which(grepl("missed", DF$Comments))-1
    col_highlight = c(5,8)-1
    rhandsontable(DF, row_highlight = row_highlight, col_highlight = col_highlight, width = 550, height = 300) %>%
      hot_rows(fixedRowsTop = 1) %>%
      hot_col(1, format = "[=10=],000.00", language = "en-US") %>%
      hot_col(2, format = "[=10=],000.00", language = "en-US") %>% 
      hot_table(highlightCol = TRUE, highlightRow = TRUE) %>% 
      hot_validate_numeric(cols = c(3, 5), min = 0, max = 500) %>% 
      hot_col(c(1, 3, 5, 6, 8), valign = 'htCenter') %>%
      hot_cols(renderer = "
            function (instance, td, row, col, prop, value, cellProperties) {
                     Handsontable.renderers.NumericRenderer.apply(this, arguments);

                     if (instance.params) {
                       hrows = instance.params.row_highlight
                       hrows = hrows instanceof Array ? hrows : [hrows]
                       hcols = instance.params.col_highlight
                       hcols = hcols instanceof Array ? hcols : [hcols]

                       if (hrows.includes(row)) {
                         td.style.background = 'yellow' 
                       }

                       if (hcols.includes(col)) {
                         td.style.fontWeight = 'bold'
                         td.style.color = '#fc0f03'
                       }
                     }
            }"
      ) 
  })
}

shinyApp(ui, server)