formattable as an htmlwidget

痛定思痛。 2022-09-21 13:09 99阅读 0赞

Kent Russell

2015-06-12

formattable was originally designed to offer additional formatting to the markdowngenerated by the deliberately sparse knitr::kable. This design limited formattable to only the context of an Rmarkdown document. Other contexts, such as the R console or RStudio IDE, would only see the much less attractive character markdown output.

  1. library(formattable)
  2. as.character(formattable(head(mtcars,3)))
  3. ## [1] "| | mpg| cyl| disp| hp| drat| wt| qsec| vs| am| gear| carb|"
  4. ## [2] "|:-------------|----:|---:|----:|---:|----:|-----:|-----:|--:|--:|----:|----:|"
  5. ## [3] "|Mazda RX4 | 21.0| 6| 160| 110| 3.90| 2.620| 16.46| 0| 1| 4| 4|"
  6. ## [4] "|Mazda RX4 Wag | 21.0| 6| 160| 110| 3.90| 2.875| 17.02| 0| 1| 4| 4|"
  7. ## [5] "|Datsun 710 | 22.8| 4| 108| 93| 3.85| 2.320| 18.61| 1| 1| 4| 1|"

Fortunately, a new function as.htmlwidget uses markdown to easily convert aformattable object to an htmlwidget. Once converted to an htmlwidget, a user in these other contexts can leverage the infrastructure of htmlwidgets to benefit from the finalHTML output of formattable.

Demonstration of conversion

note, please run these in an ?interactive environment such as the console or RStudio IDE to see the difference

Let’s explicitly convert a formattable to an htmlwidget to help us understand what is happening.

  1. as.htmlwidget( formattable( head(mtcars,3) ) )





























































  mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1

Although our htmlwidget version looks virtually the same when generated from a Rmddocument such as this vignette, a user in the console will have a noticeably different experience than before. The function interactive() can help formattable to guess when it should automatically convert to a htmlwidget. formattable will not auto-convert in the special situation where format = "pandoc" as shown below.

  1. formattable( head(mtcars,3), format = "pandoc" )
  2. ##
  3. ##
  4. ## mpg cyl disp hp drat wt qsec vs am gear carb
  5. ## -------------- ----- ---- ----- ---- ----- ------ ------ --- --- ----- -----
  6. ## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
  7. ## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
  8. ## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1

If you prefer to avoid the automatic conversion to an htmlwidget, useas.character(formattable(...)).

As an additional example, we can recreate the example from Tristan Mahr(@tjmahr)with formattable.

  1. ## use formattable to recreate the example in
  2. ## https://rpubs.com/tjmahr/prettytables_2015
  3. library("magrittr")
  4. library("dplyr")
  5. ## Warning: package 'dplyr' was built under R version 3.1.3
  6. library("broom")
  7. library("stringr")
  8. library("knitr")
  9. fix_names <- . %>%
  10. str_replace(".Intercept.", "Intercept") %>%
  11. str_replace("Species", "") %>%
  12. # Capitalize species names
  13. str_replace("setosa", "Setosa") %>%
  14. str_replace("versicolor", "Versicolor") %>%
  15. str_replace("virginica", "Virginica") %>%
  16. # Clean up special characters
  17. str_replace_all(".Width", " Width") %>%
  18. str_replace_all(".Length", " Length") %>%
  19. str_replace_all(":", " x ")
  20. # Print with n digits of precision
  21. fixed_digits <- function(xs, n = 2) {
  22. formatC(xs, digits = n, format = "f")
  23. }
  24. # Don't print leading zero on bounded numbers.
  25. remove_leading_zero <- function(xs) {
  26. # Problem if any value is greater than 1.0
  27. digit_matters <- xs %>% as.numeric %>%
  28. abs %>% is_greater_than(1)
  29. if (any(digit_matters)) {
  30. warning("Non-zero leading digit")
  31. }
  32. str_replace(xs, "^(-?)0", "\\1")
  33. }
  34. lm(Sepal.Length ~ Species * Sepal.Width, iris) %>%
  35. tidy %>%
  36. set_colnames( c("Param", "Estimate", "SE", "_t_", "_p_") ) %>%
  37. mutate( Param = fix_names( Param ) ) %>%
  38. formattable(
  39. list(
  40. "_p_" = formatter(
  41. "span"
  42. ,style = x ~ ifelse( x < 0.05, style( color = "red", font.weight = "bold" ), NA )
  43. ,ps ~ {
  44. tiny <- "< .001"
  45. ps_chr <- ps %>% fixed_digits(3) %>%
  46. remove_leading_zero
  47. ps_chr[ps < 0.001] <- tiny
  48. ps_chr
  49. }
  50. )
  51. )
  52. ,digits=2
  53. )






















































Param Estimate SE t p
Intercept 2.64 0.57 4.62 < .001
Versicolor 0.90 0.80 1.13 .261
Virginica 1.27 0.82 1.55 .123
Sepal Width 0.69 0.17 4.17 < .001
Versicolor x Sepal Width 0.17 0.26 0.67 .503
Virginica x Sepal Width 0.21 0.26 0.83 .411

htmlwidgets inside a formattable

Courtesy of this issue we have an interesting example of interactive sparklinehtmlwidgets inside a formattable. ** note: only works in Rmd currently **

  1. # use builtin chickwts ?chickwts
  2. library(dplyr)
  3. library(formattable)
  4. # devtools::install_github( "htmlwidgets/sparkline" )
  5. library(sparkline)
  6. chickwts %>%
  7. group_by( feed ) %>%
  8. summarise(
  9. weight = sprintf("`r sparkline(c(%s), type = 'box')`", paste0(weight, collapse=","))
  10. ) %>%
  11. ungroup %>%
  12. as.data.frame %>%
  13. formattable(
  14. list(
  15. weight = function(spkline){
  16. sapply(spkline, function(md) knitr::knit(text=md, quiet=T) )
  17. }
  18. )
  19. )

































feed weight
casein
horsebean
linseed
meatmeal
soybean
sunflower

发表评论

表情:
评论列表 (有 0 条评论,99人围观)

还没有评论,来说两句吧...

相关阅读