hughjonesd / huxtable

An R package to create styled tables in multiple output formats, with a friendly, modern interface.
http://hughjonesd.github.io/huxtable
Other
322 stars 28 forks source link

A way to remove all theme and css style of tables #241

Open dongweigogo opened 1 year ago

dongweigogo commented 1 year ago

I am currently using huxtable for displaying some table and it is working good. But I found that the css style is always added to huxtable object even when I use theme_basic(). Although the themes in huxtable are nice, I prefer my own ones. Thus, I would like to know if there is a way to remove the buildin themes and css style. The raw HTML code is probably what I need.

hughjonesd commented 1 year ago

Hmm, seems reasonable. I'll look into it.

hughjonesd commented 1 year ago

A workaround right now might be to capture the HTML with to_html and then edit it with tools from another package.

hughjonesd commented 1 year ago

Maybe an argument to to_html with a default options(huxtable.strip_css)would make sense, because probably people will want to do this for many tables in a document.

hughjonesd commented 1 year ago

Hmm. Let me check. Do you really mean all the CSS - heights, colours, everything?

If so, that will remove most of the functionality. So then I think the best thing would be a new function to_basic_html. But I might also think that this is beyond the package scope, and you'd do better to strip the CSS using a different tool.

dongweigogo commented 1 year ago

t. I agree that CSS matters a lot with functionality, actually the buildin css style changes the padding, background color, border width in my case. Any approach to that is OK with me. Thanks.

hughjonesd commented 1 year ago

So, I think the best way to do this is with xml2. Here is an example:

library(huxtable)
library(xml2)
jams
jams_tbl <- jams |> 
  huxtable::as_html() |>
  read_html() |>
  xml_find_first("./table")

xml_set_attr(jams_tbl, "class", "myclass") # modification in place, unusual for R 
tds <- xml_find_all(jams_tbl, ".//td")
xml_set_attr(tds, "style", "") # remove all styles

as.character(jams_tbl)

I'm still open to doing this if there is demand. There may also be better HTML manipulation libraries that I don't know about.