iboboboru
iboboboru

Reputation: 1102

Is it possible to create two ggivs charts from one reactive data source?

For example

dataReactive<-reactive({data[, c(input$selectCol)]})


dataReactive %>%
    ggvis(x=~x,y=~y) %>%
    layer_points() %>%
    bind_shiny("plot1")

dataReactive %>%
    ggvis(x=~x) %>%
    layer_histograms(width=1) %>%
    bind_shiny("plot2")

When you try to output,in the UI, it will only produce the first plot bound with bind_shiny? in the server. Is this a bug or is there a work around. I believe it might have something to do with how reactive is updating.

Upvotes: 0

Views: 116

Answers (1)

nrussell
nrussell

Reputation: 18602

Yes this is possible, and you should post your complete code otherwise there is no way to determine what is causing the issue for you. Here's a very simple example:


ui.R

library(shiny)
library(ggvis)
##
shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(
        inputId="selectCol",
        label="y-variable",
        choices=(names(data))
      )
    ),
    mainPanel(
        ggvisOutput("plot1"),
        br(),
        ggvisOutput("plot2")   
    )
  )
))

server.R

library(shiny)
library(ggvis)
##
shinyServer(function(input, output) {
  cols <- names(data)
  colIdx <- reactive({ 
    match(c("x",input$selectCol),cols)
  })

  dataReactive <-reactive({
    df <- data[, colIdx()]
    names(df) <- c("x","y")
    df
  })

  dataReactive %>%
    ggvis(x=~x,y=~y) %>%
    layer_points() %>%
    bind_shiny("plot1")

  dataReactive %>%
    ggvis(x=~x) %>%
    layer_histograms(width=1) %>%
    bind_shiny("plot2")
})

global.R

library(shiny)
library(ggvis)
##
set.seed(123)
data <- data.frame(
  x=sample(1:50,40,replace=TRUE),
  col2=rnorm(40,1,5),
  col3=rexp(40,3))

And here is what the application looks like when running through the external browser: enter image description here

Upvotes: 1

Related Questions