Gt: Render tables with HTML columns

Created on 22 Jan 2019  路  6Comments  路  Source: rstudio/gt

Is possible to render HTML content on gt ?

data.frame(A = "Text", B = html("<em>Text</em>")) %>% gt()

AB
AAAA<em>HTML</em>

[2] Intermediate [2] Medium [2] Medium

Most helpful comment

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))

All 6 comments

I had the same question, the problem seems to be that the < and > signs get converted to their HTML entities &lt; and &gt;, 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("&lt;" = "<", "&gt;" = ">"))
  }

  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 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:

image

Kudos 馃檶

Was this page helpful?
0 / 5 - 0 ratings

Related issues

r2evans picture r2evans  路  5Comments

scotbader8858 picture scotbader8858  路  4Comments

jenniferp96 picture jenniferp96  路  4Comments

DickStartz picture DickStartz  路  4Comments

vincentarelbundock picture vincentarelbundock  路  4Comments