在 Shiny 中有条件地格式化 rHandsontable 中的空单元格
Conditionally format empty cells in rHandsontable in Shiny
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)
关于你的第一个问题:你可以加上值不为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 that was only fixed in handsontable
6.2.1, but the CRAN version of rhandsontable
uses handsontable
6.1.1. The development version seems to be updated to 6.2.2, so you could install it from https://github.com/jrowen/rhandsontable
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)
关于你的第一个问题:你可以加上值不为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 that was only fixed in handsontable
6.2.1, but the CRAN version of rhandsontable
uses handsontable
6.1.1. The development version seems to be updated to 6.2.2, so you could install it from https://github.com/jrowen/rhandsontable