blob: d56859b6131b0a9cb06f868c0361d413df674021 [file] [log] [blame]
Hao Zhuf9aa4c42017-05-22 15:53:35 -04001---
2title: "Use kable in Shiny"
3output: html_document
4---
5
6```{r setup, include=FALSE}
7knitr::opts_chunk$set(echo = TRUE)
8```
9
Hao Zhubd95bb22017-05-22 16:08:49 -040010To use `knitr::kable()` and `kableExtra` in shiny couldn't be simpler.
Hao Zhuf9aa4c42017-05-22 15:53:35 -040011```{r, eval = FALSE}
12library(shiny)
13
14ui <- 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
31server <- 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
47shinyApp(ui = ui, server = server)
48
49
50```
51
52To simply:
53
54```{r, eval=FALSE}
55shiny::runGist("https://gist.github.com/haozhu233/9e675e1a8a1bb4744f9ebc9246a2366b")
56```