Skip to content

Render tables with HTML columns #146

Closed

Description

@TuSKan

Is possible to render HTML content on gt ?

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

Activity

  1. jdbarillas commented on Jan 29, 2019

    @jdbarillas

    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)

  2. rich-iannone commented on Feb 1, 2019

    @rich-iannone
    Member

    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.

  3. self-assigned this
    on Feb 1, 2019
  4. rich-iannone commented on Aug 3, 2019

    @rich-iannone
    Member

    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))
  5. leungi commented on May 12, 2020

    @leungi

    @rich-iannone : can't express enough of my appreciation of your work with gt and blastula; match made in heaven 😁🙏

  6. rich-iannone commented on May 12, 2020

    @rich-iannone
    Member

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

  7. leungi commented on May 12, 2020

    @leungi

    @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 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions