Hao Zhu | f9aa4c4 | 2017-05-22 15:53:35 -0400 | [diff] [blame] | 1 | --- |
| 2 | title: "Use kable in Shiny" |
| 3 | output: html_document |
| 4 | --- |
| 5 | |
| 6 | ```{r setup, include=FALSE} |
| 7 | knitr::opts_chunk$set(echo = TRUE) |
| 8 | ``` |
| 9 | |
Hao Zhu | bd95bb2 | 2017-05-22 16:08:49 -0400 | [diff] [blame] | 10 | To use `knitr::kable()` and `kableExtra` in shiny couldn't be simpler. |
Hao Zhu | f9aa4c4 | 2017-05-22 15:53:35 -0400 | [diff] [blame] | 11 | ```{r, eval = FALSE} |
| 12 | library(shiny) |
| 13 | |
| 14 | ui <- fluidPage( |
| 15 | |
| 16 | # Application title |
| 17 | titlePanel("mtcars"), |
| 18 | |
| 19 | sidebarLayout( |
| 20 | sidebarPanel( |
| 21 | sliderInput("mpg", "mpg Limit", |
| 22 | min = 11, max = 33, value = 20) |
| 23 | ), |
| 24 | |
| 25 | mainPanel( |
| 26 | tableOutput("mtcars_kable") |
| 27 | ) |
| 28 | ) |
| 29 | ) |
| 30 | |
| 31 | server <- function(input, output) { |
| 32 | library(dplyr) |
| 33 | library(kableExtra) |
| 34 | output$mtcars_kable <- function() { |
| 35 | req(input$mpg) |
| 36 | mtcars %>% |
| 37 | mutate(car = rownames(.)) %>% |
| 38 | select(car, everything()) %>% |
| 39 | filter(mpg <= input$mpg) %>% |
| 40 | knitr::kable("html") %>% |
| 41 | kable_styling("striped", full_width = F) %>% |
| 42 | add_header_above(c(" ", "Group 1" = 5, "Group 2" = 6)) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | # Run the application |
| 47 | shinyApp(ui = ui, server = server) |
| 48 | |
| 49 | |
| 50 | ``` |
| 51 | |
| 52 | To simply: |
| 53 | |
| 54 | ```{r, eval=FALSE} |
| 55 | shiny::runGist("https://gist.github.com/haozhu233/9e675e1a8a1bb4744f9ebc9246a2366b") |
| 56 | ``` |