This draws an xyplot
from csv data obtained from RescueTime
API for activity time for "http://codereview.stackexchange.com".
# -------------------------------------------------------
# PLOT TIME SPENT ON CODEREVIEW SE
# USING RESCUE TIME API AND R
# -------------------------------------------------------
# -------------------------------------------------------
# LIBRARIES
if(!require("RCurl")){
install.packages("RCurl")
}
if(!require("lattice")){
install.packages("lattice")
}
require("RCurl")
require("lattice")
# -------------------------------------------------------
print("Initializing...")
# -------------------------------------------------------
# RESCUE TIME API ACCESS
# api key
# get from https://www.rescuetime.com/anapi/manage
api_key <- "..."
# query from date
from_date <- "2014-01-01"
# query to date
to_date <- format(Sys.Date(), "%Y-%m-%d")
# activity to get details of
activity <- "codereview.stackexchange.com"
# --api description--
# for more details see https://www.rescuetime.com/anapi/setup/documentation
# key - API key
# format - Output format
# rb - Restrict Start Date ("%Y-%m-%d")
# re - Restrict End Date ("%Y-%m-%d")
# rk - Restrict Kind (in this case it's activity)
# pv - Perspective (in this case it's interval)
# rs - Resolution Time (if it is day it's a daily report)
# rt - Restrict Thingy (Restrict Kind of Type)
url_format <- "https://www.rescuetime.com/anapi/data?key=%s&format=csv&rb=%s&re=%s&rk=activity&pv=interval&rs=day&rt=%s"
# create url
api_url <- sprintf(url_format,api_key,from_date,to_date,activity)
# -------------------------------------------------------
# -------------------------------------------------------
# PLOT
# get csv as a text
csv_text_data <- textConnection(getURL(api_url))
# parse csv
activity_data <- read.csv(csv_text_data,header = TRUE)
print(sprintf("Mean Time Spent on '%s':%f (seconds)",activity,mean(activity_data$Time)))
activity_time_spent <- activity_data$Time
activity_dates <- activity_data$Date
# create an index for dates
activity_date_index <- 1:length(activity_dates)
# plot
print(xyplot(activity_time_spent ~ activity_date_index, type="b", xlab = "Date Index", ylab = "Time (seconds)" ))
# -------------------------------------------------------
# -------------------------------------------------------
# CLEAN UP
detach("package:RCurl", unload = TRUE)
detach("package:lattice", unload = TRUE)
rm(list = ls())
# -------------------------------------------------------
Does this follow R conventions?
Execute by loading it to R console using source("./codereview.R")
Download
Output
Loading required package: RCurl Loading required package: bitops Loading required package: lattice [1] "Initializing..." [1] "Mean Time Spent on 'codereview.stackexchange.com':2764.559322 (seconds)"