Reputation: 63
UPDATE: KableExtra creator haozhu233 added mathjax support to the package. See my original issue and his comments on Github: https://github.com/haozhu233/kableExtra/issues/473#issuecomment-668224096
When I try and create a table using kable and kableExtra I cannot get math symbols to display correctly. I am able to produce math characters in normal kable tables like expected. I've tried different variations of the options escape = FALSE and protect_latex = T without any luck. The math symbols display correctly if I knit to PDF or HTML, it's just the inline display (and preview) and notebook display that show the raw characters.
As far as I can see from the HTML output, the only difference between the kable table and the kableExtra table is that the latter contains some extra info on the class and style. The R objects are also of a different class, which makes me wonder if the problem might be that mathjax is not called to render kableExtra objects? If so, is there a way I can tell Rstudio to call on mathjax when rendering HTML from kableExtra objects?
I've reproduced this problem on two different computers and using the RStudio Cloud.
Note: i filed a Github Issue but realized that it might be more appropriate to ask here. I've also posted on the Rstudio community without getting a response so I thought I'd try my luck here.
---
title: "Kable Extra Math Symbols"
output: html_notebook
---
```{r}
math_symb <- c(1,2,3,4)
kable_table <- knitr::kable(math_symb, col.names = "$R^{2}$")
styled_kable_table <- kableExtra::kable_styling(kable_table)
kable_table
styled_kable_table
```{}
From the example the object kable_table
is rendered with the column name "R2", which is what I want to achieve whereas the styled_kable_table
object is displayed with the the undesired column name "$R^2$"
Session Info:
R version 3.6.3 (2020-02-29) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale: 2 LC_COLLATE=Norwegian Bokmål_Norway.1252 LC_CTYPE=Norwegian Bokmål_Norway.1252 [3] LC_MONETARY=Norwegian Bokmål_Norway.1252 LC_NUMERIC=C [5] LC_TIME=Norwegian Bokmål_Norway.1252
attached base packages: 2 stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached): 2 Rcpp_1.0.4.6 rstudioapi_0.11 xml2_1.3.2 knitr_1.28 magrittr_1.5 hms_0.5.3 [7] munsell_0.5.0 rvest_0.3.5 viridisLite_0.3.0 colorspace_1.4-1 R6_2.4.1 rlang_0.4.6 [13] stringr_1.4.0 httr_1.4.1 highr_0.8 tools_3.6.3 webshot_0.5.2 xfun_0.14 [19] htmltools_0.4.0 ellipsis_0.3.0 yaml_2.2.1 digest_0.6.25 tibble_3.0.1 lifecycle_0.2.0 [25] crayon_1.3.4 kableExtra_1.1.0 readr_1.3.1 vctrs_0.3.0 glue_1.4.1 evaluate_0.14 [31] rmarkdown_2.1 stringi_1.4.6 compiler_3.6.3 pillar_1.4.4 scales_1.1.1 pkgconfig_2.0.3
Upvotes: 2
Views: 1165
Reputation: 1224
This works:
```{r, results ='asis'}
math_symb <- c(1,2,3,4)
kable_table <- knitr::kable(math_symb, col.names = "$R^{2}$")
styled_kable_table <- kableExtra::kable_styling(kable_table)
kable_table
cat(styled_kable_table)
```
However, I have to investigate, why it works.
Upvotes: 2
Reputation: 12461
You're producing an HTML document, so you need to format your header using HTML:
math_symb <- c(1,2,3,4)
math_symb %>% kable(col.names = "R<sup>2</sup>")
Gives
Upvotes: 0