Is possible to render HTML content on gt ?
data.frame(A = "Text", B = html("<em>Text</em>")) %>% gt()
| A | B |
|---|---|
| AAAA | <em>HTML</em> |
I had the same question, the problem seems to be that the < and > signs get converted to their HTML entities < and >, respectively, when passed into gt.
My solution was to create my own html function that replaced the entities with their signs before calling the HTML function.
html <- function(text, replace_sign_entities = FALSE, ...) {
if(replace_sign_entities) {
text <- stringr::str_replace_all(text, c("<" = "<", ">" = ">"))
}
htmltools::HTML(text, ...)
}
and then use text_transform as seen in #126 to pass it as the function. So with your example (adding stringsAsFactors = FALSE) it would look like
data.frame(A = "Text", B = "<em>Text</em>", stringsAsFactors = FALSE) %>%
gt() %>%
text_transform(
locations = cells_data(vars(B)),
fn = function(x){
html(x, replace_sign_entities = TRUE)
}
)
| A | B |
|---|---|
| Text | Text |
Now, to make it work with multiple rows and columns, you'll have to add map to your function in text_transform since html returns one vector. There's probably a better way to do this but it would look something like this
data.frame(A = c("<b>Text</b>", "<del>Text</del>"), B = c("<em>Text</em>", "Text"), stringsAsFactors = FALSE) %>%
gt() %>%
text_transform(
locations = cells_data(vars(A, B)), # More than one column, row
fn = function(x){
purrr::map(x, html, replace_sign_entities = TRUE) # use map here
}
)
| A | B |
|---|---|
| Text | Text |
|
|
Text |
Created on 2019-01-29 by the reprex package (v0.2.1)
We do escaping to ensure that text is not retained as HTML. I think there is work to be done to make it easier to preserve inputs as HTML but we have to make sure that we do this without compromising security.
This now works with fmt_markdown(). Here's a working example:
dplyr::tibble(A = "Text", B = "<em>Text</em>") %>%
gt() %>%
fmt_markdown(columns = vars(B))
@rich-iannone : can't express enough of my appreciation of your work with gt and blastula; match made in heaven 馃榿馃檹
@leungi Aww thanks! There's still TONS of work to be done. So, thanks for being patient with the development process (and the little problems along the way).
@leungi Aww thanks! There's still TONS of work to be done. So, thanks for being patient with the development process (and the little problems along the way).
Without y'alls' hard work, it would've been a 馃椈for me (with minimal HTML/CSS knowledge 馃槄) to do something like:

Kudos 馃檶
Most helpful comment
This now works with
fmt_markdown(). Here's a working example: