为什么以下不打印“names(data)”的表th-elements?该元素保持为空。有趣的是,tr 部分中的 lapply 似乎确实可以正常工作,没有任何问题。
data <- data.frame(a = 1, b = 2)
htmltools::withTags(table(
class = 'display',
thead(
tr(
th(rowspan = 2, "Test1"),
th(rowspan = 2, "Test2"),
th(rowspan = 2, "Test3"),
th(colspan = 2, names(data)[1]),
th(colspan = 2, names(data)[2])
),
tr(
lapply(rep(c('Abs', 'Change'), 2), th)
)
)
))
Run Code Online (Sandbox Code Playgroud)
给出:
<table class="display">
<thead>
<tr>
<th rowspan="2">Test1</th>
<th rowspan="2">Test2</th>
<th rowspan="2">Test3</th>
<th colspan="2"></th> ## Why empty???
<th colspan="2"></th> ## Why empty???
</tr>
<tr>
<th>Abs</th>
<th>Change</th>
<th>Abs</th>
<th>Change</th>
</tr>
</thead>
</table>
Run Code Online (Sandbox Code Playgroud)
eval这是由和substitute组合引起的问题。我们看一下withTags命令:
> htmltools::withTags
function (code)
{
eval(substitute(code), envir = as.list(tags), enclos = parent.frame())
}
<environment: namespace:htmltools>
Run Code Online (Sandbox Code Playgroud)
它调用此嵌套评估框架内的代码。现在我们可以分解到有趣的部分:
> eval(substitute(names(data)[1]), envir = as.list(tags))
NULL
Run Code Online (Sandbox Code Playgroud)
th这就是导致标签保持为空的原因。现在,为什么会发生这种情况?
> names(htmltools::tags)
[1] "a" "abbr" "address" "area" "article"
[6] "aside" "audio" "b" "base" "bdi"
[11] "bdo" "blockquote" "body" "br" "button"
[16] "canvas" "caption" "cite" "code" "col"
[21] "colgroup" "command" "data" "datalist" "dd"
[26] "del" "details" "dfn" "div" "dl"
[31] "dt" "em" "embed" "eventsource" "fieldset"
[36] "figcaption" "figure" "footer" "form" "h1"
[41] "h2" "h3" "h4" "h5" "h6"
[46] "head" "header" "hgroup" "hr" "html"
[51] "i" "iframe" "img" "input" "ins"
[56] "kbd" "keygen" "label" "legend" "li"
[61] "link" "mark" "map" "menu" "meta"
[66] "meter" "nav" "noscript" "object" "ol"
[71] "optgroup" "option" "output" "p" "param"
[76] "pre" "progress" "q" "ruby" "rp"
[81] "rt" "s" "samp" "script" "section"
[86] "select" "small" "source" "span" "strong"
[91] "style" "sub" "summary" "sup" "table"
[96] "tbody" "td" "textarea" "tfoot" "th"
[101] "thead" "time" "title" "tr" "track"
[106] "u" "ul" "var" "video" "wbr"
Run Code Online (Sandbox Code Playgroud)
如您所见,数据是标签之一,因此它是环境中包含的命令之一。现在使用这组新变量来评估该命令。不出所料:
> names(htmltools::tags$data)
NULL
Run Code Online (Sandbox Code Playgroud)
这就是这种奇怪行为的原因。我引用这里:
笔记
替代在纯粹的词汇基础上起作用。不能保证结果表达式有任何意义。
至于避免这种行为:命名你的 data.framedata1或任何不会被误认为 html 标签的东西就足够了。
| 归档时间: |
|
| 查看次数: |
852 次 |
| 最近记录: |