gpt4 book ai didi

在 Shiny 中使用迷你图渲染数据表

转载 作者:行者123 更新时间:2023-12-05 00:16:40 25 4
gpt4 key购买 nike

我想在 Shiny 的 DT 中包含迷你图。它在 RStudio 查看器中工作正常,但在 Shiny 中不呈现迷你图。这是一个最小的例子。

# dependencies
require(sparkline)
require(DT)
require(shiny)

# create data with sparklines
spark_data <- data.frame(
id = c('spark1', 'spark2'),
spark = c(
spk_chr(values = 1:3, elementId = 'spark1'),
spk_chr(values = 3:1, elementId = 'spark2')
)
)

# render in RStudio viewer (this works)
tbl <- datatable(spark_data, escape = FALSE)
spk_add_deps(tbl)

# render in Shiny (no sparklines rendered in DT)
ui <- fluidPage(
sparklineOutput("test_spark"),
dataTableOutput("tbl")
)

server <- function(input, output) {
# sparkline outside DT (works fine) - also ensures sparkline dependencies are attached
output$test_spark <- renderSparkline(sparkline(1:3))

# sparkline inside DT (does not render)
output$tbl <- renderDataTable(
expr = spark_data,
escape = FALSE
)
}

shinyApp(ui = ui, server = server)

最佳答案

我已经修改了您的代码以生成迷你图。我引用了这个link生成迷你图。

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

# create data
spark_data1<- data.frame(id = c('spark1', 'spark2'),
spark = c("1,2,3", "3,2,1"))



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

server <- function(input, output) {

line_string <- "type: 'line', lineColor: 'black', fillColor: '#ccc', highlightLineColor: 'orange', highlightSpotColor: 'orange'"

cd <- list(list(targets = 1, 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 = "")

output$tbl <- DT::renderDataTable({
dt <- DT::datatable(as.data.frame(spark_data1), rownames = FALSE, options = list(columnDefs = cd,fnDrawCallback = cb))
})
}

shinyApp(ui = ui, server = server)

希望能帮助到你!

关于在 Shiny 中使用迷你图渲染数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41477776/

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