У меня возникли проблемы с изменением цвета фона thead с помощью пакета htmlTable в R. Играя с параметрами css.cell, я смог изменить цвет части таблицы заголовок, но не все (фрагмент кода запуска).Изменить цвет заголовка таблицы с помощью htmlTable
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;'> </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;'> </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'> </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'> </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'> </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'> </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'> </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'> </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'> </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'> </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'> </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'> </td>\n
<td style='border-bottom: 2px solid grey; text-align: center;'>20</td>\n</tr>\n</tbody>\n</table>
Очевидно, что "\ п" выход раздражает, но легко управляемым. Но есть ли у кого-нибудь идеи о том, как управлять цветом фона в заголовке таблицы непосредственно из R?
Я знаю, что я могу настроить стиль ада вручную, добавив следующее к выходу html: 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;'> </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;'> </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'> </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'> </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'> </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'> </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'> </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'> </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'> </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'> </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'> </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'> </td>\n
<td style='border-bottom: 2px solid grey; text-align: center;'>20</td>\n</tr>\n</tbody>\n</table>
Добавлен бонус, если кто-нибудь знает, как избавиться от «\ п» вывода, который также будет очень полезен
Очень круто. – anotherFishGuy
Учебник был очень полезен – anotherFishGuy
Большое спасибо. Любая обратная связь более чем приветствуется :) – LyzandeR