Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中格式化Html表-使用Css_Html_Css_R - Fatal编程技术网

在R中格式化Html表-使用Css

在R中格式化Html表-使用Css,html,css,r,Html,Css,R,我正在R中使用包“htmlTable”,并试图格式化一个表。具体来说,我想删除最上面的边框,并将其余边框更改为黑色。请运行下面的代码以获取我正在查看的表 任何帮助都将不胜感激 devtools::install_github('SwedishPensionsAgency/format.tables') library(htmlTable) Code <- ("AB", "BC", "MB", "NB") Numbers <- c(148137, 186955, 37755, 173

我正在R中使用包“htmlTable”,并试图格式化一个表。具体来说,我想删除最上面的边框,并将其余边框更改为黑色。请运行下面的代码以获取我正在查看的表

任何帮助都将不胜感激

devtools::install_github('SwedishPensionsAgency/format.tables')
library(htmlTable)

Code <- ("AB", "BC", "MB", "NB")
Numbers <- c(148137, 186955, 37755, 17376)
DataFrame <- data.frame(Code, Numbers, stringsAsFactors = FALSE)


htmlTable(DataFrame, align = "c",
      rnames = FALSE,
      caption = "<b> <center> <font face = Times New Roman> Table 1. Test  <br> <br>",
      tfoot = "<b> Source </b> <br> [1]  Test Source",
      header = paste(c(" Territory", "Number of People")),
      css.caption = "color:red;",
      col.rgroup = c("none", "#ADADAD"),
      padding.tspanner = "", ctable = TRUE,
      css.table = "width:150%;border: none")
devtools::install\u github('SwedishPensionsAgency/format.tables'))
图书馆(htmlTable)

代码如果您了解基本CSS,您可以非常轻松地格式化表中的任何元素:

x <- htmlTable(DataFrame, align = "c",
      rnames = FALSE,
      caption = "<b> <center> <font face = Times New Roman> Table 1. Test  <br> <br>",
      tfoot = "<b> Source </b> <br> [1]  Test Source",
      header = paste(c(" Territory", "Number of People")),
      css.caption = "color:red;",
      col.rgroup = c("none", "#ADADAD"),
      padding.tspanner = "", ctable = TRUE)


## add id to gmisc_table
x <- gsub('(?<=.gmisc_table.)', ' id = \'gmisc_table\'', x, perl = TRUE)
formats <- paste(x)
attributes(formats) <- attributes(x)

## Edit css
css <- '
<style>
/* Remove the top border */
#gmisc_table > thead > tr > th {
  border-top: none !important;
}

/* Add boarder to the table body */
#gmisc_table > tbody > tr > td {
  border: 2px solid black;
}
</style>'

gsub('^', css, formats)

x在我看来,您用于生成
的方法限制了您的格式化选项。下面是我的建议,若你们想绝对控制风格,你们应该怎么做。它还允许您使用以下资源修复完美的格式:

您的数据:

Code <- c("AB", "BC", "MB", "NB")
Numbers <- c(148137, 186955, 37755, 17376)
DataFrame <- data.frame(Code, Numbers, stringsAsFactors = FALSE)
names(DataFrame) <- c("Territory", "Number of People")
基本表格结构和添加样式:

my_table <- hlt::html_table(DataFrame)
hlt::tag_attr(my_table) <- list(id = "table1", class = "gmisc_table")
my_表
my_table <- hlt::html_table(DataFrame)
hlt::tag_attr(my_table) <- list(id = "table1", class = "gmisc_table")
a <- html_p("Table 1. Test")
b <- html_p("<b>Source</b><br>[1] Test Source")
my_table <- hlt::html_div(a +
                          my_table +
                          b)
hlt::tag_attr(my_table) <- list(style = "margin: 0 auto; display: table; margin-top: 1em;")
head <- hlt::html_head(hlt::html_meta(charset="utf-8") +
                       hlt::html_meta("http-equiv" = "Content-type") +
                       hlt::html_meta("content" = "text/html") +
                       hlt::html_style(c(".gmisc_table {",
                                         "    width:150%;",
                                         "    border:1px solid black;",
                                         "    border-collapse:collapse",
                                         "}",
                                         ".gmisc_table th {",
                                         "    border-bottom: 2px solid grey;",
                                         "    border-left: 1px solid black;",
                                         "    text-align: center;",
                                         "}",
                                         ".gmisc_table tr:nth-child(even) {",
                                         "    background-color: #adadad;",
                                         "}",
                                         ".gmisc_table tr:nth-child(odd) {",
                                         "    background-color: transparent;",
                                         "}",
                                         ".gmisc_table td {",
                                         "    background-color: transparent;",
                                         "    text-align: center;",
                                         "    border-left: 1px solid black",
                                         "}")))
page <- hlt::html_html(head +
                       hlt::html_body(my_table))
tab <- tempfile()
capture.output(file = tab, print(page))
browseURL(tab)