Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a Tibble to HTML Table in R tidyverse?

I'm wanting a way to convert the results of a pipeline manipulation into a table so it can be rendered as a HTML table in R Markdown.

Sample data:

Category <- sample(1:6, 394400) 
Category <- sample(1:6, 394400, replace=TRUE)
Category <- factor(Category,
                    levels = c(1,2,3,4,5,6),
                    labels = c("First",
                               "Second",
                               "Third",
                               "Fourth",
                               "Fifth",
                               "Sixth"))
data <- data.frame(Category)

Then I build a frequency table using the pipeline:

Table <- data %>%
             group_by(Category) %>%
             summarise(N= n(), Percent = n()/NROW(data)*100) %>%
             mutate(C.Percent = cumsum(Percent))

Which gives me this nice little summary table here:

# A tibble: 6 × 4
  Category     N  Percent C.Percent
    <fctr> <int>    <dbl>     <dbl>
1    First 65853 16.69701  16.69701
2   Second 66208 16.78702  33.48403
3    Third 65730 16.66582  50.14985
4   Fourth 65480 16.60243  66.75228
5    Fifth 65674 16.65162  83.40390
6    Sixth 65455 16.59610 100.00000

However if I try to convert that to a table to then convert to HTML, it tells me it cannot coerce Table to a table. This is the same with data frames as well.

Does anyone know a way, as I'd quite like to customise the appearance of the output?

like image 567
Nick Avatar asked Sep 03 '25 14:09

Nick


1 Answers

There are several packages for that. Here are some:

knitr::kable(Table)

htmlTable::htmlTable(Table)

ztable::ztable(as.data.frame(Table))

DT::datatable(Table)

stargazer::stargazer(Table, type = "html")

Each of these has different customization options.

like image 124
yeedle Avatar answered Sep 05 '25 06:09

yeedle