【问题标题】:Conditionally format empty cells in rHandsontable in Shiny有条件地格式化 Shiny rHandsontable 中的空单元格
【发布时间】:2020-12-02 12:02:02
【问题描述】:

Q1:我想在 Shiny 中的 rhandsontable 单元格内容为空时更改其格式。 我以为我使用hot_cols(renderer = "...") 找到了它,但我对结果感到非常惊讶:内容为 0 的单元格也被突出显示。有人可以告诉我应该如何通过 R 测试 JS 中的空性吗? 我试过 value === '' 和 isEmpty() 没有任何成功。

Q2:另外,如果我们在第3列输入“1e6”,出现的值确实是1000000,但是它的背景变成了红色:有什么办法可以防止呢?即允许输入科学记数法?

这是一个可复制的最小示例:

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)

【问题讨论】:

    标签: r shiny rhandsontable


    【解决方案1】:

    关于你的第一个问题:你可以添加值不为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)
    

    关于你的第二个问题:这是一个已知的bug,它只在handsontable 6.2.1 中修复,但rhandsontable 的CRAN 版本使用handsontable 6.1.1。开发版好像更新到6.2.2了,可以从https://github.com/jrowen/rhandsontable安装

    【讨论】:

    • 我非常确定有一种特定的方法可以同时测试两者...我没有想到这个明显的解决方案 :) 你为什么使用“&&”而不仅仅是“& “?
    • 我对javascript不太了解,但我认为JS中的logical AND&amp;&amp;
    猜你喜欢
    • 2020-03-03
    • 2020-09-09
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    相关资源
    最近更新 更多