2
votes

I'm having a tough time getting ggvis and shiny to play nicely in an Rmarkdown-based application. I'm able to create the following ggvis figure with no problem even without using bind_shiny and ggvisOutput (as seen here):

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

```{r setup, include=FALSE}
require(ggvis)
knitr::opts_chunk$set(echo = TRUE)
```

```{r static}
inputPanel(
  sliderInput('n', 'n:', min = 10, max = 100, value = 50),
  actionButton('run', 'Run!')
)

data = data.frame(x = rnorm(50))

data %>%
  ggvis(~x) %>%
  layer_histograms()
```

However, I'm building dynamic reports to allow users to configure the input data and then re-execute by hitting a 'run' button, like this:

```{r config}
inputPanel(
  sliderInput('n', 'n:', min = 10, max = 100, value = 50),
  actionButton('run', 'Run!')
)

data = eventReactive(input$run, { data = data.frame(x = rnorm(input$n)) })

data %>%
  ggvis(~x) %>%
  layer_histograms()
```

When I try to run the document I get the nondescript error, Quitting from lines 26-36 (test.Rmd). Anyone know how to get this working?

UPDATE:

This almost works, but when I hit 'Run!' the ggvis plot renders in a separate browser window instead of in the document:

```{r config}
inputPanel(
  sliderInput('n', 'n:', min = 10, max = 100, value = 50),
  actionButton('run', 'Run!')
)

data = eventReactive(input$run, { data = data.frame(x = rnorm(input$n)) })

renderTable({summary(data())})

renderPlot({
  data() %>%
    ggvis(~x) %>%
    layer_histograms() %>%
    bind_shiny('plot')

})

ggvisOutput('plot')
```
1
does your data %>% ggvis(~x) %>%... need to infact be dependant on the reactive data(), so you would have data() %>% ggvis... ?tospig
It shouldn't. ggvis can be passed "bare reactives" as shown here: stackoverflow.com/a/25060999/1574941Erin Shellman
ah ok, good to know.tospig
change your renderPlot to reactivetospig

1 Answers

1
votes

The two questions you've linked to show you need the 'ggvis' code inside a reactive({, not a renderPlot({

This now works for me

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

```{r config}
library(ggvis)
inputPanel(
  sliderInput('n', 'n:', min = 10, max = 100, value = 50),
  actionButton('run', 'Run!')
)

data = eventReactive(input$run, { data = data.frame(x = rnorm(input$n)) })

renderTable({summary(data())})

reactive({
  data() %>%
    ggvis(~x) %>%
    layer_histograms() %>%
    bind_shiny('plot')

})

ggvisOutput('plot')
```