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 | |
| 10 | ```{r, eval = FALSE} |
| 11 | library(shiny) |
| 12 | |
| 13 | ui <- fluidPage( |
| 14 | |
| 15 | # Application title |
| 16 | titlePanel("mtcars"), |
| 17 | |
| 18 | sidebarLayout( |
| 19 | sidebarPanel( |
| 20 | sliderInput("mpg", "mpg Limit", |
| 21 | min = 11, max = 33, value = 20) |
| 22 | ), |
| 23 | |
| 24 | mainPanel( |
| 25 | tableOutput("mtcars_kable") |
| 26 | ) |
| 27 | ) |
| 28 | ) |
| 29 | |
| 30 | server <- function(input, output) { |
| 31 | library(dplyr) |
| 32 | library(kableExtra) |
| 33 | output$mtcars_kable <- function() { |
| 34 | req(input$mpg) |
| 35 | mtcars %>% |
| 36 | mutate(car = rownames(.)) %>% |
| 37 | select(car, everything()) %>% |
| 38 | filter(mpg <= input$mpg) %>% |
| 39 | knitr::kable("html") %>% |
| 40 | kable_styling("striped", full_width = F) %>% |
| 41 | add_header_above(c(" ", "Group 1" = 5, "Group 2" = 6)) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | # Run the application |
| 46 | shinyApp(ui = ui, server = server) |
| 47 | |
| 48 | |
| 49 | ``` |
| 50 | |
| 51 | To simply: |
| 52 | |
| 53 | ```{r, eval=FALSE} |
| 54 | shiny::runGist("https://gist.github.com/haozhu233/9e675e1a8a1bb4744f9ebc9246a2366b") |
| 55 | ``` |