You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R Shiny中如何通过renderTable取消列名加粗并缩小其字号

Customize Table Header Style in Shiny's renderTable

Got it, let's fix this for you! Since you already know how to adjust the overall table font size with CSS, we just need to target the table headers specifically to remove their bold styling and shrink their font size independently. Here's a straightforward, flexible solution using the iris dataset as an example:

Full Shiny App Example

library(shiny)

ui <- fluidPage(
  # Embed custom CSS to style the table and its headers
  tags$style(HTML("
    /* Optional: Shrink the entire table's font size if you still need this */
    table {
      font-size: 13px;
    }
    /* Target ONLY the table header cells to remove bold and reduce size */
    table thead th {
      font-weight: normal; /* Removes default bold styling */
      font-size: 11px; /* Adjust this value to your preferred header size */
    }
  ")),
  
  mainPanel(
    # Render the iris table with your desired built-in styles
    renderTable({
      iris
    }, striped = TRUE, hover = TRUE)
  )
)

server <- function(input, output) {}

shinyApp(ui, server)

How This Works

  1. Precise CSS Targeting: The selector table thead th specifically targets every header cell (<th>) inside the table's header section (<thead>). This lets us modify only the headers without affecting the table body content.
    • font-weight: normal overrides the default bold styling of Shiny table headers.
    • font-size: 11px sets a smaller font size for headers (you can tweak this number to match your exact needs).
  2. Optional Overall Table Size: If you still want the entire table to have a smaller font, keep the table { font-size: 13px; } rule—this will apply to body cells, while the header uses its own smaller size for clear hierarchy.

Alternative (Less Flexible) Method: Manual Header Styling

If you prefer to define styles directly in the renderTable call (not recommended for dynamic datasets), you can use the header parameter to inject inline styles:

renderTable({
  iris
}, 
# Generate styled headers for each column
header = paste0("<th style='font-weight:normal; font-size:11px;'>", colnames(iris), "</th>"),
striped = TRUE, hover = TRUE)

This works but requires you to manually generate headers, which is a hassle if your column names change dynamically. The CSS method above is far more maintainable for most use cases.

内容的提问来源于stack exchange,提问作者tryingtosolve

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:49:59