簡體   English   中英

如何在R shiny中的數據表中對齊和繪制復雜的header的邊框?

[英]How to align and draw borders on complex header for datatable in R shiny?

我正在使用容器使用此處的示例創建 header。 我希望將“總體”和“住院患者”header 居中對齊。我嘗試在 function 中添加 align =“left”參數。它在居中對齊時僅考慮兩列。 我還試圖在三個 header 列之間添加行。

這是代碼片段

  # a custom table container
     sketch = htmltools::withTags(table(
      class = 'display',
      thead(
        tr(
          th(rowspan = 2, style = "text-align:center; border-right: solid 2px;", 'Condition'),
          th(align = 'center',
            th(colspan = 5, style = "text-align:center;",'Overall'),
            th(colspan = 5, style = "text-align:center;",'In-patient'),
            th(colspan = 5, style = "text-align:center;",'Out-patient')
          )
        ),
        tr(
          lapply(rep(c('US.pat.count','freq','cond.rate','exp.rate','odds.ratio'), 3), th)
        )
      )
    ))

datatable(df,rownames = FALSE,
      container = sketch,
      extensions = "Buttons",

      options = list(
        dom = "Blfrtip",
        scrollX = TRUE,
        buttons = c("excel"),
        pageLength = 10,
        lengthMenu = c(10,20,50)
      )
) %>% formatStyle(c(1,6,11,16), `border-right` = "solid 2px")
    

我也想在列之間添加線條

這是我使用的示例數據

structure(list(concept_name = structure(c(8L, 4L, 9L, 10L, 6L, 
3L, 2L, 7L, 1L, 5L), .Label = c("Abnormal findings on diagnostic imaging of lung", 
"Anemia", "Disorder of bone", "Essential hypertension", "Fatigue", 
"Hyperlipidemia", "Low back pain", "Primary malignant neoplasm of prostate", 
"Raised prostate specific antigen", "Secondary malignant neoplasm of bone"
), class = "factor"), w_pop = c(220103, 172228, 120983, 132156, 
134501, 63495, 76318, 64025, 65184, 63564), freq = c("97.36%", 
"76.18%", "53.52%", "58.46%", "59.50%", "28.09%", "33.76%", "28.32%", 
"28.83%", "28.12%"), w_cond_rate = c(1, 0.81, 0.58, 0.62, 0.72, 
0.45, 0.48, 0.37, 0.39, 0.54), w_exp_rate = c(0.11, 0.74, 0.13, 
0.01, 0.7, 0.12, 0.21, 0.26, 0.15, 0.29), w_odds_ratio = c(2006.65, 
1.52, 8.99, 144.41, 1.15, 6.19, 3.55, 1.64, 3.6, 2.91), w_pop_in = c(77528, 
75272, 3560, 44813, 51407, 5062, 27250, 7084, 21970, 5312), freq_in = c("62.15%", 
"60.34%", "2.85%", "35.92%", "41.21%", "4.06%", "21.84%", "5.68%", 
"17.61%", "4.26%"), w_cond_rate_inp = c(0.6288, 0.6224, 0.0287, 
0.3755, 0.4539, 0.0782, 0.3305, 0.0701, 0.2193, 0.2548), w_exp_rate_inp = c(0.022457, 
0.33757, 0.000987, 0.007383, 0.259038, 0.015242, 0.104238, 0.018032, 
0.102137, 0.088998), w_odds_ratio_inp = c(73.75, 3.24, 29.96, 
80.87, 2.38, 5.49, 4.24, 4.11, 2.47, 3.5), w_pop_out = c(219908, 
169664, 115765, 130459, 128855, 57169, 71833, 59935, 56476, 54998
), freq_out = c("97.28%", "75.05%", "51.21%", "57.71%", "57.00%", 
"25.29%", "31.77%", "26.51%", "24.98%", "24.33%"), w_cond_rate_outp = c(0.9952, 
0.7906, 0.5568, 0.6119, 0.7001, 0.412, 0.4461, 0.3463, 0.3274, 
0.4791), w_exp_rate_outp = c(0.113117, 0.730356, 0.130933, 0.010632, 
0.684835, 0.104181, 0.192314, 0.255055, 0.115249, 0.261738), 
    w_odds_ratio_outp = c(1651.78, 1.39, 8.34, 146.8, 1.07, 6.03, 
    3.38, 1.55, 3.74, 2.6)), class = c("data.table", "data.frame"
), row.names = c(NA, -10L), .internal.selfref = <pointer: 0x55e2614c3650>)

我認為您必須共享原始數據才能獲得最佳答案。 DT 將使標題居中,除非您告訴它這樣做。 但是,數據很可能是右對齊的。 您的圖片看起來標題不居中。

這是一個例子。 我使用了您使用 Iris 數據提供的鏈接中的代碼。

在此處輸入圖像描述

看起來“萼片”和“花瓣”偏離了中心。 它們居中。 但是,“長度”和“寬度”不居中。

在此處輸入圖像描述

如果我以一切為中心,感知就會與現實相匹配。

datatable(iris[1:20, c(5, 1:4)], 
          options = list(columnDefs = list(list(
            className = 'dt-center', targets = 0:4))),
          container = sketch, rownames = FALSE)

在此處輸入圖像描述



更新

很高興看到您添加了數據。 因此,我首先看到的是數據框中的數據的列數比您考慮的要多,關於標題。 表標題中還有兩個人口字段,但只有一個對數據框中的每個部分有意義。

由於您使用的名稱和表中的名稱不同,我使用舊的“最佳猜測”方法來確定它們的順序。您將看到我在代碼中使用的確切順序。

names(df) # 16 names and they're not in the same order as the heading names?
#  [1] "concept_name"      "w_pop"             "freq"              "w_cond_rate"      
#  [5] "w_exp_rate"        "w_odds_ratio"      "w_pop_in"          "freq_in"          
#  [9] "w_cond_rate_inp"   "w_exp_rate_inp"    "w_odds_ratio_inp"  "w_pop_out"        
# [13] "freq_out"          "w_cond_rate_outp"  "w_exp_rate_outp"   "w_odds_ratio_outp" 

# the column order--- I think? There isn't a US vs not US pop?
# 1, 2, 7, 8, 9, 10, 11, 2?? 12, =>
df1 <- df[, c(1, 2, 2:6, 2, 7:11)]

接下來我注意到的是垂直邊框。 他們似乎沒有落在列跨度發生變化的地方。 如果您希望在第一列之后有一個邊框,在另外六列之后有一個邊框,那么您可以將其編碼為在第 1 列和第 7 列之后,而不是第 1 列和第 6 列之后。

datatable(df1,
          rownames = FALSE,
          container = sketch,
          extensions = "Buttons",
          
          options = list(
            dom = "Blfrtip",
            scrollX = TRUE,
            buttons = c("excel"),
            pageLength = 10,
            lengthMenu = c(10,20,50)
          )
) %>% formatStyle(c(1,7), `border-right` = "solid 2px")

這些更改給了我這個 output:

在此處輸入圖像描述

所有標題都已居中。 列中的數據右對齊(第一列除外)。 要使每一列(第一列除外)中的數據datatable ,您可以像這樣修改對數據表的調用:

datatable(df1,
          rownames = FALSE,
          container = sketch,
          extensions = "Buttons",
          
          options = list(
            dom = "Blfrtip",
            scrollX = TRUE,
            buttons = c("excel"),
            pageLength = 10,
            lengthMenu = c(10,20,50),
            columnDefs = list(list(className = 'dt-center',
                                   targets = 1:12))
          )
) %>% formatStyle(c(1,7), `border-right` = "solid 2px")

在此處輸入圖像描述

如果您希望它在美學上更加均勻,您可以將每個字段的舍入設置為特定位數。

如果這不起作用,那么表之前或之后的某些內容正在更改 styles。例如,如果您在 Shiny 應用程序或 RMarkdown 中使用它。 如果是這樣的話,你必須讓我知道。 我們會想辦法的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM