我有一个数据框
df
,看起来像
索引 | 价值1 | 范围 |
---|---|---|
0.1 | 0.2 | c(1,2,3) |
0.1 | 0.2 | c(8,2,6) |
我想使用 R Shiny 显示表格,以便 Range 列是一个迷你折线图,其中每个单元格都是使用 Range 中的列表创建的
为此我正在使用以下方法
sparkline_html <- function (value) {
sparkline(value, type = "line")
}
df$Price_Range2 = NA
for (i in 1:nrow(df)) {
df$Price_Range2[i] = sparkline_html(df$Price_Range[[i]])
}
reactable(data_latest_ordered)
Price_Range2 列中的单元格应如下图所示
但是我看到的输出是
它不显示图表而是显示
[object Object ]
请告诉我应该怎样做才能更正。
{gt} 和 {gtExtras} 可以帮助解决此问题:
library(shiny)
library(gt)
library(gtExtras)
library(dplyr)
gt_tbl <- dplyr::tibble(
price_range = list(
c(0.964, 0.685, 0.593),
c(0.561, 0.600, 0.868),
c(0.964, 0.742, 0.950)
)
) |>
dplyr::mutate(price_range_2 = price_range) |>
gt::gt() |>
gtExtras::gt_plt_sparkline(column = 2)
ui <- fluidPage(
gt::gt_output(outputId = "table")
)
server <- function(input, output, session) {
output$table <- gt::render_gt(expr = gt_tbl)
}
shinyApp(ui = ui, server = server)
创建于 2024-04-08,使用 reprex v2.1.0