blob: c2af6bf3d28e479ee53a7cd3d3eb9f59c7d36992 [file] [log] [blame]
Hao Zhuf9aa4c42017-05-22 15:53:35 -04001---
2title: "Use kable in Shiny"
3output: html_document
Hao Zhu6ce29212017-05-22 16:29:56 -04004vignette: >
5 %\VignetteIndexEntry{Use kable in Shiny}
6 %\VignetteEngine{knitr::rmarkdown}
7 %\VignetteEncoding{UTF-8}
Hao Zhuf9aa4c42017-05-22 15:53:35 -04008---
9
10```{r setup, include=FALSE}
11knitr::opts_chunk$set(echo = TRUE)
12```
13
Hao Zhu25d78f62017-05-23 12:06:51 -040014Since the output is just HTML, it's very easy to use `kable` and `kableExtra` in the Shiny environment. For example:
Hao Zhuf9aa4c42017-05-22 15:53:35 -040015```{r, eval = FALSE}
16library(shiny)
17
18ui <- 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
35server <- 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
51shinyApp(ui = ui, server = server)
52
53
54```
55
Hao Zhu25d78f62017-05-23 12:06:51 -040056You can copy/paste the code above or simply run:
Hao Zhuf9aa4c42017-05-22 15:53:35 -040057
58```{r, eval=FALSE}
59shiny::runGist("https://gist.github.com/haozhu233/9e675e1a8a1bb4744f9ebc9246a2366b")
60```