1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
---
title: "Use kable in Shiny"
output: html_document
vignette: >
%\VignetteIndexEntry{Use kable in Shiny}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Since the output is just HTML, it's very easy to use `kable` and `kableExtra` in the Shiny environment. For example:
```{r, eval = FALSE}
library(shiny)
ui <- fluidPage(
# Application title
titlePanel("mtcars"),
sidebarLayout(
sidebarPanel(
sliderInput("mpg", "mpg Limit",
min = 11, max = 33, value = 20)
),
mainPanel(
tableOutput("mtcars_kable")
)
)
)
server <- function(input, output) {
library(dplyr)
library(kableExtra)
output$mtcars_kable <- function() {
req(input$mpg)
mtcars %>%
mutate(car = rownames(.)) %>%
select(car, everything()) %>%
filter(mpg <= input$mpg) %>%
knitr::kable("html") %>%
kable_styling("striped", full_width = F) %>%
add_header_above(c(" ", "Group 1" = 5, "Group 2" = 6))
}
}
# Run the application
shinyApp(ui = ui, server = server)
```
You can copy/paste the code above or simply run:
```{r, eval=FALSE}
shiny::runGist("https://gist.github.com/haozhu233/9e675e1a8a1bb4744f9ebc9246a2366b")
```
|