R Shiny plot by time

146 views Asked by At

I have data with a row corresponding to an event (Receipts, Made Tackle or Ball carry) at a given time (GameSeconds). I would like to have a dynamic graph adding each event according to their time. The solution I am trying is to put the graph in a loop. But it doesn't work it only displays the last iteration.

    ui <- fluidPage(
  titlePanel("Event"),
  
  sidebarLayout(
    sidebarPanel(
      sliderInput("slider", "slider",
                min = 0, max = 80, value = c(0, 80)),
      actionButton("do", "Play")
    ),
    mainPanel(
      plotOutput("myplot")
    )
  )
)

And

    shinyServer(function(input, output) {
  
  play<- reactiveValues(data=0)
  
  ymin <- 0 # minimum width
  ymax <- 70 # maximum width
  xmin <- -20 # minimum length
  xmax <- 120 # maximum length
  padding = 5
  
  # set our colours for our pitch
  grass_colour <- "#538032"
  line_colour <- "#ffffff"
  background_colour <- "#538032"
  goal_colour <- "#ffffff"
  
    
  observeEvent(input$do, {
    play$data<-1
  })
  
  output$myplot<-renderPlot({

    if(play$data==1){
      for(i in input$slider[1]:input$slider[2]){
        #plot from the start of the sequence to the current time
        DataEventF<-DataEvent[DataEvent$GameSeconds>=input$slider[1]&DataEvent$GameSeconds<=i,]
       p<-ggplot(DataEventF) + xlim(c(xmin-padding,xmax+padding)) +
          ylim(c(ymin-padding,ymax+padding)) +
          theme_blankPitch() +
          geom_rect(aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), fill = NA, colour = line_colour) +
          geom_segment(aes(x = 0, y = ymin, xend =0, yend = ymax),colour = line_colour) + geom_segment(aes(x = 100, y = ymin, xend =100, yend = ymax),colour = line_colour) + geom_segment(aes(x = 50, y = ymin, xend =50, yend = ymax),colour = line_colour) + geom_segment(aes(x = 22, y = ymin, xend = 22, yend = ymax),colour = line_colour)+ geom_segment(aes(x = 40, y = ymin, xend = 40, yend = ymax),colour = line_colour,linetype="dashed")+ geom_segment(aes(x = 60, y = ymin, xend = 60, yend = ymax),colour = line_colour,linetype="dashed")+ geom_segment(aes(x = 78, y = ymin, xend = 78, yend = ymax),colour = line_colour)+ geom_segment(aes(x = 5, y = ymin, xend = 5, yend = ymax),colour = line_colour,linetype="dashed")+ geom_segment(aes(x = 95, y = ymin, xend = 95, yend = ymax),colour = line_colour,linetype="dashed")+geom_segment(aes(x = 0, y = 32.3, xend = 0, yend = 37.7),colour = goal_colour, size = 2)+geom_segment(aes(x = 100, y = 32.3, xend = 100, yend = 37.7),colour = goal_colour, size = 2) +
          geom_segment(aes(x = 19.5, y = 15, xend =24.5, yend = 15),colour = line_colour)+ geom_segment(aes(x = 19.5, y = 55, xend =24.5, yend = 55),colour = line_colour)+ geom_segment(aes(x = 47.5, y = 15, xend =52.5, yend = 15),colour = line_colour)+ geom_segment(aes(x = 47.5, y = 55, xend =52.5, yend = 55),colour = line_colour)  + geom_segment(aes(x = 75.5, y = 15, xend =80.5, yend = 15),colour = line_colour)+ geom_segment(aes(x = 75.5, y = 55, xend =80.5, yend = 55),colour = line_colour) +     
          geom_point(data = DataEventF[DataEventF$StatName=="Receipts",],aes(x = X, y = Y,colour=TeamId,fill=TeamId),
                     pch = 21,
                     size = 2) +
          geom_point(data = DataEventF[DataEventF$StatName=="Made Tackle",],aes(x = X, y = Y,colour=TeamId),
                     pch = 2,
                     size = 2) + 
          geom_segment(data = DataEventF[DataEventF$StatName=="Other Passes",],aes(x=X, y=Y, xend=X.Pass.end, yend=Y.Pass.end, colour=TeamId), size = 1,linetype="dotted") 

    
        plot(p)
      } 
    }
  })
})

Maybe this is not possible and I have to use solutions like the gganimate package but I also had some difficulties with this package which has a rather long loading time in addition.

Thanks !

Sample data :

structure(list(StatName = c("Receipts", "Made Tackle", "Made Tackle", 
"Receipts", "Receipts", "Made Tackle", "Receipts"), GameSeconds = c(7, 
9, 10, 21, 26, 26, 34), X = c(110, 125, 125, 141, 385, 370, 366
), Y = c(62, 103, 103, 98, 44, 43, 72), X.Pass.end = c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), 
    Y.Pass.end = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_), TeamId = structure(c(1L, 2L, 2L, 1L, 
    2L, 1L, 2L), .Label = c("1114", "1128"), class = "factor")), row.names = c(NA, 
-7L), class = c("tbl_df", "tbl", "data.frame"))
0

There are 0 answers