Skip to content

Instantly share code, notes, and snippets.

@Protonk
Created July 17, 2013 14:18
Show Gist options
  • Save Protonk/6020947 to your computer and use it in GitHub Desktop.
Save Protonk/6020947 to your computer and use it in GitHub Desktop.
Useful mostly for forcing sub-second timing in R
library(RJSONIO)
library(ggplot2)
library(reshape2)
accel.list <- fromJSON(file.path("~/dev/bocoup", "johnny-five", "accel.json"))
accelToDF <- function(x) {
sm <- x[["smooth"]]
rough <- x[["accel"]]
time <- x[["time"]]
names(sm) <- c("X", "Y")
names(rough) <- c("X", "Y")
out.df <- data.frame(Type = c("smooth", "accel"),
rbind(sm, rough),
Time = time)
return(out.df)
}
accel.df <- do.call(rbind, lapply(accel.list, accelToDF))
rownames(accel.df) <- as.character(1:nrow(accel.df))
timeNS <- function() {
options(digits.secs = 6)
timed <- unclass(as.POSIXlt(substring(accel.df[, "Time"], 12, 23),
"%H:%M:%OS", tz = "UTC"))
timed <- ((timed$sec / 60 + timed$min) / 60 + timed$hour) * 3600
timed <- timed - timed[1]
options(digits.secs = 3)
return(timed)
}
accel.df[, "Time"] <- timeNS()
accel.melt <- melt(accel.df[, names(accel.df) != "Time"])
accel.melt[, "Time"] <- accel.df[, "Time"]
ggplot(accel.melt) + geom_line(aes(x = Time, y = value, colour = Type)) +
facet_grid(variable ~ .)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment