【问题标题】:Change table header colour using htmlTable使用 htmlTable 更改表头颜色
【发布时间】:2017-01-12 19:18:38
【问题描述】:

我在使用 R 中的 htmlTable 包更改 thead 背景颜色时遇到了一些问题。通过使用 css.cell 选项,我能够更改部分表头的颜色,但不是全部(运行代码 sn-p)。

require(htmlTable)
mat <- matrix(1:20, ncol=5)

htmlTable(mat,cgroup = c("","Rank",""),n.cgroup = c(1,3,1),
      header = LETTERS[1:5],
      css.cell = rbind(rep("background: lightgrey;", 
                           times=ncol(mat)),
                       matrix("", ncol=ncol(mat), nrow=nrow(mat))))

# Not Run
# x = htmlTable(mat,cgroup = c("","Rank",""),n.cgroup = c(1,3,1),
#               header = LETTERS[1:5],
#               css.cell = rbind(rep("background: lightgrey;", 
#                                times=ncol(mat)),
#                                matrix("", ncol=ncol(mat), nrow=nrow(mat))))
# get the output
# as.character(x)

代码的输出产生以下内容

<table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;'>\n
  <thead>\n
    <tr>\n
      <th colspan='1' style='font-weight: 900; border-top: 2px solid grey; text-align: center;'></th>
      <th style='border-top: 2px solid grey;; border-bottom: hidden;'>&nbsp;</th>\n
      <th colspan='3' style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Rank</th>
      <th style='border-top: 2px solid grey;; border-bottom: hidden;'>&nbsp;</th>\n
      <th colspan='1' style='font-weight: 900; border-top: 2px solid grey; text-align: center;'></th>\n</tr>\n
    <tr>\n
      <th style='background: lightgrey; border-bottom: 1px solid grey; text-align: center;'>A</th>\n
      <th style='border-bottom: 1px solid grey;' colspan='1'>&nbsp;</th>\n
      <th style='background: lightgrey; border-bottom: 1px solid grey; text-align: center;'>B</th>\n
      <th style='background: lightgrey; border-bottom: 1px solid grey; text-align: center;'>C</th>\n
      <th style='background: lightgrey; border-bottom: 1px solid grey; text-align: center;'>D</th>\n
      <th style='border-bottom: 1px solid grey;' colspan='1'>&nbsp;</th>\n
      <th style='background: lightgrey; border-bottom: 1px solid grey; text-align: center;'>E</th>\n</tr>\n</thead>\n
  <tbody>\n
    <tr>\n
      <td style='text-align: center;'>1</td>\n
      <td style='' colspan='1'>&nbsp;</td>\n
      <td style='text-align: center;'>5</td>\n
      <td style='text-align: center;'>9</td>\n
      <td style='text-align: center;'>13</td>\n
      <td style='' colspan='1'>&nbsp;</td>\n
      <td style='text-align: center;'>17</td>\n</tr>\n
    <tr>\n
      <td style='text-align: center;'>2</td>\n
      <td style='' colspan='1'>&nbsp;</td>\n
      <td style='text-align: center;'>6</td>\n
      <td style='text-align: center;'>10</td>\n
      <td style='text-align: center;'>14</td>\n
      <td style='' colspan='1'>&nbsp;</td>\n
      <td style='text-align: center;'>18</td>\n</tr>\n
    <tr>\n
      <td style='text-align: center;'>3</td>\n
      <td style='' colspan='1'>&nbsp;</td>\n
      <td style='text-align: center;'>7</td>\n
      <td style='text-align: center;'>11</td>\n
      <td style='text-align: center;'>15</td>\n
      <td style='' colspan='1'>&nbsp;</td>\n
      <td style='text-align: center;'>19</td>\n</tr>\n
    <tr>\n
      <td style='border-bottom: 2px solid grey; text-align: center;'>4</td>\n
      <td style='border-bottom: 2px solid grey;' colspan='1'>&nbsp;</td>\n
      <td style='border-bottom: 2px solid grey; text-align: center;'>8</td>\n
      <td style='border-bottom: 2px solid grey; text-align: center;'>12</td>\n
      <td style='border-bottom: 2px solid grey; text-align: center;'>16</td>\n
      <td style='border-bottom: 2px solid grey;' colspan='1'>&nbsp;</td>\n
      <td style='border-bottom: 2px solid grey; text-align: center;'>20</td>\n</tr>\n</tbody>\n</table>

显然“\n”输出很烦人,但很容易管理。但是有人对如何直接从 R 控制表头中的背景颜色有任何想法吗?

我知道我可以通过将以下内容添加到 html 输出来手动调整 thead 样式:style="background-color: lightgrey;",但是如果我可以直接从 R 获得所需的输出,那就太好了。

这是我想要的表格输出,当我手动更改 thead 中的背景颜色时:

<table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;'>\n
  <thead style="background-color: lightgrey;">\n
    <tr>\n
      <th colspan='1' style='font-weight: 900; border-top: 2px solid grey; text-align: center;'></th>
      <th style='border-top: 2px solid grey;; border-bottom: hidden;'>&nbsp;</th>\n
      <th colspan='3' style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Rank</th>
      <th style='border-top: 2px solid grey;; border-bottom: hidden;'>&nbsp;</th>\n
      <th colspan='1' style='font-weight: 900; border-top: 2px solid grey; text-align: center;'></th>\n</tr>\n
    <tr>\n
      <th style='border-bottom: 1px solid grey; text-align: center;'>A</th>\n
      <th style='border-bottom: 1px solid grey;' colspan='1'>&nbsp;</th>\n
      <th style='border-bottom: 1px solid grey; text-align: center;'>B</th>\n
      <th style='border-bottom: 1px solid grey; text-align: center;'>C</th>\n
      <th style='border-bottom: 1px solid grey; text-align: center;'>D</th>\n
      <th style='border-bottom: 1px solid grey;' colspan='1'>&nbsp;</th>\n
      <th style='border-bottom: 1px solid grey; text-align: center;'>E</th>\n</tr>\n</thead>\n
  <tbody>\n
    <tr>\n
      <td style='text-align: center;'>1</td>\n
      <td style='' colspan='1'>&nbsp;</td>\n
      <td style='text-align: center;'>5</td>\n
      <td style='text-align: center;'>9</td>\n
      <td style='text-align: center;'>13</td>\n
      <td style='' colspan='1'>&nbsp;</td>\n
      <td style='text-align: center;'>17</td>\n</tr>\n
    <tr>\n
      <td style='text-align: center;'>2</td>\n
      <td style='' colspan='1'>&nbsp;</td>\n
      <td style='text-align: center;'>6</td>\n
      <td style='text-align: center;'>10</td>\n
      <td style='text-align: center;'>14</td>\n
      <td style='' colspan='1'>&nbsp;</td>\n
      <td style='text-align: center;'>18</td>\n</tr>\n
    <tr>\n
      <td style='text-align: center;'>3</td>\n
      <td style='' colspan='1'>&nbsp;</td>\n
      <td style='text-align: center;'>7</td>\n
      <td style='text-align: center;'>11</td>\n
      <td style='text-align: center;'>15</td>\n
      <td style='' colspan='1'>&nbsp;</td>\n
      <td style='text-align: center;'>19</td>\n</tr>\n
    <tr>\n
      <td style='border-bottom: 2px solid grey; text-align: center;'>4</td>\n
      <td style='border-bottom: 2px solid grey;' colspan='1'>&nbsp;</td>\n
      <td style='border-bottom: 2px solid grey; text-align: center;'>8</td>\n
      <td style='border-bottom: 2px solid grey; text-align: center;'>12</td>\n
      <td style='border-bottom: 2px solid grey; text-align: center;'>16</td>\n
      <td style='border-bottom: 2px solid grey;' colspan='1'>&nbsp;</td>\n
      <td style='border-bottom: 2px solid grey; text-align: center;'>20</td>\n</tr>\n</tbody>\n</table>

额外的奖励,如果有人知道如何摆脱“\n”输出,这也将非常有帮助

【问题讨论】:

    标签: html r


    【解决方案1】:

    您可能需要考虑使用tableHTML,因为它增加了对 CSS 的完全控制。 tableHTML 使用管道运算符链接命令。

    使用您的数据:

    library(tableHTML)
    mat %>%
     as.data.frame() %>%
     #tableHTML is the main function that creates the html table
     tableHTML(border = 0, rownames = FALSE, 
               second_header = list(c(1, 3, 1), c('', 'Rank', ''))) %>%
     #the add_css family of functions add CSS to the corresponding
     #parts of the table
     add_css_thead(list(c('background-color'), c('lightgrey'))) %>%
     add_css_table(list(c('text-align'), c('center'))) %>%
     add_css_row(css = list('border-top', '2px solid black'), rows = 1) %>%
     add_css_row(css = list('border-bottom', '1px solid black'), rows = 2) %>%
     add_css_row(css = list('border-bottom', '2px solid black'), rows = 6) %>%
     add_css_second_header(css = list('border-bottom', '1px solid black'), second_headers = 2)
    

    这会产生你想要的表格,但不需要使用hack来修改tablehtml或任何其他包(或需要手动处理/ns):

    如果您愿意,您仍然可以进一步修改它。还有一个在线教程here如果你有兴趣。

    【讨论】:

    • 教程很有帮助
    【解决方案2】:

    我对 R 中的htmlTable 不是很熟悉,但这里有一个使用XML 包来操纵htmlTable 输出的过度杀伤解决方案。

    require(htmlTable)
    mat <- matrix(1:20, ncol=5)
    
    tab <- htmlTable(mat,cgroup = c("","Rank",""),n.cgroup = c(1,3,1),
          header = LETTERS[1:5],
          css.cell = rbind(rep("background: lightgrey;", 
                               times=ncol(mat)),
                           matrix("", ncol=ncol(mat), nrow=nrow(mat))))
    
    
    library(XML)
    
    # Parse the HTML table and add the background color attribute
    doc <- htmlParse(tab)
    addAttributes(doc[['//thead']], 'style' = "background-color: lightgrey;")
    
    # Convert HTML DOM object to string
    html_str <- toString.XMLNode(doc)
    
    # Remove line escapes, as requested
    html_str <- gsub('\\\n', '', html_str)
    
    # Remove the DOCTYPE included by htmlParse()
    html_str <- gsub('<!DOCTYPE[^>]*>', '', html_str)
    
    cat(html_str)
    

    它输出一个创建所需表格的 HTML 字符串:

    &lt;html&gt;&lt;body&gt;&lt;table class="gmisc_table" style="border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;"&gt;&lt;thead style="background-color: lightgrey;"&gt;&lt;tr&gt;&lt;th colspan="1" style="font-weight: 900; border-top: 2px solid grey; text-align: center;"&gt;&lt;/th&gt;&lt;th style="border-top: 2px solid grey;; border-bottom: hidden;"&gt; &lt;/th&gt;&lt;th colspan="3" style="font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;"&gt;Rank&lt;/th&gt;&lt;th style="border-top: 2px solid grey;; border-bottom: hidden;"&gt; &lt;/th&gt;&lt;th colspan="1" style="font-weight: 900; border-top: 2px solid grey; text-align: center;"&gt;&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th style="background: lightgrey; border-bottom: 1px solid grey; text-align: center;"&gt;A&lt;/th&gt;&lt;th style="border-bottom: 1px solid grey;" colspan="1"&gt; &lt;/th&gt;&lt;th style="background: lightgrey; border-bottom: 1px solid grey; text-align: center;"&gt;B&lt;/th&gt;&lt;th style="background: lightgrey; border-bottom: 1px solid grey; text-align: center;"&gt;C&lt;/th&gt;&lt;th style="background: lightgrey; border-bottom: 1px solid grey; text-align: center;"&gt;D&lt;/th&gt;&lt;th style="border-bottom: 1px solid grey;" colspan="1"&gt; &lt;/th&gt;&lt;th style="background: lightgrey; border-bottom: 1px solid grey; text-align: center;"&gt;E&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;1&lt;/td&gt;&lt;td style="" colspan="1"&gt; &lt;/td&gt;&lt;td style="text-align: center;"&gt;5&lt;/td&gt;&lt;td style="text-align: center;"&gt;9&lt;/td&gt;&lt;td style="text-align: center;"&gt;13&lt;/td&gt;&lt;td style="" colspan="1"&gt; &lt;/td&gt;&lt;td style="text-align: center;"&gt;17&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;2&lt;/td&gt;&lt;td style="" colspan="1"&gt; &lt;/td&gt;&lt;td style="text-align: center;"&gt;6&lt;/td&gt;&lt;td style="text-align: center;"&gt;10&lt;/td&gt;&lt;td style="text-align: center;"&gt;14&lt;/td&gt;&lt;td style="" colspan="1"&gt; &lt;/td&gt;&lt;td style="text-align: center;"&gt;18&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;3&lt;/td&gt;&lt;td style="" colspan="1"&gt; &lt;/td&gt;&lt;td style="text-align: center;"&gt;7&lt;/td&gt;&lt;td style="text-align: center;"&gt;11&lt;/td&gt;&lt;td style="text-align: center;"&gt;15&lt;/td&gt;&lt;td style="" colspan="1"&gt; &lt;/td&gt;&lt;td style="text-align: center;"&gt;19&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style="border-bottom: 2px solid grey; text-align: center;"&gt;4&lt;/td&gt;&lt;td style="border-bottom: 2px solid grey;" colspan="1"&gt; &lt;/td&gt;&lt;td style="border-bottom: 2px solid grey; text-align: center;"&gt;8&lt;/td&gt;&lt;td style="border-bottom: 2px solid grey; text-align: center;"&gt;12&lt;/td&gt;&lt;td style="border-bottom: 2px solid grey; text-align: center;"&gt;16&lt;/td&gt;&lt;td style="border-bottom: 2px solid grey;" colspan="1"&gt; &lt;/td&gt;&lt;td style="border-bottom: 2px solid grey; text-align: center;"&gt;20&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;

    【讨论】:

      猜你喜欢
      • 2013-01-21
      • 2014-07-18
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多