Skip to content

Commit c4b8f15

Browse files
KonScience PodcastKonScience Podcast
authored andcommitted
simplified selection of working directory
user is prompted for 1st .csv => script reuses same path to find other files
1 parent 7ef5ef6 commit c4b8f15

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

summarize-flattr-reports.R

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
1-
# Creates Flattr summary from "Monthly Revenue" reports
2-
# Started by Katrin from the KonScience Podcast in Sept. 2014
3-
4-
# IMPORTANT: Adjust folder path that contains all the downloaded .csv files here
5-
# Windows: Please use double backslashes like C:\\User\\YOU...
6-
# Linux & Mac: /Users/YOU/ can be abbreviated as '~/...'
7-
path_to_flattr_reports <- "/Users/YOU/Flattr/"
8-
1+
# READ ME: https://github.com/KonScience/Summarize-Flattr-Reports#summarize-flattr-reports
92

3+
# assumes all .csv files were downloaded into same folder
4+
first_flattr_file <- file.choose()
5+
flattr_dir <- dirname(first_flattr_file) #learned from http://stackoverflow.com/a/18003224
106

117
# saves original working directory and sets new one as provided above
128
original_wd <- getwd()
13-
setwd(path_to_flattr_reports)
9+
setwd(flattr_dir)
1410

1511
# get filenames of Flattr Monthly Revenue CSVs
16-
Flattr_filenames <- list.files(path_to_flattr_reports,
12+
Flattr_filenames <- list.files(flattr_dir,
1713
pattern = "flattr-revenue-[0-9]*.csv"
1814
)
1915

20-
# // TODO find easier way to select path, e.g.
21-
# - auto-use folder where script runs from // TODO adjust ReadMe.md
22-
# - prompt user for path, all files (file.choose selects only one) or 1st file and find others in same folder // TODO remove original_wd code
23-
2416
# read data from CSVs into data frame
2517
raw <- do.call("rbind",
26-
lapply(Flattr_filenames,
27-
read.csv,
28-
sep = ";",
29-
dec = ",", # convert decimal separator from , to . for following calculations
30-
stringsAsFactors = FALSE #
31-
)
32-
) # Function structure learned from https://stat.ethz.ch/pipermail/r-help/2010-October/255593.html
18+
lapply(Flattr_filenames,
19+
read.csv,
20+
sep = ";",
21+
dec = ",", # convert decimal separator from , to . for following calculations
22+
stringsAsFactors = FALSE #
23+
)
24+
) # Function structure learned from https://stat.ethz.ch/pipermail/r-help/2010-October/255593.html
3325

3426
# append 1st days to months & convert to date format
3527
# learned from http://stackoverflow.com/a/4594269
@@ -56,8 +48,8 @@ export_csv <- function(data_source, filename) {
5648
sep = ";",
5749
dec = ",",
5850
row.names = FALSE
59-
)}
60-
51+
)
52+
}
6153

6254
# exports summary to same folder
6355
export_csv(per_thing_ordered, "flattr-revenue-summary.csv")
@@ -75,21 +67,19 @@ per_period <- ddply(raw,
7567

7668
# plots Flattr clicks over time
7769
# // TODO colorize by "title"-category
78-
# // TODO spread elements out randomly over 25 days in their months (leave visible gap to next month)
7970
per_period$EUR_per_click <- (per_period$all_revenue / per_period$all_clicks)
8071
library(ggplot2)
8172
qplot(x = per_period$period,
8273
y = per_period$EUR_per_click,
8374
xlab = "time",
8475
ylab = "EUR per click"
8576
)
86-
ggsave("flattr-revenue-clicks.pdf")
77+
ggsave("flattr-revenue-clicks.png")
8778

8879
# orders by title
8980
per_period_orderd <- per_period[order(per_period$title),]
9081

9182
# export to same folder
92-
# // TODO functionalize export with dataframe & filename objects
9383
export_csv(per_period_orderd, "flattr-revenue-click-value.csv")
9484

9585
# restore original working directory

0 commit comments

Comments
 (0)