Hao Zhu | f9aa4c4 | 2017-05-22 15:53:35 -0400 | [diff] [blame] | 1 | --- |
| 2 | title: "Use kable in Shiny" |
| 3 | output: html_document |
Hao Zhu | 6ce2921 | 2017-05-22 16:29:56 -0400 | [diff] [blame^] | 4 | vignette: > |
| 5 | %\VignetteIndexEntry{Use kable in Shiny} |
| 6 | %\VignetteEngine{knitr::rmarkdown} |
| 7 | %\VignetteEncoding{UTF-8} |
Hao Zhu | f9aa4c4 | 2017-05-22 15:53:35 -0400 | [diff] [blame] | 8 | --- |
| 9 | |
| 10 | ```{r setup, include=FALSE} |
| 11 | knitr::opts_chunk$set(echo = TRUE) |
| 12 | ``` |
| 13 | |
Hao Zhu | bd95bb2 | 2017-05-22 16:08:49 -0400 | [diff] [blame] | 14 | To use `knitr::kable()` and `kableExtra` in shiny couldn't be simpler. |
Hao Zhu | f9aa4c4 | 2017-05-22 15:53:35 -0400 | [diff] [blame] | 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 | To simply: |
| 57 | |
| 58 | ```{r, eval=FALSE} |
| 59 | shiny::runGist("https://gist.github.com/haozhu233/9e675e1a8a1bb4744f9ebc9246a2366b") |
| 60 | ``` |