r - Shiny的DT中styleColorBar行的比较
问题描述
我想用 styleColorBar 做行比较。在这个示例中,我想逐行比较 Sepal.Length 和 Sepal.Width。
应用程序.R
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(DT::dataTableOutput("table")),
server = function(input, output) {
iris2 <- iris[c(1:10),c(1,2)]
observe({
output$table <- DT::renderDataTable({
dat <- datatable(iris2,
options = list(
paging = FALSE
)) %>%
formatStyle('Sepal.Length',background = styleColorBar(iris2$Sepal.Length, '#66cdaa'),backgroundSize = '98% 88%',backgroundRepeat = 'no-repeat', backgroundPosition = 'center') %>%
formatStyle('Sepal.Width',background = styleColorBar(iris2$Sepal.Width, '#66cdaa'),backgroundSize = '98% 88%',backgroundRepeat = 'no-repeat', backgroundPosition = 'center')
return(dat)
})
})
})
但是由于styleColorBar是对每一列进行比较,所以左右最大值和最小值不一样,条形长度不匹配
应用程序.R
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(DT::dataTableOutput("table")),
server = function(input, output) {
iris2 <- iris[c(1:10),c(1,2)]
observe({
max <- max(iris2[,1:2], na.rm = TRUE)
max <- c(max,max)
min <- min(iris2[,1:2], na.rm = TRUE)
min <- c(min,min)
iris2 <- rbind(iris2,max,min)
output$table <- DT::renderDataTable({
dat <- datatable(iris2,
options = list(
paging = FALSE
)) %>%
formatStyle('Sepal.Length',background = styleColorBar(iris2$Sepal.Length, '#66cdaa'),backgroundSize = '98% 88%',backgroundRepeat = 'no-repeat', backgroundPosition = 'center') %>%
formatStyle('Sepal.Width',background = styleColorBar(iris2$Sepal.Width, '#66cdaa'),backgroundSize = '98% 88%',backgroundRepeat = 'no-repeat', backgroundPosition = 'center')
return(dat)
})
})
})
因此,我在每一列中添加了 Sepal.Length 和 Sepal.Width 的最大值和最小值的记录,并使条形长度相等。酒吧里的左右比较很顺利,但不是很酷。有没有办法向用户隐藏最后两行?或者我可以在不添加两条线的情况下证明左右条的合理性吗?
解决方案
你可以做
library(DT)
iris2 <- iris[1:10, c(1,2)]
datatable(iris2,
options = list(
paging = FALSE
)) %>%
formatStyle('Sepal.Length',
background = styleColorBar(c(iris2$Sepal.Length, iris2$Sepal.Width), '#66cdaa'),
backgroundSize = '98% 88%',
backgroundRepeat = 'no-repeat',
backgroundPosition = 'center') %>%
formatStyle('Sepal.Width',
background = styleColorBar(c(iris2$Sepal.Length, iris2$Sepal.Width), '#66cdaa'),
backgroundSize = '98% 88%',
backgroundRepeat = 'no-repeat',
backgroundPosition = 'center')
推荐阅读
- c# - 在 WPF .net core 5 中运行时更改应用程序文化时如何更新属性绑定
- python - AttributeError:类型对象“PostPagesTests”没有属性“post”
- android - 即使在 onComplete() 中初始化后,数组上的空指针异常
- c# - 如何通过 Dynamics 365 CRM 中的 C# 插件填充查找字段
- angular - Angular 动态组件的属性未定义
- javascript - ping 是在往返服务器的时间上测量的,还是在发出请求和服务器接收请求之间的时间?
- python - 在 python 中使用 .endswith 查找出现次数
- php - libmysql 之间的默认值是否存在差异
- javascript - 草稿 js(react js) 装饰器,在 1000 字之后,文本背景应该是彩色的高音推文框,如
- python - 将不同长度的字典映射在一起以实现一致的格式