Skip to content

Commit

Permalink
Merge branch 'import-export-existing-raw-data' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Katrin Leinweber committed Dec 21, 2014
2 parents e5a639c + d18247a commit 48f1930
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions summarize-flattr-reports.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,31 @@ Flattr_filenames <- list.files(flattr_dir, pattern = "flattr-revenue-[0-9]*.csv"
original_wd <- getwd()
setwd(flattr_dir)

# read data from CSVs into data frame
raw <- do.call("rbind", # constructs and executes a call of the rbind function => combines R objects
lapply(Flattr_filenames, # applies function read.csv over list or vector
read.csv,
encoding = "UTF-8", # learned from RTFM, but works only on Win7
sep = ";", dec = ",", # csv defaults: , & . but Flattr uses "European" style
stringsAsFactors = FALSE)) # Function structure learned from https://stat.ethz.ch/pipermail/r-help/2010-October/255593.html
if ("flattr-revenue-raw.csv" %in% list.files(flattr_dir, pattern = "*.csv")) {
known_raw <- read.csv("flattr-revenue-raw.csv", encoding = "UTF-8", sep = ";", dec = ",", stringsAsFactors = FALSE)
# check for existing raw date & merge with new
if (length(unique(known_raw$period)) < length(Flattr_filenames)) {
known_months <- paste(paste("flattr-revenue", # turn months into filenames
sub("-", "", unique(known_raw$period)),
sep = "-"),
"csv", sep = ".")

new_months <- setdiff(Flattr_filenames, known_months)
new_raw <- do.call("rbind", lapply(new_months, read.csv, encoding = "UTF-8", sep = ";", dec = ",", stringsAsFactors = FALSE))
raw <- rbind(known_raw, new_raw) # learned from http://stackoverflow.com/a/27313467
}} else { # read data from all CSVs into data frame
raw <- do.call("rbind", # constructs and executes a call of the rbind function => combines R objects
lapply(Flattr_filenames, # applies function read.csv over list or vector
read.csv,
encoding = "UTF-8", # learned from RTFM, but works only on Win7
sep = ";", dec = ",", # csv defaults: , & . but Flattr uses "European" style
stringsAsFactors = FALSE)) # Function structure learned from https://stat.ethz.ch/pipermail/r-help/2010-October/255593.html
}

Sys.setlocale("LC_ALL", "UTF-8") # respect non-ASCII symbols like German umlauts on Mac OSX, learned from https://stackoverflow.com/questions/8145886/

write.table(raw, "flattr-revenue-raw.csv", sep = ";", dec = ",")

# append 1st days to months & convert to date format; learned from http://stackoverflow.com/a/4594269
raw$period <- as.Date(paste(raw$period, "-01"), format="%Y-%m -%d")
raw$EUR_per_click <- raw$revenue / raw$clicks
Expand Down Expand Up @@ -163,3 +178,4 @@ export_plot(monthly_domain_plot, "flattr-revenue-months-domain.png")

# restore original working directory; only useful if you use other scripts in parallel => comment out with # while tinkering with this script, or the files won't be exported to your Flattr folder
setwd(original_wd)

0 comments on commit 48f1930

Please sign in to comment.