Search code examples
rshinyr-markdownggvis

markdown+shiny+ggvis: graphics updated in new browser window


When using ggvis with shiny+markdown, everytime my graphic is updated, a new browser window is open.

Consider the following MWE:

---
title: "a"
author: "b"
date: "2015"
output: html_document
runtime: shiny
---

Works fine when using base graphics:

```{r,echo=FALSE}
X <- data.frame(t=1:50,x=arima.sim(list(1,0,0),50))
inputPanel(
    sliderInput('p','p',0,2,0,1,TRUE),
    sliderInput('n','n',0,1,0.5,0.1,TRUE)
)
renderPlot({
    plot(X)
    lines(predict(loess(x~t,X,span=input$n,degree=input$p),X$t),col='red')
})
```

When using ggvis, the graphic is updated in a new window!

```{r,echo=FALSE}
library(ggvis)
inputPanel(
    sliderInput('p','p',0,2,0,1,TRUE),
    sliderInput('n','n',0,1,0.5,0.1,TRUE)
)
renderPlot({
    X %>% ggvis(x=~t,y=~x) %>% layer_points() %>%
        layer_model_predictions(stroke:='red',model='loess',formula=x~t,
            model_args=list(span=input$n,degree=input$p))
})
```

I found no updated example where the Shiny variable is accessed explicitly as in this MWE...


Solution

  • Reading through the Properties and scales vignette of ggvis, I now realize that it is easier to use their Shiny wrappers: input_slider instead of sliderInput.

    So, the previous code would become:

    ```{r,echo=FALSE}
    X %>% ggvis(x=~t,y=~x) %>% layer_points() %>%
            layer_model_predictions(stroke='red',model='loess',formula=x~t,
                model_args=list(
                    span=input_slider(0,2,1,1,TRUE),
                    degree=input_slider(0,1,0.01))
    ```
    

    I could have used Shiny directly, but apparently, I would have to tell ggvis about shiny using bind_shiny, and tell shiny about ggvis using ggvisOutput.