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
|
---
title: "Brushing"
output: html_document
runtime: shiny
---
```{r, message = FALSE, echo = FALSE}
library(ggvis)
library(shiny)
set.seed(1233)
cocaine <- cocaine[sample(1:nrow(cocaine), 500), ]
```
Two linked plots. Brushing in the scatter plot will update the histogram.
```{r fig.width = 4, fig.height = 3, echo = FALSE, results = "hold"}
cocaine$id <- seq_len(nrow(cocaine))
lb <- linked_brush(keys = cocaine$id, "red")
cocaine %>%
ggvis(~weight, ~price, key := ~id) %>%
layer_points(fill := lb$fill, fill.brush := "red", opacity := 0.3) %>%
lb$input()
# A subset of cocaine, of only the selected points
selected <- lb$selected
cocaine_selected <- reactive({
cocaine[selected(), ]
})
cocaine %>%
ggvis(~potency) %>%
layer_histograms(width = 5, boundary = 0) %>%
add_data(cocaine_selected) %>%
layer_histograms(width = 5, boundary = 0, fill := "#dd3333")
```
A summary of the selected points:
```{r, echo = FALSE}
renderPrint(
summary(cocaine_selected())
)
```
|