无法在 R Shiny 中使用 renderDT 修复第一列?

Unable to fix the first column using renderDT in R Shiny?

在我下面发布的示例中,我有一个包含 100 行和 100 列的带有数值(第一列除外)的 df,我想在 Shiny 的主面板上使用顺序色标打印。由于无法将所有列都放在一个 window 中,我想在保持第一列固定的情况下使用水平滚动。这是我的尝试。不知道我要去哪里错了。 如您所见,我正在使用 DT 包。如果有人也可以向我展示可反应包的解决方案,我将不胜感激。

library(shiny)
library(tidyverse)
df <- matrix(runif(100*100,0,1000) %>% floor(),nrow = 100,ncol = 100) %>% as_tibble() %>% 
    mutate(V1=rep_len(letters,length.out = 100))
# Define UI for application that draws a histogram
ui <- fluidPage(
    # Application title
    titlePanel("Table"),
    sidebarLayout(
        # Show the table
        sidebarPanel(),
        mainPanel(
           DT::DTOutput("table")
        )
    )
)
# Define server logic
server <- function(input, output) {
    output$table <- DT::renderDT({
        #splitting values into quantiles
        brks <- quantile(df %>% select(-1), probs = seq(0,1,length.out = 39), na.rm = TRUE)
        #creating a blue palette
        clr_plt_blue_func <- colorRampPalette(c("white","lightblue","skyblue","royalblue","navyblue"))
        clr_plt_blue <- clr_plt_blue_func(40)
        DT::datatable(df,options = list(pageLength=20,
                                        class= 'cell-border stripe',
                                        searching=TRUE,
                                        extensions='FixedColumns',
                                        scrollX = TRUE,
                                        fixedColumns=list(leftColumns=2)
        )
        ) %>% 
            DT::formatStyle(columns =  names(df)[-1], 
                            backgroundColor = DT::styleInterval(brks, clr_plt_blue),
                            color = 'red',
                            fontWeight = 'bold')
    })
}
# Run the application 
shinyApp(ui = ui, server = server)

DT

In DT extensions='FixedColumns' 需要是 datatable 中的直接参数而不是 options.

DT::datatable(df,options = list(pageLength=20,
                                    class= 'cell-border stripe',
                                    searching=TRUE,
                                    scrollX = TRUE,
                                    fixedColumns = list(leftColumns = 2)),
                  extensions='FixedColumns'
                  
                  
    ) %>% 
      DT::formatStyle(columns =  names(df)[-1], 
                      backgroundColor = DT::styleInterval(brks, clr_plt_blue),
                      color = 'red',
                      fontWeight = 'bold')

可反应

这是 reactable 的示例。固定列显示在浏览器中,可能不在 R 的查看器选项卡中。

pal <- function(x) rgb(colorRamp(c("white","lightblue","skyblue","royalblue","navyblue"))(x), maxColorValue = 255)

reactable(
  df,
  #x and y scrollable
  pagination = FALSE,
  height = 500,
  #define style for 2:100 column
  defaultColDef = colDef(
    style = function(value) {
      if (!is.numeric(value)) return()
      normalized <- (value - min(df[, -1])) / (max(df[, -1]) - min(df[, -1]))
      color <- pal(normalized)
      list(background = color, color = "red")
    },
    minWidth = 50
  ),
  rownames = TRUE,
  #fix first column and rownames
  columns = list(
    V1 = colDef(
      style = list(position = "sticky",
                   left = "50px", 
                   background = "#fff", 
                   zIndex = 1),
      headerStyle = list(position = "sticky", 
                         left = "50px",
                         background = "#fff", 
                         zIndex = 1)
    ),
    .rownames = colDef(
      style = list(position = "sticky",
                   left = 0,
                   background = "#fff", 
                   color = "black"),
      headerStyle = list(position = "sticky", 
                         left = 0,
                         background = "#fff", 
                         zIndex = 1)
    )
  )
)