Connect a ggvis graphic to a shiny app.

Share:

Description

Embedding ggvis in a shiny app is easy. You need to make a place for it in your ui.r with ggvisOutput, and tell your server.r where to draw it with bind_shiny. It's easiest to learn by example: there are many shiny apps in demo/apps/ that you can learn from.

Usage

1
2
3
4
5
6
bind_shiny(vis, plot_id, controls_id = NULL, ...,
  session = shiny::getDefaultReactiveDomain())

bind_shiny_ui(vis, controls_id, session = shiny::getDefaultReactiveDomain())

ggvisOutput(plot_id = rand_id("plot_id"))

Arguments

vis

A ggvis object, or a reactive expression that returns a ggvis object.

plot_id

unique identifier to use for the div containing the ggvis plot.

controls_id

Unique identifier for controls div.

...

Other arguments passed to as.vega.

session

A Shiny session object.

Client-side

In your UI, use ggvisOutput() in ui.r to insert an html placeholder for the plot.

If you're going to be using interactive controls generated by ggvis, use renderUI() to add a place holder. By convention, if the id of plot placehold is called "plot", call the controls placeholder "plot_ui".

Server-side

When you run ggvis plot interactively, it is automatically plotted because it triggers the default print method. In shiny apps, you need to explicitly render the plot to a specific placeholder with bind_shiny:

p %>% bind_shiny("plot")

If the plot has controls, and you've reserved space for them in the UI, supply the name of the placeholder as the third argument:

p %>% bind_shiny("plot", "plot_ui")

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
## Run these examples only in interactive R sessions
if (interactive()) {

# Simplest possible app:
library(shiny)
runApp(list(
  ui = bootstrapPage(
    ggvisOutput("p"),
    uiOutput("p_ui")
  ),
  server = function(..., session) {
    mtcars %>%
      ggvis(~wt, ~mpg) %>%
      layer_points() %>%
      layer_smooths(span = input_slider(0, 1)) %>%
      bind_shiny("p", "p_ui")
  }
))

}

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.