rshinyggvischoroplethr

ggvis interactivity in Shiny


I am trying to embed a ggvis plot in shiny with a hover element, but it seems that it doesn't render in shiny (the hover element that is). Below are the Server and UI components:

server.R:

library(choroplethrAdmin1)
library(choroplethr)
library(ggvis)
library(dplyr)

jor<-get_admin1_map("jordan")
jor$total <- runif(983) * 100


jor %>% ggvis(~long, ~lat) %>%
layer_paths(data = jor %>% group_by(name), 
          strokeWidth := 0, fill = ~total) %>%
hide_axis("x") %>% hide_axis("y") %>%
add_tooltip(function(data){paste("Gov: ", data$name, "<br>", "Total: ", as.character(data$lat))}, "hover")

shinyServer(function(input, output) {

jor%>%
ggvis(~long, ~lat) %>%
layer_paths(data = jor %>% group_by(name), 
          strokeWidth := 0, fill = ~total) %>%
hide_axis("x") %>% hide_axis("y") %>%
bind_shiny("p")
})

ui.R

library(shiny)
library(ggvis)

shinyUI(fluidPage(


titlePanel("Hello Shiny!"),


mainPanel(
  ggvisOutput("p")
)
))

Is there something wrong in the code, or is that natural? It would be a real disappointment if the interactivity of ggvis is lost in shiny. If that is the case, are there any alternatives?


Solution

  • You are binding to shiny with bind_shiny with a ggvis plot that doesnt have the hover points. You do however have it above the shinyServer function. Just remove that, surplus to requirements anyway, and add the hover before the bind_shiny with the shinyServer.

    server.R

    library(choroplethrAdmin1)
    library(choroplethr)
    library(ggvis)
    library(dplyr)
    
    jor<-get_admin1_map("jordan")
    jor$total <- runif(983) * 100
    
    shinyServer(function(input, output) {
    
      jor%>%
        ggvis(~long, ~lat) %>%
        layer_paths(data = jor %>% group_by(name), 
                    strokeWidth := 0, fill = ~total) %>%
        hide_axis("x") %>% hide_axis("y") %>%
        add_tooltip(function(data){paste("Gov: ", data$name, "<br>", "Total: ",     as.character(data$lat))}, "hover") %>%
        bind_shiny("p")
    })