Save gt table as html w/o extra line return

I have a simple table with a link in it. I am using gt because it does some formatting nicely, which is not shown, because it is not relevant to my question.

I wish to save the gt table as html with no \<html> tag so that it can be inserted into another html page.

Here is the gt_tbl in RStudio's Viewer. Great! enter image description here

And here is the raw_html_tbl as viewed in a web browser saved using either the writeLines method or the cat method as shown in the script.

The HTML table in the browser seems to have added a line return, hence the need for the scroll bar.

How can I save the raw html file so that the scroll bar does not appear and the height of "This is a link" remains one line?

enter image description here

library(gt)
library(tibble)

df <- tibble(
  note = c("This is a <a href = https://www.cnn.com>link</a>.")
)

gt_tbl <- gt(df) |> 
  tab_options(
    table.width = 450
  ) |> 
  fmt_markdown(columns = note)


gt_tbl
raw_html_tbl <- gt_tbl |>
  as_raw_html()

writeLines(text = raw_html_tbl, con = "test/test_tbl.html")
cat(raw_html_tbl, file = "test/test_tbl.html")

I don't know the elegant way to do this, but I brute forced it after understanding why the scrollbar appeared by using web browser inspector.
I chose this which works for me:

raw_html_tbl <- gt_tbl |>
  as_raw_html() |> {function(x){gsub("overflow-x: hidden;",
                                     "",x=x)}}()
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.