简体   繁体   English

有条件地格式化 Shiny 中 rHandsontable 中的空单元格

[英]Conditionally format empty cells in rHandsontable in Shiny

Q1: I would like to change the format of a rhandsontable cell in Shiny when its content become empty. Q1:当内容为空时,我想更改 Shiny 中的 rhandsontable 单元格的格式。 I thought I found it using hot_cols(renderer = "...") but I am quite surprised of the result: cells with content 0 are also highlighted.我以为我使用hot_cols(renderer = "...")找到了它,但我对结果感到非常惊讶:内容为 0 的单元格也被突出显示。 Could someone tell me how should I test emptyness in JS through R?有人能告诉我如何通过 R 测试 JS 中的空虚吗? I tried value === '' and isEmpty() without any success.我尝试value === ''和 isEmpty() 没有任何成功。

Q2: Additionnally, if we enter "1e6" in column 3, the value which appears is indeed 1000000 but its background switch to red: any way to prevent it? Q2:另外,如果我们在第3列输入“1e6”,出现的值确实是1000000,但它的背景变成了红色,有什么办法可以防止呢? ie to allow scientific notation input?即允许科学记数法输入?

Here's a minimal reproductible example:这是一个最小的可复制示例:

library(shiny)
library(rhandsontable)

DF <- data.frame(col1 = c(1, 0, 3), col2 = c(letters[23:22], NA), col3 = round(rnorm(3, 1e6, 1e3),0))

server <- shinyServer(function(input, output, session) {
  
  output$rt <- renderRHandsontable({
    rhandsontable(DF) %>%      
      
      # conditional overall formatting > grey empty cells
      hot_cols(renderer = "
           function (instance, td, row, col, prop, value, cellProperties) {
             Handsontable.renderers.NumericRenderer.apply(this, arguments);
             if(!value) {
                td.style.background = '#EEE';
              }
           }")
  })
})

ui <- shinyUI(fluidPage(
  rHandsontableOutput("rt")
))

shinyApp(ui, server)

Regarding your first question: you can add the condition that the value is not 0:关于您的第一个问题:您可以添加值不为 0 的条件:

library(shiny)
library(rhandsontable)

DF <- data.frame(col1 = c(1, 0, 3), col2 = c(letters[23:22], NA), col3 = round(rnorm(3, 1e6, 1e3),0))

server <- shinyServer(function(input, output, session) {
  
  output$rt <- renderRHandsontable({
    rhandsontable(DF) %>%      
      
      # conditional overall formatting > grey empty cells
      hot_cols(renderer = "
           function (instance, td, row, col, prop, value, cellProperties) {
             Handsontable.renderers.NumericRenderer.apply(this, arguments);
             if(!value && value != 0) {
                td.style.background = '#EEE';
              }
           }")
  })
})

ui <- shinyUI(fluidPage(
  rHandsontableOutput("rt")
))

shinyApp(ui, server)

Regarding your second question: it's a known bug that was only fixed in handsontable 6.2.1, but the CRAN version of rhandsontable uses handsontable 6.1.1.关于您的第二个问题:这是一个已知错误,仅在handsontable 6.2.1 中修复,但handsontable rhandsontable The development version seems to be updated to 6.2.2, so you could install it from https://github.com/jrowen/rhandsontable开发版好像更新到了6.2.2,所以可以从https://github.com/jrowen/rhandsontable安装

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM