2

嗨,感谢您阅读我

我正在使用 r 中的“reactable”包处理一些表格,但我只想将每列的标题居中。例如:

library(reactable)
reactable(iris,
          defaultColDef = colDef(
            header = function(value) gsub(".", " ", value, fixed = TRUE),
            cell = function(value) format(value, nsmall = 1),
            align = "center",
            minWidth = 70,
            headerStyle = list(background = "#12a09a")
          ))

显然该选项colDef(align = "center")使整个列居中,但是是否有仅使标题居中的选项?

4

1 回答 1

0

我假设您在 R markdown 文档或闪亮的应用程序中使用此表。在两者中,您都可以覆盖css以左对齐表格文本或让默认表格和标题居中。

在第一种情况下,您只需要添加这一行.rt-align-center{text-align: left;}

在闪亮中,您只需将其添加到您的自定义css或使用样式标签将其添加到内联,如下例所示:

library(shiny)
library(reactable)


ui <- fluidPage(
    # inline style tag to define table row text alignment
    tags$style(HTML(".rt-align-center {text-align: left;}")),

    titlePanel("Iris react table"),

    sidebarLayout(
        sidebarPanel(
           helpText("Nothing to see here")
        ),
        mainPanel(
            reactableOutput("table")
        )

    )
)

server <- function(input, output) {

    output$table <- renderReactable({
    
        reactable(iris,
                  defaultColDef = colDef(
                      header = function(value) gsub(".", " ", value, fixed = TRUE),
                      cell = function(value) format(value, nsmall = 1),
                      align = "center",
                      minWidth = 70,
                      headerStyle = list(background = "#12a09a")
                  ))
    })
}

shinyApp(ui = ui, server = server)

如果您正在使用 Rmarkdown 文档,只需将其添加到样式标记中的 html 中,如下所示:

<style> .rt-align-center {text-align: left;} </style>

结果:

想要的表

于 2021-09-19T20:34:41.837 回答