Search code examples
rshinyggvis

Display a ggvis plot in a shiny document


Is ggvis working with Shiny Documents yet?

In this example, ggplot is visible but ggvis is not

---
title: "testShiny"
runtime: shiny
output: html_document
---

ggplot2

```{r, echo=FALSE}

require(ggplot2)

renderPlot({
  ggplot(women, aes(height, weight))+
    geom_point()+
    theme_bw()

  })

```

ggvis

```{r, echo=FALSE}

require(ggvis)

renderPlot({
  women %>%
    ggvis(x= ~height, y = ~weight) %>%
    layer_points()

  })

```

While searching I came across bind_shiny, but it didn't solve the problem


Solution

  • You need to use bind_shiny to assign an id to the visualisation. You then need to use ggvisOutput to create an element in the DOM to display the visualisation:

    ---
    title: "testShiny"
    runtime: shiny
    output: html_document
    ---
    
    ```{r, echo=FALSE}
    
    require(ggvis)
    require(knitr)
    require(shiny)
    
    ggvisOutput("p")
    
    women %>%
      ggvis(x= ~height, y = ~weight) %>%
      layer_points()%>%
      bind_shiny("p")
    
    ```
    

    enter image description here