Hao Zhu | 014d621 | 2017-08-07 04:20:23 +0800 | [diff] [blame] | 1 | --- |
| 2 | title: "Use kable in Shiny" |
| 3 | output: html_document |
| 4 | vignette: > |
| 5 | %\VignetteIndexEntry{Use kable in Shiny} |
| 6 | %\VignetteEngine{knitr::rmarkdown} |
| 7 | %\VignetteEncoding{UTF-8} |
| 8 | --- |
| 9 | |
| 10 | ```{r setup, include=FALSE} |
| 11 | knitr::opts_chunk$set(echo = TRUE) |
| 12 | ``` |
| 13 | |
| 14 | Since the output is just HTML, it's very easy to use `kable` and `kableExtra` in the Shiny environment. For example: |
| 15 | ```{r, eval = FALSE} |
| 16 | library(shiny) |
| 17 | |
| 18 | ui <- fluidPage( |
| 19 | |
| 20 | # Application title |
| 21 | titlePanel("mtcars"), |
| 22 | |
| 23 | sidebarLayout( |
| 24 | sidebarPanel( |
| 25 | sliderInput("mpg", "mpg Limit", |
| 26 | min = 11, max = 33, value = 20) |
| 27 | ), |
| 28 | |
| 29 | mainPanel( |
| 30 | tableOutput("mtcars_kable") |
| 31 | ) |
| 32 | ) |
| 33 | ) |
| 34 | |
| 35 | server <- function(input, output) { |
| 36 | library(dplyr) |
| 37 | library(kableExtra) |
| 38 | output$mtcars_kable <- function() { |
| 39 | req(input$mpg) |
| 40 | mtcars %>% |
| 41 | mutate(car = rownames(.)) %>% |
| 42 | select(car, everything()) %>% |
| 43 | filter(mpg <= input$mpg) %>% |
| 44 | knitr::kable("html") %>% |
| 45 | kable_styling("striped", full_width = F) %>% |
| 46 | add_header_above(c(" ", "Group 1" = 5, "Group 2" = 6)) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | # Run the application |
| 51 | shinyApp(ui = ui, server = server) |
| 52 | |
| 53 | |
| 54 | ``` |
| 55 | |
| 56 | You can copy/paste the code above or simply run: |
| 57 | |
| 58 | ```{r, eval=FALSE} |
| 59 | shiny::runGist("https://gist.github.com/haozhu233/9e675e1a8a1bb4744f9ebc9246a2366b") |
| 60 | ``` |