Reputation: 325
I'm currently having issues rendering my plot in shiny. The layout is all fine, but when run the plot does not appear.
Link to data in csv file: https://www.dropbox.com/s/hv3k12ja9r10tzz/pointvaluedfmelt.csv?dl=0
UI Code:
library(shiny)
library(ggplot2)
library(RColorBrewer)
pointvaluedf.melt<- read.csv("pointvaluedfmelt.csv", stringsAsFactors = F)
pointvaluedf.melt$X<- NULL
pointvaluedf.melt$PLAYER_NAME<- as.factor(pointvaluedf.melt$PLAYER_NAME)
pointvaluedf.melt$TEAM_ABBREVIATION<- as.factor(pointvaluedf.melt$TEAM_ABBREVIATION)
pointvaluedf.melt$name.zone<- as.factor(pointvaluedf.melt$name.zone)
ui <- fluidPage(
titlePanel("Top 5 Most Valuable Shots by Player"),
sidebarLayout(
sidebarPanel(selectInput("team",
label = "Choose a Team",
choices = c("Celtics", "Nets","Knicks", "76ers", "Raptors",
"Mavericks","Rockets","Grizzlies","Pelicans",
"Spurs","Bulls","Cavs","Pistons","Pacers","Bucks",
"Nuggets","Timberwolves","Thunder","Blazers",
"Jazz","Hawks","Hornets","Heat","Magic","Wiz",
"Warriors","Clippers","Lakers","Suns","Kings"),
selected = "Celtics"), width = 2
),
mainPanel(plotOutput("myplot"))
))
Server Code:
library(shiny)
library(ggplot2)
library(RColorBrewer)
pointvaluedf.melt<- read.csv("pointvaluedfmelt.csv", stringsAsFactors = F)
pointvaluedf.melt$X<- NULL
pointvaluedf.melt$PLAYER_NAME<- as.factor(pointvaluedf.melt$PLAYER_NAME)
pointvaluedf.melt$TEAM_ABBREVIATION<- as.factor(pointvaluedf.melt$TEAM_ABBREVIATION)
pointvaluedf.melt$name.zone<- as.factor(pointvaluedf.melt$name.zone)
server <- function(input, output) {
df<- reactive({pointvaluedf.melt[pointvaluedf.melt$TEAM_ABBREVIATION==input$team,]})
output$myplot <- renderPlot(function(){
dd<- df()
tea <- switch(input$team,
"Celtics" = "BOS",
"Nets" = "BKN",
"Knicks" = "NYK",
"76ers" = "PHI",
"Raptors" = "TOR",
"Mavericks" = "DAL",
"Rockets" = "HOU",
"Grizzlies" = "MEM",
"Pelicans" = "NOP",
"Spurs" = "SAS",
"Bulls" = "CHI",
"Cavs" = "CLE",
"Pistons" = "DET",
"Pacers" = "IND",
"Bucks" = "MIL",
"Nuggets" = "DEN",
"Timberwolves" = "MIN",
"Thunder" = "OKC",
"Blazers" = "POR",
"Jazz" = "UTA",
"Hawks" = "ATL",
"Hornets" = "CHA",
"Heat" = "MIA",
"Magic" = "ORL",
"Wiz" = "WAS",
"Warriors" = "GSW",
"Lakers" = "LAL",
"Clippers" = "LAC",
"Suns" = "PHX",
"Kings" = "SAC")
p<- ggplot(data=head(subset(dd, TEAM_ABBREVIATION %in% tea)
[order(-subset(dd, TEAM_ABBREVIATION %in% tea)[,4]),],5),
aes(x=reorder(name.zone,-value), y=value))+
geom_bar(stat="identity", fill="#4292C6", col="black", size=1.2)+
theme(axis.text.x=element_text(angle=35, hjust=1))+
labs(x="Player and Shot Type", y="Point Value", title="Top 5 Value Shots")
print(p)
})
}
Upvotes: 0
Views: 947
Reputation: 4172
There are some errors in there:
1- Render plot does not need the "function()" keyword on it, just renderPlot({})
2- You are not using reactive the proper way. You can make it simple and better with two reactive objects, and renderPlot consuming it besides put everything inside the renderPlot logic. This way, you can reuse objects and make your code cleaner.
3- Because you are doing reactive the wrong way, the data.frame was empty when you change the values...
library(shiny)
library(ggplot2)
library(RColorBrewer)
pointvaluedf.melt<- read.csv("pointvaluedfmelt.csv", stringsAsFactors = F)
pointvaluedf.melt$X<- NULL
pointvaluedf.melt$PLAYER_NAME<- as.factor(pointvaluedf.melt$PLAYER_NAME)
pointvaluedf.melt$TEAM_ABBREVIATION<- pointvaluedf.melt$TEAM_ABBREVIATION
pointvaluedf.melt$name.zone<- as.factor(pointvaluedf.melt$name.zone)
ui <- fluidPage(
titlePanel("Top 5 Most Valuable Shots by Player"),
sidebarLayout(
sidebarPanel(selectInput("team",
label = "Choose a Team",
choices = c("Celtics", "Nets","Knicks", "76ers", "Raptors",
"Mavericks","Rockets","Grizzlies","Pelicans",
"Spurs","Bulls","Cavs","Pistons","Pacers","Bucks",
"Nuggets","Timberwolves","Thunder","Blazers",
"Jazz","Hawks","Hornets","Heat","Magic","Wiz",
"Warriors","Clippers","Lakers","Suns","Kings"),
selected = "Celtics"), width = 2
),
mainPanel(plotOutput("myplot"))
))
server <- function(input, output, session) {
df <- reactive({
pointvaluedf.melt[pointvaluedf.melt$TEAM_ABBREVIATION==tea(),]
})
tea <- reactive({
switch(input$team,
"Celtics" = "BOS",
"Nets" = "BKN",
"Knicks" = "NYK",
"76ers" = "PHI",
"Raptors" = "TOR",
"Mavericks" = "DAL",
"Rockets" = "HOU",
"Grizzlies" = "MEM",
"Pelicans" = "NOP",
"Spurs" = "SAS",
"Bulls" = "CHI",
"Cavs" = "CLE",
"Pistons" = "DET",
"Pacers" = "IND",
"Bucks" = "MIL",
"Nuggets" = "DEN",
"Timberwolves" = "MIN",
"Thunder" = "OKC",
"Blazers" = "POR",
"Jazz" = "UTA",
"Hawks" = "ATL",
"Hornets" = "CHA",
"Heat" = "MIA",
"Magic" = "ORL",
"Wiz" = "WAS",
"Warriors" = "GSW",
"Lakers" = "LAL",
"Clippers" = "LAC",
"Suns" = "PHX",
"Kings" = "SAC")
})
output$myplot <- renderPlot({
p <- ggplot(data=head(subset(df(), TEAM_ABBREVIATION %in% tea())
[order(-subset(df(), TEAM_ABBREVIATION %in% tea())[,4]),],5),
aes(x=reorder(name.zone,-value), y=value))+
geom_bar(stat="identity", fill="#4292C6", col="black", size=1.2)+
theme(axis.text.x=element_text(angle=35, hjust=1))+
labs(x="Player and Shot Type", y="Point Value", title="Top 5 Value Shots")
p
})
}
shinyApp(ui, server)
Upvotes: 1