1

Using shiny and ggplot2 I am trying to create a scatter plot that will go through the Time variable (starting from 00:00 and ending at 24:00) and plot X and Y when the time for that coordinate comes up. Which will create a frame by frame reference on how the points are moving around.

df: df example

ui.R - Slider Widget Input

sliderInput("time", "Time:", 00:00, 24:00, 00:00,
                    step = 00:01, animate=
                        animationOptions(interval=300, loop=TRUE))
  • In the widget I specified step = 00:01 so it should go through one minute intervals, however, it actually goes through 1 hour intervals.

  • I can't figure out the scatter plot section for the server side and how to get it to draw from the df that I have in the environment.

Pieter van den Ham
  • 4,381
  • 3
  • 26
  • 41
t00T
  • 25
  • 3
  • 1
    what's wrong with the widget code? What do you get now and how is it different from what you're trying to get? – PabTorre Aug 03 '17 at 21:42
  • Got a few things I should have added but the site said I put in too many requests so I was not able to edit it: (1) In the widget I specified step = 00:01 so it goes through one minute intervals, however, it actually goes through 1 hour intervals. (2) I can't figure out the scatter plot section for the server side and how to get it to draw from the df that I have in the environment. – t00T Aug 04 '17 at 13:53

1 Answers1

1

It's a good habit to provide reproducible example for the whole question as it's hard to guess what you've made and what you want exactly.

In sliderInput you show either numeric values or POSIXct, shiny cannot render "00:00" without letting it know it's time format. When you use time format, don't forget to check timezones issue.

Probably, this is what you're looking for:

library(shiny)
shinyApp(
  ui = fluidPage(sliderInput("time", "Time:", 
                             min = as.POSIXct("00:00",format="%H:%M", tz="UTC"),
                             max = as.POSIXct("24:00",format="%H:%M", tz="UTC"),
                             value = c(
                               as.POSIXct("00:00",format="%H:%M")
                             ), timeFormat = "%H:%M", step=60, timezone = "+0000",
                             animate=
                               animationOptions(interval=300, loop=TRUE)),
                 plotOutput("plot")
                 ),
  server = function(input, output) {

    df<-data.frame(id=c(1,1,2,2), x=c(11,8,3,19), y=c(6,13,4,12), 
                   Time=c("0:03","1:03","15:45","13:29"))

    output$plot<-renderPlot({
      dt<-cbind(df,Ftime=as.POSIXct(df$Time, format="%H:%M", tz="UTC"))
      dt<-dt[dt$Ftime==input$time,]

      if(nrow(dt)==0) ggplot(df, aes(x,y)) else ggplot(dt,aes(x,y))+geom_point()

    })


  }
)
Asayat
  • 645
  • 10
  • 23