ggvis+shiny issue

404 views
Skip to first unread message

Karsten Hansen

unread,
Jul 5, 2014, 12:43:02 PM7/5/14
to gg...@googlegroups.com
Trying to learn the tricks of getting the Shiny+ggvis combo to do what I want. I think I have read through every single post on this and I'm still stumped on why I can't get the following example to work. So now I must appeal to the gurus :) 

Here is the example: 

library(shiny)
library(ggvis)

shinyUI(fluidPage(

  # Application title
  titlePanel("Shiny/ggvis Test"),

  sidebarLayout(
    sidebarPanel(
      
      selectInput("x.var", "X:",choices=names(mtcars),selected='mph'),
      selectInput("y.var", "Y:",choices=names(mtcars),selected='hp')
      
    ),
    mainPanel(
      ggvisOutput("theplot")
    )
  )
))

shinyServer(function(input, output, session) {
  
  selectedData <- reactive({
    mtcars[, c(input$x.var, input$y.var)]
  })
  
  varNames <- reactive({
    names(selectedData())
  })
  
  selectedData() %>% 
    ggvis(x = as.name(varNames()[1]), y = as.name(varNames()[2])) %>%    
    layer_points() %>%
    add_tooltip(all_values,"hover") %>%
    bind_shiny("theplot")
  
  all_values <- function(x){
    if(is.null(x)) return(NULL)
    paste0(names(x), ": ", format(x), collapse = "<br />")
  }
  
})



This gives me the error: 

Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

So somehow I must be accessing something that should be in a reactive, right? But I don't know what. 

Session Info: ggvis 0.3.0.1, shiny 0.10.0, R 3.1.0

- Karsten


Ian Lyttle

unread,
Jul 5, 2014, 2:02:00 PM7/5/14
to gg...@googlegroups.com
Hi Karsten,

I got this to almost work - if you don't care about axes labels, it works completely.

I've been twiddling with this myself - a few points:

I have discovered that ggvis takes "naked" reactives. I'm sure Winston and Hadley will have a better way to describe it, but ggvis, does not take the data that the reactive provides, instead takes the name of the reactive that provides it. Hence, the parentheses are removed from selectedData.

I made reactives that provide the names of the variables as "names"  (you were on the right track). Apparently, I have to tell ggvis to construct a scale (I don't know why, but it seems to work).

Here's the remaining issue: Vega does not let ggvis update the name of an axis label. (https://groups.google.com/forum/#!topic/ggvis/ZJXdIFAmb68) Winston can let me know if I have misinterpreted this.

Server code below.

Hope this helps.

Ian



shinyServer(function(input, output, session) {
  
  selectedData <- reactive({
    mtcars[, c(input$x.var, input$y.var)]
  })
  
  rct_name_x <- reactive({
    as.name(input$x.var) 
  })

  rct_name_y <- reactive({
    as.name(input$y.var) 
  })
  
  selectedData %>% 
    ggvis(
      prop("x", rct_name_x, scale = TRUE), 
      prop("y", rct_name_y, scale = TRUE)
    ) %>%    
    layer_points() %>%
    add_tooltip(all_values, "hover") %>%
    bind_shiny("theplot")
  
  all_values <- function(x){
    if(is.null(x)) return(NULL)
    paste0(names(x), ": ", format(x), collapse = "<br />")
  }
  
})

Ian Lyttle

unread,
Jul 5, 2014, 2:04:38 PM7/5/14
to gg...@googlegroups.com
Badly placed comma, sentence should read: 

I'm sure Winston and Hadley will have a better way to describe it, but ggvis does not take...

Ian

Karsten Hansen

unread,
Jul 5, 2014, 2:34:16 PM7/5/14
to gg...@googlegroups.com
Ian,
thanks - this is super helpful. We are 98% there - just need to find a way to label the axes. I will work on this up until the start of the Netherlands-Costa Rica game. I will post if I find a way to do this.
-Karsten

Ian Lyttle

unread,
Jul 5, 2014, 3:14:50 PM7/5/14
to gg...@googlegroups.com
Hi Karsten,

Cool! 

I'll post, as well if I find a way.

Ian

Ian Lyttle

unread,
Jul 8, 2014, 10:47:40 AM7/8/14
to gg...@googlegroups.com
Hi Karsten,

Another possible solution here: wrap the entire ggvis call in a reactive (again, full shiny):  https://github.com/wch/movies

I understand this can incur some performance issues; Winston will have the definitive answer.

Ian

Winston Chang

unread,
Jul 8, 2014, 11:24:21 AM7/8/14
to Ian Lyttle, gg...@googlegroups.com
That's right - ggvis can take "bare" reactives as data sources; when the data changes, the updated data is processed and sent to the browser for rendering. 

You can also put the whole ggvis call in a reactive() block, but this behaves a bit differently: instead of simply updating the data, the entire plot spec (a JSON description of the plot) is sent to the browser and the plot is rebuilt from scratch. This will take a bit more time than simply updating the data, and it will also result in a blanking out of the plotting area while it's re-rendered.

At the moment the data in a plot can be changed without sending a new spec, but other parts can't. For example, the plot title and axis titles can't be changed simply by updating the data.

-Winston




--
You received this message because you are subscribed to the Google Groups "ggvis" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggvis+un...@googlegroups.com.
To post to this group, send email to gg...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Karsten Hansen

unread,
Jul 9, 2014, 3:30:33 PM7/9/14
to gg...@googlegroups.com
hmm..that's a clever solution. Will have to tinker with this a bit.Ian - thanks again.

Tuija Sonkkila

unread,
Jul 10, 2014, 3:43:54 AM7/10/14
to gg...@googlegroups.com
Hi,

Thanks for this thread. I've been working with a similar example, and you solved my problem. If you are interested, here's the Shiny app live https://ttso.shinyapps.io/AaltoAltmSince2007/ and the code with some sample data https://gist.github.com/tts/31d9173cd5733079126e

A small cosmetic issue is that with only one selected item, i.e. when the dataframe has one row, the ggvis doesn't look quite right. 

-Tuija
 
Reply all
Reply to author
Forward
0 new messages