1
votes

I am trying to learn how to replace static plots in shiny with ggvis plots. With the following ui.R and server.R files, I can get a perfectly fine working shiny app when my plot output is base-R or ggplot. I get the following error when I try to use ggvis.

Error in handlers$add(handler, key, tail) : Key / already in use

I have tried changing the location of my files to a different directory, clearing my global environment, etc. Nothing seems to work thus far.

I have tried to reproduce the most minimal reproducible example. The following is reproducing the error on my machine. I am inputting the data from a csv file stored in the same folder as the ui.R and server.R . I have added the dput() of some data that can be used to reproduce the csv file.

Here is the example:

ui.R

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


# Define UI
shinyUI(pageWithSidebar(


  # Application title
  headerPanel("Cricket"),

  sidebarPanel(

    selectInput("hteam", 
                label = "Home Team",
                choices = c("All Teams", "Australia", "England"), selected = "All teams"),

    br()

  ),


  mainPanel(
    plotOutput("CrickPlot")
  )

))

server.R

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




shinyServer(function(input, output) {


  #### Input raw data
  df <- read.csv("mydf.csv", stringsAsFactors=F, header=T)

  df1 <- reactive({

    hometeam <-  input$hteam

  if(input$hteam != "All Teams"){  df <- df %>% filter(team==hometeam) }

  df %>% 
    group_by(player) %>% 
    summarize(totalruns=sum(runs,na.rm=T), totalinns=n() )

  })


####

output$CrickPlot <- renderPlot({


    tmp <- df1()

    tmp$id <- 1:nrow(tmp)

       all_values <- function(x) {
         if(is.null(x)) return(NULL)
         row <- tmp[tmp$id == x$id, ]
         paste("Name: ", tmp$player[x$id],
               "<br>Country: ",
               tmp$team[x$id],
               "<br>Total runs: ",
               tmp$totalruns[x$id])
       }  


    tmp %>% 
               ggvis(x = ~totalinns, 
                     y = ~totalruns, 
                     size := input_slider(10, 100),
                     size.hover := 200,
                     opacity := input_slider(0, 1),
                     key := ~id) %>%
         layer_points() %>%
         add_tooltip(all_values, "hover")



#  x <- df1()
#  plot(x$totalruns, x$totalinns)

})

}
)

The ggvis as coded here is a little simpler than what I am using in reality. However, this still reproduces the error. If I summarize my df and try to make a ggvis chart outside of shiny, this code works perfectly well. Also, just for illustration purposes, the last two lines that come after the hashmarks will produce a scatterplot in base-R if all the ggvis stuff is removed. Therefore, I think it's something to do with the ggvis inside the shiny app.

Here is the mydf data for this example:

dput(mydf)

structure(list(player = c("CB Fry", "CB Fry", "G Boycott", "G Boycott", 
                          "G Boycott", "G Boycott", "MJ Slater", "MJ Slater", "MJ Slater", 
                          "MJ Slater", "MJ Slater", "MJ Slater", "MJ Slater", "MJ Slater", 
                          "SK Warne", "SK Warne", "SK Warne", "SK Warne", "SK Warne", "SK Warne"
), team = c("England", "England", "England", "England", "England", 
            "England", "Australia", "Australia", "Australia", "Australia", 
            "Australia", "Australia", "Australia", "Australia", "Australia", 
            "Australia", "Australia", "Australia", "Australia", "Australia"
), runs = c(1L, 50L, 68L, 31L, 30L, 23L, 26L, 16L, 123L, 1L, 
            45L, 43L, 28L, 10L, 15L, 2L, 0L, 14L, 2L, NA)), row.names = c(NA, 
                                                                          -20L), .Names = c("player", "team", "runs"), class = "data.frame")

Thanks in advance. I tried to make this as minimal as possible whilst showing all possible details relevant to the error.

1

1 Answers

9
votes

In server.R

  output$ggvisplot_ui <- renderUI({
    ggvisOutput("ggvisplot")
  })

  dl <- mtcars
  dl %>%
    ggvis(~mpg, ~wt) %>% 
    layer_points() %>% 
    bind_shiny("ggvisplot")

In ui.R

  mainPanel(
    htmlOutput("ggvisplot_ui")
  )