问题渲染ggplot闪亮

时间:2016-05-17 18:05:00

标签: r csv ggplot2 shiny rstudio

我目前在渲染情节时遇到问题。布局都很好,但在运行时,情节不会出现。

链接到csv文件中的数据: https://www.dropbox.com/s/hv3k12ja9r10tzz/pointvaluedfmelt.csv?dl=0

UI代码:

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"))

  ))

服务器代码:

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)
})
  }

1 个答案:

答案 0 :(得分:1)

那里有一些错误:

1-渲染图不需要“function()”关键字,只需要renderPlot({})
2-你没有以正确的方式使用反应。您可以使用两个响应对象使其变得简单和更好,并且除了将所有内容放在renderPlot逻辑中之外,renderPlot也会使用它。这样,您就可以重用对象并使代码更清晰 3-因为你以错误的方式做反应,所以当你改变值时,data.frame是空的......

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)