Skip to content

Instantly share code, notes, and snippets.

@alexschell
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexschell/e3192413874cf51b9ca6 to your computer and use it in GitHub Desktop.
Save alexschell/e3192413874cf51b9ca6 to your computer and use it in GitHub Desktop.
Shiny app that displays time use trends for a custom set of tags from a TagTime log file
# Based on https://github.com/alexschell/misc-R/blob/master/tagtime_trends.R
# Example of the running app: http://alexschell.shinyapps.io/tagtime-trends/
require("httr")
ping.interval <- 45 # mean interval between pings in minutes (default 45)
tags <- c("prod3", "prod0", "proj", "auto", "school|class|hw",
"work", "slp", "drive", "social|im", "email|sms|phonecall",
"favor", "net", "tagtime|pfeedback", "outside|garden") # CHANGEME
# vector of tag names (or regular expressions) to plot trends of
# should correspond to elements in choices below
choices <- c("Productivity", #CHANGEME
"Wasted time",
"Projects",
"Autodidacticism",
"School",
"Work",
"Sleep",
"Driving",
"Social time",
"Communication",
"Favors",
"Web browsing",
"Self tracking",
"Time spent outside")
# should be identical to choices argument to selectInput() in ui.R
shinyServer(
function(input, output) {
ttlog <- content(GET("http://log/file/url")) # CHANGEME
ttlog <- unlist(strsplit(ttlog, "\n"))
ttlog <- strsplit(ttlog, " ")
dat <- data.frame(time = sapply(ttlog, function(x) as.numeric(x[1])),
tags = sapply(ttlog, function(x) {
str <- x[x != ""]
paste(str[-(length(str) - 2:0)][-1], collapse = " ")
}))
mat <- sapply(tags, function(x) grepl(x, dat$tags))
x.label.int <- seq(min(dat$time), max(dat$time), length = 8)
x.label.str <- as.Date(as.POSIXct(x.label.int, origin = "1970-01-01"))
output$hist <- renderPlot({
n.breaks <- as.numeric(input$n.bins) + 1
breaks <- seq(min(dat$time), max(dat$time), length = n.breaks)
bin.width <- diff(breaks[1:2]) # histogram bin width in seconds
n.tag <- which(input$tag == choices)
uptime <- dat$time[mat[,n.tag]]
h <- hist(uptime, plot = FALSE, breaks = breaks)
h$counts <- h$counts * ping.interval / 60 * 7 * 24 * 3600 / bin.width
# convert from pings per bin to hours per week
d <- density(uptime, adjust = input$bandwidth, cut = -0.5)
d$y <- d$y * sum(h$counts) * bin.width # scale density to plot
par(las = 1)
plot(h, main = paste(input$tag, "(hours per week)"),
col = "aliceblue", border = "white", ylab = "",
xaxt = 'n', xlab = "")
axis(side = 1, at = x.label.int, labels = x.label.str)
axis(4)
rug(uptime, tick = 0.02, side = 1, col = "lightcoral")
lines(d, lwd = 2, col = "lightcoral")
})
}
)
shinyUI(fluidPage(
titlePanel("Some things I do - tracked with TagTime"),
sidebarLayout(
sidebarPanel(
p("Display time use patterns from my ",
a("TagTime", href = "http://messymatters.com/tagtime/"),
" log file."),
selectInput("tag",
label = "Pick a category to look at:",
choices = c("Productivity", #CHANGEME; same as choices in server.R
"Wasted time",
"Projects",
"Autodidacticism",
"School",
"Work",
"Sleep",
"Driving",
"Social time",
"Communication",
"Favors",
"Web browsing",
"Self tracking",
"Time spent outside"),
selected = "Productivity"),
#helpText
selectInput(inputId = "n.bins",
label = "Number of bins in the histogram:",
choices = c(10, 20, 30, 50, 70),
selected = 20),
sliderInput("bandwidth",
label = "Density estimate smoothness:",
min = 0.25, max = 1, value = 0.55)
),
mainPanel(plotOutput("hist"))
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment