ggplot没有出现在闪亮的应用程序中

时间:2016-04-23 01:06:23

标签: r ggplot2 shiny

我似乎无法让我的情节出现在拉赫曼数据这个闪亮的应用程序中。任何帮助将不胜感激。

ui.R

library(shiny)
library(ggplot2)
shinyUI(fluidPage(
  fluidRow(
sidebarLayout(
  sidebarPanel(
    selectInput("team","Select Team",choices=levels(teamID)),
    sliderInput("year","Include Years in Range",min=1871,max=2014,value=c(1871,2014), sep="")
    ),
  mainPanel(
    plotOutput("pitchingplot")
  )
)
 )
))

server.R

 library(dplyr)
library(ggplot2)
library(shiny)

shinyServer(function(input, output) {
  Pitching%>%
group_by(input$teamID,yearID)%>%
filter(teamID=input$team,yearID>=input$year[1]& yearID<=input$year[2])
summarise(TotER=sum(ER))

  output$pitchingplot<-renderPlot({
g<-ggplot(data=Pitching,aes(x=yearID,y=TotER)) + geom_point(aes(color=input$team))
g
  })
})

1 个答案:

答案 0 :(得分:1)

您没有准确指定您想要的内容,因此很难提供帮助。我所能做的只是使代码工作 - 添加了反应数据集data并改变了一些事情。

library(shiny)
library(ggplot2)
library(dplyr)
library(Lahman)

data("LahmanData")

ui <- shinyUI(fluidPage(
  fluidRow(
    sidebarLayout(
      sidebarPanel(                                     # added Pithing$
        selectInput("team","Select Team", choices=levels(Pitching$teamID)), 
        sliderInput("year","Include Years in Range",min=1871,max=2014,value=c(1871,2014), sep="")
      ),
      mainPanel(
        plotOutput("pitchingplot")
      )
    )
  )
))



server <- shinyServer(function(input, output) {

  # create a reactive dataset which is passed to ggplot object
  data <- reactive({ 
    Pitching%>%
      group_by(teamID == input$team, yearID)%>% # changed to teamID == input$team
      filter(yearID >= input$year[1] & yearID <= input$year[2]) %>%
      summarise(TotER=sum(ER))
  })

  output$pitchingplot<-renderPlot({
             # changed to data()                 # use aes_string if the variable is passed as a string
    g<-ggplot(data=data(),aes(x=yearID,y=TotER)) + geom_point()
    g
  })
})

shinyApp(ui = ui, server)