Search code examples
rshinydashboardreactive

ggplot does not show up on my R shiny dashboard


I am struggling to figure out how to make the plot reactive. The point of my code is you select a radio button with your team where each team is its own dataset buy they all have the same variables. I understand that I could merge all of them together but I am worried that would slow down the speed of the application itself, also if I were to continue working on this and add specific player stats then I believe my current method is the proper way.

I can print the table easily outside of the shiny app but not in it.

So, overall, the code runs but the plot does not show up and I think I have been looking at it too long and am possibly overlooking a potentially simple error.

The datasets I am using come from NBAsavant.com (link to 76ers team data)

library(shiny)
library(plotly)
library(tidyverse)
library(rsconnect)
library(readr)
library(ggplot2)
library(dplyr)
library(jpeg)
library(grid)
library(RCurl)


ui <- fluidPage(
  
  titlePanel("NBA 2017-2018 Season: Shooting Analysis"),
  
  fluidRow(
    
    column(2,
      
      radioButtons(inputId = "radio", label = "Select NBA Team",
                   choices = c("76ers", "Bucks", "Bulls",
                                "Cavaliers", "Celtics", "Clippers",
                                "Grizzlies","Hawks","Heat",
                                "Hornets","Jazz","Kings",
                                "Knicks","Lakers","Magic",
                                "Mavericks","Nets","Nuggets",
                                "Pacers","Pelicans","Pistons",
                                "Raptors","Rockets","Spurs",
                                "Suns","Thunder","Timberwolves",
                                "Trail Blzers","Warriors","Wizards"),
                   selected = "76ers")
      ),
    
    fluidRow(10,
      box(width = 10,
              plotOutput("court_plot")))
  )
)

Server <- function(input, output) {
  
   TeamNBA <- reactive({
     if (input$radio == "76ers")
       SeventySixers
     else if (input$radio == "Bucks")
       Bucks
     else if (input$radio == "Bulls")
       Bulls
     else if (input$radio == "Cavaliers")
       Cavaliers
     else if (input$radio == "Celtics")
       Celtics
     else if (input$radio == "Clippers")
       Clippers
     else if (input$radio == "Grizzlies")
       Grizzlies
     else if (input$radio == "Hawks")
       Hawks
     else if (input$radio == "Heat")
       Heat
     else if (input$radio == "Hornets")
       Hornets
     else if (input$radio == "Jazz")
       Jazz
     else if (input$radio == "Kings")
       Kings
     else if (input$radio == "Knicks")
       Knicks
     else if (input$radio == "Lakers")
       Lakers
     else if (input$radio == "Magic")
       Magic
     else if (input$radio == "Mavericks")
       Mavericks
     else if (input$radio == "Nets")
       Nets
     else if (input$radio == "Nuggets")
       Nuggets
     else if (input$radio == "Pacers")
       Pacers
     else if (input$radio == "Pelicans")
       Pelicans
     else if (input$radio == "Pistons")
       Pistons
     else if (input$radio == "Raptors")
       Raptors
     else if (input$radio == "Rockets")
       Rockets
     else if (input$radio == "Spurs")
       Spurs
     else if (input$radio == "Suns")
       Suns
     else if (input$radio == "Thunder")
       Thunder
     else if (input$radio == "Timberwolves")
       Timberwolves
     else if (input$radio == "Trail Blazers")
       TrailBlazers
     else if (input$radio == "Warriors")
       Warriors
     else if (input$radio == "Wizards")
       Wizards
   })


  output$court_plot <- renderPlot({
    
    courtImg <- "http://robslink.com/SAS/democd54/nba_court_dimensions.jpg"
    court <- rasterGrob(readJPEG(getURLContent(courtImg)),
                    width=unit(1, "npc"), height=unit(1, "npc"))
    
    court_plot <- ggplot(TeamNBA, aes(x=x, y=y)) +
      annotation_custom(court, -250, 250, -50, 420) +
           geom_hex(bins = 50, alpha = .8) +
        scale_fill_continuous(type = "viridis") +
           xlim(-250, 250) +
           ylim(-50, 420)
    
    print(court_plot)
    
  })
}

shinyApp(ui = ui, server = server)

Solution

  • As you didn't provide the data, I used the good old mtcars as an example.

    Issues in the code:

    • server/Server (R is case sensitive)
    • a reactive dataset has to be called with brackets: TeamNBA()
    • scale_fill_continuous(type = "viridis") uses a variable probably not in the dataset (sounds like from the iris dataset), so I removed it
    • at the end of the renderPlot, don't use print
    • remove the box in the ui function
    library(shiny)
    library(ggplot2)
    library(jpeg)
    library(grid)
    library(RCurl)
    
    
    ui <- fluidPage(
      
      titlePanel("NBA 2017-2018 Season: Shooting Analysis"),
      
      fluidRow(
        
        column(2,
               
               radioButtons(inputId = "radio", label = "Select NBA Team",
                            choices = c("76ers", "Bucks", "Bulls",
                                        "Cavaliers", "Celtics", "Clippers",
                                        "Grizzlies","Hawks","Heat",
                                        "Hornets","Jazz","Kings",
                                        "Knicks","Lakers","Magic",
                                        "Mavericks","Nets","Nuggets",
                                        "Pacers","Pelicans","Pistons",
                                        "Raptors","Rockets","Spurs",
                                        "Suns","Thunder","Timberwolves",
                                        "Trail Blzers","Warriors","Wizards"),
                            selected = "76ers")
        ),
        
        fluidRow(10,
                 
                     plotOutput("court_plot"))
      )
    )
    
    server <- function(input, output) {
      
      TeamNBA <- reactive({
        if (input$radio == "76ers")
          mtcars
        else if (input$radio == "Bucks")
          Bucks
        else if (input$radio == "Bulls")
          Bulls
        else if (input$radio == "Cavaliers")
          Cavaliers
        else if (input$radio == "Celtics")
          Celtics
        else if (input$radio == "Clippers")
          Clippers
        else if (input$radio == "Grizzlies")
          Grizzlies
        else if (input$radio == "Hawks")
          Hawks
        else if (input$radio == "Heat")
          Heat
        else if (input$radio == "Hornets")
          Hornets
        else if (input$radio == "Jazz")
          Jazz
        else if (input$radio == "Kings")
          Kings
        else if (input$radio == "Knicks")
          Knicks
        else if (input$radio == "Lakers")
          Lakers
        else if (input$radio == "Magic")
          Magic
        else if (input$radio == "Mavericks")
          Mavericks
        else if (input$radio == "Nets")
          Nets
        else if (input$radio == "Nuggets")
          Nuggets
        else if (input$radio == "Pacers")
          Pacers
        else if (input$radio == "Pelicans")
          Pelicans
        else if (input$radio == "Pistons")
          Pistons
        else if (input$radio == "Raptors")
          Raptors
        else if (input$radio == "Rockets")
          Rockets
        else if (input$radio == "Spurs")
          Spurs
        else if (input$radio == "Suns")
          Suns
        else if (input$radio == "Thunder")
          Thunder
        else if (input$radio == "Timberwolves")
          Timberwolves
        else if (input$radio == "Trail Blazers")
          TrailBlazers
        else if (input$radio == "Warriors")
          Warriors
        else if (input$radio == "Wizards")
          Wizards
      })
      
      
      output$court_plot <- renderPlot({
        
        courtImg <- "http://robslink.com/SAS/democd54/nba_court_dimensions.jpg"
        court <- rasterGrob(readJPEG(getURLContent(courtImg)),
                            width=unit(1, "npc"), height=unit(1, "npc"))
        
        court_plot <- ggplot(TeamNBA(), aes(x=mpg, y=wt)) +
          annotation_custom(court, -250, 250, -50, 420) +
          geom_hex(bins = 50, alpha = .8) +
          xlim(-250, 250) +
          ylim(-50, 420)
        
        court_plot
        
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    Note: I changed the aes to work with mtcars, you have to adapt it