问题描述
Q1:我想在Shiny中更改其可空单元格的格式,使其内容为空。
我以为我是使用hot_cols(renderer = "...")
找到它的,但是我对结果感到非常惊讶:内容为0的单元格也被突出显示。有人可以告诉我如何通过R测试JS中的空度吗?
我尝试了value === ''
和isEmpty()都没有成功。
Q2:另外,如果我们在第3列中输入“ 1e6”,则显示的值确实为1000000,但是其背景变为红色:有什么方法可以防止它出现?即允许科学记数法输入?
这是一个最小的可复制示例:
library(shiny)
library(rhandsontable)
DF <- data.frame(col1 = c(1,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,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