[ solved ] RStudio IDE Viewer is not rendering the styled HTML output from ztable package

Hi everyone,
I am trying to reproduce this tutorial about cross tabulation using ztable package:

It should reproduce a table like this one I did using my macbook:

However, when I replicated the same code using a Windows notebook, it just shows me a bunch of html code. I tried on both console and .RMD file, none of then worked.

The same is happening with xtable package:

Does anyone knows why this is happening just with rstudio on Windows? I have both RStudio and Windows updated.

Thank you!

# code from M.Devlin tutorial described above
[Tables and Plots of Counts and Proportions](https://rstudio-pubs-static.s3.amazonaws.com/80278_2a7b4c5561394b04ad3f135d89421919.html)

library(tidyverse)
library(ztable) #format tables for reporting

dfr <- mtcars %>%             
  mutate(cyl = as.factor(cyl)     
         , gear = as.ordered(gear))

dfr_prop <- dfr %>% 
  count(cyl, gear) %>%            
  mutate(prop = prop.table(n)) 

as.data.frame(dfr_prop)   

dfr_perc <- dfr %>% 
  count(cyl, gear) %>% 
  mutate(perc = prop.table(n)*100) %>%   
  select(-n) %>%                         
  spread(gear, perc)     

dfr_dist <- dfr %>% 
  count(cyl) %>%                         
  mutate(`(\\%)` = prop.table(n)*100) %>%
  left_join(dfr_perc, by = 'cyl')   

### 1 digit to distinguish % from n and caption
ztab <- ztable(as.data.frame(dfr_dist), digits = 1
               , caption = 'Distribution of Gears by Cylinders')

### name and number column groups
ztab <- addcgroup(ztab, cgroup = c('Cylinders', 'Gear Distribution (\\%)') 
                  , n.cgroup = c(3, ncol(dfr_dist)-3))

### when I try this on Windows it just shows a bunch of html code
ztab

I don't know what's wrong, but you might get an answer more quickly if you:

2 Likes

Hi jcblum,

Thank you for the feedback.

Fun fact is that I was not sure on how to describe my problem. After your comment I might finding something on stackoverflow...

Thank you!

:smile: Sometimes half the problem is figuring out the right search term! The auto-rendering that the RStudio IDE does is magically helpful when all goes well, but can sure be confusing when you run into trouble.

If you do find an answer on SO, you should link to it here for everyone's benefit! And if you wind up posting a new question on SO, definitely say so here (to abide by this community's cross-site posting policy).

Good luck!

2 Likes

[ solution for ztable package and xtable package on RStudio IDE Viewer ]

Searching for related terms on SO I found this topic: HTML Table not Appearing in RStudio Viewer but Only as Code in the Console - Why?

I just needed to install and load htmltools package and voilà, it is now perfectly rendering tables from ztable :grinning:

install.packages("htmltools")
library(htmltools)
ztab

However, this is not the final solution since xtable package still not rendering tables properly. I tried the code below and returned me an error:

htmlTable(xtable(dfr_prop))
Error in htmlTable(dfr_prop, useViewer = TRUE) : 
  could not find function "htmlTable"

After I installed htmlTable package I was able to print xtable's prackage tables:

install.packages("htmlTable")
library(htmlTable)
table <- xtable(dfr_prop)
htmlTable(table)

[ solution for ztable package on a .RMD file ]

Finally, I tried on a .RMD file and rendering still not working for ztable. So I wrapped up the code with print() and now the table looks perfect.

before: ztab
after: print(ztab)

Thank you again for the feedback. It was really helpful :wink:

1 Like