[英]R/markdown: rendering html tags inside table
I want to create some sort of html-tag cheatsheet in [R] using markdown.我想使用 markdown 在 [R] 中创建某种 html-tag 备忘单。 I thought this would be a good idea as I could easily show the tag and the result.
我认为这将是一个好主意,因为我可以轻松地显示标签和结果。 Turns out it is not that easy.
事实证明这并不容易。 Let's reframe the sentence: I think it should be easy, but still I am stuck when it comes to printing the results.
让我们重新定义这句话:我认为这应该很容易,但在打印结果时我仍然卡住了。 I would really appreciate some hints:)
我真的很感激一些提示:)
What I am doing right now:我现在在做什么:
---
title: "HTML Cheatsheet"
output: html_document
---
```{r, results="asis"}
# init
"%>%" <- magrittr::"%>%"
# create table
tbl <- dplyr::tibble(
"TAG"="<h1></h1>",
"EXAMPLE"="<h1>Headline</h1>",
"RESULT"=cat("<h1>Headline</h1>"))
# print table
tbl %>%
knitr::kable() %>%
kableExtra::kable_styling()
```
My approah does not work.我的方法不起作用。 I think it's because
cat("<h1>Headline</h1>")
is printed directely and ruins the tibble.我认为这是因为
cat("<h1>Headline</h1>")
直接打印并破坏了小标题。 Probably there is a super easy solution to the problem.可能有一个超级简单的解决方案。 Unfortunately I am missing the right words to search for answers to my problem (the only results I get is 'how to print html tables in markdown')
不幸的是,我缺少正确的词来搜索我的问题的答案(我得到的唯一结果是'如何在降价中打印 html 表')
Thanks for your expertise:)感谢您的专业知识:)
Solution:解决方案:
As nate mentioned, I had to use knitr::kable(escape=FALSE)
to render the html tags.正如 nate 所提到的,我不得不使用
knitr::kable(escape=FALSE)
来渲染 html 标签。 To keep the tags in non rendered form in the EXAMPLE column the only thing I had to do was escape them manually.为了使标签在示例列中保持非呈现形式,我唯一要做的就是手动转义它们。
---
title: "HTML Cheatsheet"
output: html_document
---
```{r, results="asis"}
# init
"%>%" <- magrittr::"%>%"
# create table
tbl <- dplyr::tibble(
"TAG"="<h1></h1>",
"EXAMPLE"="\\<h1\\>Headline\\</h1\\>",
"RESULT"=cat("<h1>Headline</h1>"))
# print table
tbl %>%
knitr::kable(escape=FALSE) %>%
kableExtra::kable_styling()
```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.