【问题标题】:r handsontable: Can a custom renderer negate column formatting commands?r handsontable:自定义渲染器可以否定列格式命令吗?
【发布时间】:2020-02-01 08:20:36
【问题描述】:

我在 Shiny 中的 rhandsontable 渲染效果很好。除其他外,列'Pat。 Owes 和 Ins。 Owes 的显示格式为 US$(参见下面代码部分底部的第 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 = "$0,000.00", language = "en-US") %>%
      hot_col("Ins. Owes", format = "$0,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 = "$0,000.00", language = "en-US") %>%
    # hot_col("Ins. Owes", format = "$0,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 条专门用于 US$ 格式的已注释 hot_col 行 - 它们不能与底部的渲染器结合使用。我的意思是,没有错误,但是这 2 列只是没有显示为 US$ 格式——它们只是显示为数字。显然,我在底部的渲染器(尽管它没有专门引用这些列)以某种方式否定了 US$ 格式。 将这两行移到 rhandsontable 定义的底部也无济于事。渲染的东西工作正常,但美元格式不工作。

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

【问题讨论】:

  • @Ben 嗨,Ben! :) 真的,rHandsontable 的作者不是在回答很多人的问题时告诉他们应该是文本渲染器吗?
  • 是的,它做到了!惊人的!谢谢你,本!也许让您的评论成为答案,以便我接受?

标签: r rhandsontable


【解决方案1】:

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

这是previous example(带有行和列格式),加上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 = "$0,000.00", language = "en-US") %>%
      hot_col(2, format = "$0,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)

【讨论】:

    猜你喜欢
    • 2015-01-01
    • 2016-12-03
    • 2015-03-29
    • 2017-11-06
    • 2016-04-26
    • 2012-10-14
    • 2018-10-09
    • 2019-07-07
    • 2016-08-07
    相关资源
    最近更新 更多