-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyPackagePopularity_MGBT.R
More file actions
127 lines (113 loc) · 4.54 KB
/
Copy pathMyPackagePopularity_MGBT.R
File metadata and controls
127 lines (113 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
need_to_see_srcfile <- "MyPackagePopularity_MGBT.R"
if("rstudioapi" %in% installed.packages()) {
if(rstudioapi::isAvailable()) {
if(! file.exists(need_to_see_srcfile)) {
setwd(dirname(rstudioapi::getSourceEditorContext()$path))
}
}
}
the.pkg <- "MGBT"
the.origin <- "2019-10-31"
options(repos = c(CRAN = "http://cran.rstudio.com"))
# Here's an easy way to get all the URLs in R
start <- as.Date('2021-10-17'); #start <- as.Date(the.origin)
today <- as.Date('2022-02-03')
all_days <- seq(start, today, by='day')
year <- as.POSIXlt(all_days)$year + 1900
files <- paste0(all_days, '.csv.gz')
urls <- paste0('http://cran-logs.rstudio.com/', year, '/', all_days, '.csv.gz')
# If you only want to download the files you don't have, try:
missing_days <- setdiff(all_days, tools::file_path_sans_ext(dir(), TRUE))
m <- length(all_days)
for(i in 1:m) {
if(file.exists(files[i])) {
message("skipping ",files[i])
next
}
try(download.file(urls[i], files[i]))
}
NAs <- rep(NA, m)
AP <- data.frame(date=NAs, PKG_rnk=NAs,
PKG_pct=NAs, PKG_cnt=NAs, PKG_countries=NAs,
total_cnt=NAs)
for(i in 1:m) {
if(! file.exists(files[i])) next
system(paste0("gzcat ",files[i]," > tmp.txt"))
df <- read.table("tmp.txt", sep=",", header=TRUE)
df <- df[! is.na(df$package),]
P <- aggregate(df$package, by=list(df$package), length)
names(P) <- c("package", "count")
P <- P[order(P$count, decreasing=FALSE),]; n <- length(P$count)
P$rank <- 1:n
dflm <- df[df$package == the.pkg, ]
Cl <- length(unique(dflm$country))
PKG <- P[P$package == the.pkg, ]
if(length(PKG$count) == 0) {
PKG <- list(count=0, rank=NA, countries=NA)
} else {
PKGBarRank <- mean(P[P$count == PKG$count,]$rank)
PKG$rank <- PKGBarRank
PKG$countries <- Cl
}
message("processing ",i," ",files[i], " ", PKG$count, " with ",
n, " total all CRAN downloads")
AP[i,1] <- as.character(all_days[i])
AP[i,2:6] <- c(PKG$rank,
100*round(PKG$rank/n, digits=4),
PKG$count, PKG$countries, n)
unlink("tmp.txt")
}
AP[,1] <- as.Date(AP[,1])
Package <- AP
save(Package, file=paste0(the.pkg,".RData")); rm(Package)
file <- paste0(the.pkg,"_","20191001_20211016.RData")
if(file.exists(file)) load(file) # Packages is coming back
AP <- merge(Package, AP, all=TRUE)
#Package <- AP
#save(Package, file=paste0(the.pkg,"_","20191001_20211016.RData"))
library(kernlab)
yearize <- 365
tmp <- AP[complete.cases(AP),]
svmy <- ksvm(tmp$PKG_pct~I(as.numeric(tmp$date)/yearize), cross=0, C=.1)
y <- predict(svmy, tmp)
RT <- read.table("timeline_R.txt", header=TRUE, stringsAsFactors=FALSE)
ME <- read.table(paste0("timeline_",the.pkg,".txt"), header=TRUE, stringsAsFactors=FALSE)
RT$time <- as.Date(RT$time)
ME$time <- as.Date(ME$time)
#pdf(paste0(the.pkg,"_PackagePopularity.pdf"), useDingbats=FALSE, width=7, height=6)
par(las=1, lend=1, mgp=c(3,0.5,0))
plot(AP$date, AP$PKG_pct, type="n",
xlab="Date (daily download data from cran-logs.rstudio.com)", tcl=0.5,
ylab="Rank as percentile against other package downloads (100 is best)", ylim=c(0,100),
xaxs="i", yaxs="i")
suppressWarnings(rug(RT$time, tcl=-0.5, col="#22a524", lwd=3))
for(i in seq(05,95, by=5)) {
lines(par()$usr[1:2], rep(i,2), lty=2, lwd=0.6)
}
for(i in ME$time) {
lines(rep(i,2), c(0,100), col=4, lty=1)
}
ap <- data.frame(date=AP$date, PKG_pct=AP$PKG_pct, PKG_countries=AP$PKG_countries)
ap <- ap[! is.na(ap$date),]; ix <- 1:length(ap$date)
ix <- sample(ix, size=length(ix), replace=FALSE)
ap <- ap[ix,]
ap$col <- rgb(1,.4,0,.5)
ap$cex <- ap$PKG_countries/10
points(ap$date, ap$PKG_pct, cex=ap$cex, col=ap$col, pch=16, lwd=0.4)
lines(tmp$date, y, col=4, lwd=4)
legend(as.Date(the.origin), 100,
c(paste0("Trend line for ",the.pkg," package by kernlab::ksvm(<defaults>)"),
"Release date of R (see outside 'rug' ticks on horizontal axis)",
paste0("Release date of ",the.pkg," (solid and dashed aids viewing when overplotting)"),
paste0(the.pkg," package (L-moments and many distributions) [size {cex}=no. countries/10]")),
pch=c(NA,NA,NA,16), lwd=c(4,3,1,NA), bty="o", box.col=NA, bg=grey(1,.8),
col=c(4,"#22a524",4,rgb(1,.4,0)), cex=0.85,
)
mtext(paste0("TRENDS IN 'GLOBAL' R PACKAGE POPULARITY (",the.pkg,")"))
#dev.off()
m <- length(AP$PKG_pct)
if(m > 180) {
message("Mean last 180 days PKG: ",
round(mean(AP$PKG_pct[ (m-180):m], na.rm=TRUE), digits=1))
}
message("Total package count: ", sum(AP$PKG_cnt, na.rm=TRUE))