---
title: "Example: Grand tour Widget"
runtime: shiny
output: html_document
---

This widget conducts a grand tour of a multi-dimensional data set.  
The widget embeds a *ggvis* visualization.  
The author selects the data set to explore when he or she writes the app, but the author _does not need to write the code that makes the visualization._

```{r echo=FALSE, message=FALSE}
library(shiny)
library(ggvis)
library(tourr)

gtour <- function(data, groups) {
  aps <- 2
  fps <- 30
  mat <- rescale(as.matrix(data))
  tour <- new_tour(mat, grand_tour(), NULL)
  start <- tour(0)

  proj_data <- reactive({
    invalidateLater(1000 / fps, NULL);
    step <- tour(aps / fps)
    data.frame(center(mat %*% step$proj), groups = groups)
  })

  proj_data %>% ggvis(~X1, ~X2, fill = ~groups) %>%
    layer_points() %>%
    scale_numeric("x", domain = c(-1, 1)) %>%
    scale_numeric("y", domain = c(-1, 1)) %>%
    add_axis("x", title = "") %>% add_axis("y", title = "") %>%
    set_options(duration = 0)
}
```

```{r echo=FALSE}
# gtour(flea[1:6], flea[[7]])
# gtour(olive[3:10], olive[[1]])
gtour(ratcns[1:9], ratcns[[10]])
```