gpt4 book ai didi

r - 将标签添加到数据表中的迷你图

转载 作者:行者123 更新时间:2023-12-04 21:34:55 25 4
gpt4 key购买 nike

是否可以向迷你图添加自定义标签?

例如,在下面的代码中,我想用标签列中的相应字母标记每个条形。

从以前的建筑 [answer]

require(sparkline)
require(DT)
require(shiny)
require(tibble)

# create data


spark_data1<-tribble(
~id, ~label,~spark,
"a", c("C,D,E"),c("1,2,3"),
"b", c("C,D,E"),c("3,2,1")
)

ui <- fluidPage(
sparklineOutput("test_spark"),
DT::dataTableOutput("tbl")
)

server <- function(input, output) {

output$tbl <- DT::renderDataTable({
line_string <- "type: 'bar'"
cd <- list(list(targets = 2, render = JS("function(data, type, full){ return '<span class=sparkSamples>' + data + '</span>' }")))
cb = JS(paste0("function (oSettings, json) {\n $('.sparkSamples:not(:has(canvas))').sparkline('html', { ",
line_string, " });\n}"), collapse = "")
dt <- DT::datatable(as.data.frame(spark_data1), rownames = FALSE, options = list(columnDefs = cd,fnDrawCallback = cb))

})

}

shinyApp(ui = ui, server = server)

最佳答案

好的,所以我们从获取数据表中的迷你图开始。此 Github issue可能会有所帮助,并提供我认为比原始流行的更好的方法 Combining data tables and sparklines邮政。

在数据表中添加迷你图

我来评论 ####内联解释变化。

require(sparkline)
require(DT)
require(shiny)
require(tibble)

# create data

spark_data1<-tribble(
~id, ~label,~spark,
#### use sparkline::spk_chr helper
#### note spk_chr build for easy usage with dplyr, summarize
"a", c("C,D,E"),spk_chr(1:3,type="bar"),
"b", c("C,D,E"),spk_chr(3:1,type="bar")
)

ui <- tagList(
fluidPage(
DT::dataTableOutput("tbl")
),
#### add dependencies for sparkline in advance
#### since we know we are using
htmlwidgets::getDependency("sparkline", "sparkline")
)

server <- function(input, output) {

output$tbl <- DT::renderDataTable({
cb <- htmlwidgets::JS('function(){debugger;HTMLWidgets.staticRender();}')

dt <- DT::datatable(
as.data.frame(spark_data1),
rownames = FALSE,
escape = FALSE,
options = list(
#### add the drawCallback to static render the sparklines
#### staticRender will not redraw what has already been rendered
drawCallback = cb
)
)

})

}

shinyApp(ui = ui, server = server)

添加带标签的工具提示

我们将借鉴 Github issue的经验做一个小助手函数.
#### helper function for adding the tooltip
spk_tool <- function(labels) {
htmlwidgets::JS(
sprintf(
"function(sparkline, options, field){
return %s[field[0].offset];
}",
jsonlite::toJSON(labels)
)
)
}



live example
screenshot of example
require(sparkline)
require(DT)
require(shiny)
require(tibble)

#### helper function for adding the tooltip
spk_tool <- function(labels) {
htmlwidgets::JS(
sprintf(
"function(sparkline, options, field){
return %s[field[0].offset];
}",
jsonlite::toJSON(labels)
)
)
}

# create data
spark_data1<-tribble(
~id, ~spark,
#### use sparkline::spk_chr helper
#### note spk_chr build for easy usage with dplyr, summarize
"a", spk_chr(1:3,type="bar", tooltipFormatter=spk_tool(c("C","D","E"))),
"b", spk_chr(3:1,type="bar",tooltipFormatter=spk_tool(c("C","D","E")))
)

ui <- tagList(
fluidPage(
DT::dataTableOutput("tbl")
),
#### add dependencies for sparkline in advance
#### since we know we are using
htmlwidgets::getDependency("sparkline", "sparkline")
)

server <- function(input, output) {

output$tbl <- DT::renderDataTable({
cb <- htmlwidgets::JS('function(){debugger;HTMLWidgets.staticRender();}')

dt <- DT::datatable(
as.data.frame(spark_data1),
rownames = FALSE,
escape = FALSE,
options = list(
#### add the drawCallback to static render the sparklines
#### staticRender will not redraw what has already been rendered
drawCallback = cb
)
)

})

}

shinyApp(ui = ui, server = server)

关于r - 将标签添加到数据表中的迷你图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45179410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com