【问题标题】:Shiny Rhandsontable - Renderer - Use color from factor (Pallete)Shiny Rhandsontable - 渲染器 - 使用来自因子的颜色(调色板)
【发布时间】:2019-03-28 17:51:56
【问题描述】:

我有一个数据集,我根据另一列创建了一个带有颜色代码的列。

 colourCount = length(unique(Desc.File$VARIABLECODE))
getPalette = colorRampPalette(brewer.pal(9, "Set1"))
Colors <- getPalette(colourCount) 

Desc.File$ColorCode <- factor(Desc.File$VARIABLECODE, labels = Colors)

创建的对象是一个十六进制颜色代码列表

“#E41A1C”“#D42229”“#C52B37”“#B63445”“#A73D52”“#974560”“#884E6E”“#79577C”“#6A6089”“#5B6997” “#4B71A5” “#3C7AB2” “#3880B1” “#3A85A8” “#3C899E” “#3E8D94” “#3F918B” “#419681” “#439A77” “#459E6E” "#47A364" "#49A75A"

在本专栏中,我将颜色采购到绘图中,并且我还想在 Rhandsontable 的背景/文本颜色上使用相同的颜色。

color_renderer = "function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);

clr   = instance.params.ColorCode

td.style.background=  hex(clr[row])
}
"

colnames(Summary.tab) <- c("Code", "Brand", "Type", "Description", "Metric", "ColorCode", "Execution", "Cost")
Summary.tab <- data.frame(Check=rep(TRUE, n),Summary.tab)
DT = rhandsontable(Summary.tab, readOnly = FALSE, rowHeaders= NULL, useTypes= TRUE, selectCallback = TRUE) %>% hot_col("Code", renderer=color_renderer)

我试图使用渲染器从我的数据集中的颜色列中获取颜色,然后将其用作背景颜色,但它不起作用。 (我之前从来没有写过JS,所以我不知道我在做什么)

请帮忙:)

【问题讨论】:

  • 有人吗? :(我需要解决这个问题

标签: javascript r shiny rhandsontable


【解决方案1】:

您需要将参数ColorCode 传递给rhansontable,然后您才能在渲染器函数中获取它。示例如下:

library(shiny)
library(rhandsontable)

color_renderer = "
    function(instance, td, row, col, prop, value, cellProperties) {
        Handsontable.renderers.TextRenderer.apply(this, arguments);
        if (instance.params) {
            clr = instance.params.ColorCode
            clr = clr instanceof Array ? clr : [clr]
            td.style.background =  clr[row]
        }

    }"

set.seed(12345)
Desc.File <- data.frame(VARIABLECODE = sample(letters, 10, replace = TRUE))

colourCount <- length(unique(Desc.File$VARIABLECODE))
getPalette <- colorRampPalette(RColorBrewer::brewer.pal(9, "Set1"))
colors <- getPalette(colourCount) 

Desc.File$ColorCode <- factor(Desc.File$VARIABLECODE, labels = colors)


ui <- fluidPage(
    rHandsontableOutput("table")
)

server <- function(input, output) {

    output$table <- renderRHandsontable({
        rhandsontable(Desc.File, ColorCode = Desc.File$ColorCode) %>% 
            hot_cols(renderer = color_renderer)
    })

}

shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2016-04-11
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    相关资源
    最近更新 更多