Skip to content

Commit e457d99

Browse files
committed
Make separate examples proper R-style demos
Change-Id: I0ac284cfc1d0c508030c91189f260299680c1485
1 parent 8e05d9c commit e457d99

9 files changed

+48
-8
lines changed

.Rbuildignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
^.*\.Rproj$
22
^\.Rproj\.user$
33
^Readme.md
4-
^examples/

Readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ library(RKorAPClient)
2727
kqo <- corpusQuery(new("KorAPConnection", verbose=TRUE), "Hello world")
2828
fetchAll(kqo)
2929
```
30-
## Examples
30+
## Demos
3131

32-
More elaborate R scripts demonstrating the use of the package can be found in the [examples](examples) folder.
32+
More elaborate R scripts demonstrating the use of the package can be found in the [demo](demo) folder.
3333

3434
## Development and License
3535

demo/00Index

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
frequenciesOverTime Plot frequency of query expressions over time
2+
frequenciesOverDomains Box plot frequency of query expressions per topic domain
3+
conditionsOverTime Plot frequency of query expressions over time under different conditions
4+
alternativesOverTime Plot proportion of alternative spellings/variants over time
5+
regional Map plot regional frequencies of query expression
File renamed without changes.

examples/simple/conditionsOverTime.R renamed to demo/conditionsOverTime.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ conditionsOverTime <- function(query, conditions, years, kco = new("KorAPConnect
2626
geom_ribbon(aes(ymin=ci[, 1], ymax=ci[, 2], fill=condition, color=condition), alpha=.3, linetype=0) +
2727
xlab("TIME") +
2828
labs(color="Virtual Corpus", fill="Virtual Corpus") +
29-
ylab(sprintf("Observed frequency of “%s”", query)) +
29+
ylab(sprintf("Observed frequency of \u201c%s\u201d", query)) +
3030
theme(axis.text.x = element_text(angle = 45, hjust = 1)) + scale_x_continuous(breaks=unique(df$year))
3131
print(g)
3232
# print(ggplotly(g, tooltip = c("x", "y")))
File renamed without changes.

examples/simple/frequenciesOverDomains.R renamed to demo/frequenciesOverDomains.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ freqPerDomain <- function(query, con = new("KorAPConnection", verbose = TRUE)) {
2121
g <- ggplot(data = df, mapping = aes(x = Domain, y = freq)) +
2222
geom_col() +
2323
geom_errorbar(aes(ymin=ci[, 1], ymax=ci[, 2]), width=.5, alpha=.5) +
24-
ylab(sprintf("Observed frequency of “%s”", query)) +
24+
ylab(sprintf("Observed frequency of \u201c%s\u201d", query)) +
2525
theme(axis.text.x = element_text(angle = 45, hjust = 1))
2626
print(g)
2727
df

demo/frequenciesOverTime.R

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env Rscript
2+
#
3+
# Plot frequency of query expressions over time
4+
#
5+
library(RKorAPClient)
6+
library(ggplot2)
7+
8+
freqPerYear <- function(query, con = new("KorAPConnection", verbose = TRUE)) {
9+
vc <- "pubDate since 2000 & pubDate until 2018 & textType = /Zeit.*/"
10+
q <- corpusQuery(con, query = query, vc=vc)
11+
q <- fetchAll(q)
12+
tokensPerYear <- function(year) {
13+
return(corpusStats(con, sprintf("%s & pubDate in %s", vc, year))@tokens)
14+
}
15+
df <- as.data.frame(table(as.numeric(format(q@collectedMatches$pubDate,"%Y")), dnn="year"),
16+
stringsAsFactors = FALSE)
17+
df <- merge(data.frame(year=min(df$year):max(df$year)), df, all = TRUE)
18+
df[is.na(df$Freq),]$Freq <- 0
19+
df$total <- sapply(df$year, tokensPerYear)
20+
df$freq <- df$Freq / df$total
21+
df$ci <- t(sapply(Map(prop.test, df$Freq, df$total), "[[","conf.int"))
22+
g <- ggplot(data = df, aes(x = year, y = freq, group=1)) +
23+
geom_ribbon(aes(ymin=ci[, 1], ymax=ci[, 2]), alpha=.3) +
24+
geom_point() +
25+
geom_line() +
26+
xlab("TIME") +
27+
ylab(sprintf("Observed frequency of \u201c%s\u201d", query)) +
28+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
29+
print(g)
30+
df
31+
}
32+
#df <- freqPerYear("Car-Bikini")
33+
#df <- freqPerYear("[tt/p=ART & opennlp/p=ART] [tt/l=teilweise] [tt/p=NN]")
34+
df <- freqPerYear("Buschzulage")
35+

examples/geo/regional.R renamed to demo/regional.R

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ library(broom)
66
library(plotly)
77
library(htmlwidgets)
88

9-
mapfile <- "examples/geo/data/cache/map-v2.rds"
9+
devAskNewPage(ask = FALSE)
10+
mapfile <- "demo/data/cache/map-v2.rds"
1011

1112
fetchAndPrepareMap <- function(map, pick) {
1213
cat("Downloading GADM map data for ", map, "\n")
@@ -34,7 +35,7 @@ fetchMaps <- function(maps, picks) {
3435
map <- fetchMaps(c("DEU_1", "AUT_0", "CHE_0", "LUX_0", "BEL_3", "ITA_1", "LIE_0"), c(0, 0, 0, 0, 34, 17, 0))
3536

3637
geoDistrib <- function(query, kco = new("KorAPConnection", verbose=TRUE)) {
37-
regions <- readRDS("examples/geo/data/regions.rds")
38+
regions <- readRDS("demo/data/regions.rds")
3839
regions$freq <- NA
3940
regions$url <- NA
4041
plot <- NULL
@@ -84,7 +85,7 @@ updatePlot <- function(query, map, regions) {
8485
axis.title.x=element_blank(),
8586
axis.title.y=element_blank()) +
8687
coord_equal(ratio=1.5) +
87-
labs(title = sprintf("Regional distribution of “%s”", query))
88+
labs(title = sprintf("Regional distribution of \u201c%s\u201d", query))
8889
print(regionsPlot)
8990
regionsPlot
9091
}

0 commit comments

Comments
 (0)