3

Simple example of a Shiny app using ggvis. Trying to use a pulldown to filter a variable. So here I'm trying to filter by mtcars' gear (either 3, 4, or 5), then plotting x and y of mpg and hp for each of the unique values of gear.

I get the initial plot drawn with a default of '3' selected, but if I change the value via the pulldown nothing happens. I think I know where things are going wrong (commented in the code), but I've tried just about everything I can think of and have no idea what the actual mistake I'm making is.

Thanks

ui.R

# ui.R

library(shiny)

shinyUI(fluidPage(
  titlePanel("Car Thing"),

  sidebarLayout(
    sidebarPanel(

        uiOutput("choose_gear")                     
    ),

    mainPanel(
       ggvisOutput("ggvis")         
    )
  )
))

server.R

#server.R

library(shiny)
library(ggvis)
library(dplyr)

gear_nos <- sort(unique(mtcars$gear))

    shinyServer(function(input, output, session) {

        output$choose_gear <- renderUI({        
            selectInput("gears", "Choose Gear", gear_nos, selected="3")                                 
        })              

        # I'm pretty sure this is where I'm messing something up
        pickedGear <- reactive({
            mtcars %>% filter(gear == input$gears)
        })

        if(is.null(dim(pickedGear))){
            pickedGear <- mtcars[mtcars$gear == 3,]
        }

        pickedGear %>% ggvis(~mpg, ~hp) %>% layer_points(fill := "green") %>% bind_shiny("ggvis")

})
noLongerRandom
  • 521
  • 1
  • 5
  • 17

1 Answers1

1

I think this might be what you want.

Note that it took me quite awhile to figure out the validate piece that eliminates an extraneous error message (incorrect string: length(0) 32 expected) on startup initialization of the shinyServer code, but I will remember it for the future now I guess.

library(shiny)
library(ggvis)
library(dplyr)
# library(googleVis) # used observe instead now

u <- shinyUI(fluidPage(
  titlePanel("Car Thing"),

  sidebarLayout(
    sidebarPanel(
      uiOutput("choose_gear")                     
    ),
    mainPanel(
      ggvisOutput("ggvis")         
    )
  )
))
gear_nos <- sort(unique(mtcars$gear))

s <- shinyServer(function(input, output, session) {

  output$choose_gear <- renderUI({
      selectInput("gears", "Choose Gear", gear_nos, selected="3")
  })              

  pickedGear <- reactive({
    shiny::validate(need(input$gears, message=FALSE))
    mtcars %>% filter(gear == input$gears)
  })

  # could also replace "observe" with this from googlevis : "output$ggvis <- renderGvis({" 
  observe({
   pickedGear() %>% ggvis(~mpg,~hp) %>% layer_points(fill:="green") %>% bind_shiny("ggvis")
  })
})
shinyApp(u,s)

Yielding:

enter image description here

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • Note that one can get rid of the error message by making the `selectInput` device static. But this should work too, but I have not been successful so far. – Mike Wise Jan 19 '16 at 20:57
  • Thanks. That works and I marked it as accepted, but I'm curious about a coupe of things A) What wasn't working in my code (or why the reactive function wasn't passing a 'pickedGear' value)? B) Why the addition of the 'renderGvis'? Shouldn't it be possible to do this without the additional googleVis package? – noLongerRandom Jan 19 '16 at 23:53
  • For 1), `pickedgear `was not in a reactive so it would not get executed when input changed. Pretty much all your code (except initialization) has to be in a `reactive`, or an `observe`, or some other packaging (like `renderGvis`), or it won't get executed. Shiny uses an event driven model. As for 2), I have only seen `ggvis` work with shiny with the `googleVis` package. But I can't guarantee that it is absolutely needed, since I don't use `ggvis` a lot. – Mike Wise Jan 19 '16 at 23:58
  • Check out the R-shiny cheatsheet if you have not seen it. Google it, it is from R-Studio. – Mike Wise Jan 19 '16 at 23:59
  • Thanks. Re: googleVis. I haven't seen an example with ggvis/shiny that has used it. Two quick samples found on SO: [here](http://stackoverflow.com/questions/26085104/i-cant-get-ggvis-scales-to-remain-fixed-with-reactive-input) and [here](http://stackoverflow.com/questions/24935452/rstudio-shiny-not-able-to-use-ggvis). As I try to back out what's going on, didn't want to have to dig into another package. Thanks again for the replies. – noLongerRandom Jan 20 '16 at 00:11
  • 1
    Those are interesting examples. And I played around and got it to work with an `observe` instead of a `renderGvis` which seems more appropriate to the use of `bind_shiny`. There seems to be some reactive magic going on in the ggvis pipeline, but I am not sure how much flexibility it really gives you in the end. Pipelines are nice and simple, but sometimes too simple. – Mike Wise Jan 20 '16 at 09:09
  • Using `observe` obviates the need for `googlevis` by the way. – Mike Wise Jan 20 '16 at 09:19