blob: 98b6f0e1d54131597cfcf3639061e5d888648219 [file] [log] [blame]
Marc Kupietz6ac65072023-11-17 20:31:39 +01001library(shiny)
2library(shinythemes)
3library(highcharter)
4library(RKorAPClient)
5library(tidyverse)
Rainer Perkuhn0f5d1952023-12-06 10:59:12 +01006
7#library(devtools)
8#install_git("https://korap.ids-mannheim.de/gerrit/IDS-Mannheim/idsThemeR")
9
Marc Kupietz6ac65072023-11-17 20:31:39 +010010library(idsThemeR)
11
Marc Kupietzf6bb6cb2023-11-18 17:17:50 +010012corpus=c("", "referTo ratskorpus-2023-1", "referTo drukola.20180909.1b_words")
13
14vcFromString <- function(string) {
15 string %>%
16 str_split_1(" *[;] *") %>%
17 str_replace("^(.+)$", "(\\1) & ") %>%
18 str_replace_all(" +", " ")
19}
20
Marc Kupietze52fef32023-11-22 20:33:24 +010021hc_add_log_linear_toggle <- function(hc, index=50) {
22 hc_add_series(hc, name="[toggle log/linear]", legendIndex=index, visible=TRUE, type="spline", color="white") %>%
23
24 hc_plotOptions(spline = list(
25 events = list(legendItemClick = JS("
26 function() {
27 var conall = $(this.chart.container).parents('.hc-link-legend').find('div.highchart');
28 for(var i = 0; i < conall.length; i++) {
29 var hc = $(conall[i]).highcharts();
30 hc.yAxis[0].update({type: hc.yAxis[0].options['type']=='logarithmic' ? 'linear' : 'logarithmic'});
31 }
32 }
33 "))
34 ))
35}
36
37
Marc Kupietz6ac65072023-11-17 20:31:39 +010038ui <- fluidPage(
39
40 theme = shinytheme("paper"),
Marc Kupietzf6bb6cb2023-11-18 17:17:50 +010041 fluidRow(column(width = 12, textInput("cq", "Corpus definitions", paste0(corpus, collapse = ";"), width="100%"))),
Marc Kupietz6ac65072023-11-17 20:31:39 +010042 fluidRow(
43 column(width = 6, highchartOutput("country")),
44 column(width = 6, highchartOutput("domain")),
45 column(width = 6, highchartOutput("decade")),
46 column(width = 6, highchartOutput("texttype")),
47 ) %>% tagAppendAttributes(class="hc-link-legend")
48
49)
50
51server <- function(input, output, session) {
52
Marc Kupietzf6bb6cb2023-11-18 17:17:50 +010053# corpus <- str_split(input$corpus, ",")
54# corpus <- corpus %>% str_replace("^(.+)$", "\\1 & ")
55
Marc Kupietz6ac65072023-11-17 20:31:39 +010056 observe({
57 query <- parseQueryString(session$clientData$url_search)
58 if (!is.null(query[['cq']])) {
Marc Kupietzf6bb6cb2023-11-18 17:17:50 +010059 corpus = as.vector(unlist(query))
60 updateTextInput(session, "cq", value = corpus)
Marc Kupietz6ac65072023-11-17 20:31:39 +010061 }
62 })
63
64 sharelegend = JS('function(event){
65 var vis = this.visible;
66 var conall = $(this.chart.container).parents(".hc-link-legend").find("div.highchart");
67 for(var i = 0; i < conall.length; i++){
68 var hc = $(conall[i]).highcharts();
69 var series = hc.series[this.index];
70 if(series){
71 if(vis){
72 series.hide();
73 } else{
74 series.show();
75 }
76 }
77 }
78 return false;
79 }')
80
Marc Kupietz6ac65072023-11-17 20:31:39 +010081 kco <- new("KorAPConnection", verbose=TRUE)
82 highchart <- function(...) {
83 highcharter::highchart() %>%
84 hc_add_theme(hc_theme_ids_light()) %>%
85 hc_add_onclick_korap_search() %>%
Marc Kupietzf6bb6cb2023-11-18 17:17:50 +010086 hc_yAxis(type = "logarithmic") %>%
87 hc_legend(enabled=F) %>%
Marc Kupietze52fef32023-11-22 20:33:24 +010088 hc_plotOptions(series = list(events = list(legendItemClick = sharelegend))) %>%
89 hc_add_log_linear_toggle()
Marc Kupietz6ac65072023-11-17 20:31:39 +010090 }
91
92 prettifyCorpusNames <- function(df) {
93 rownames(df) = NULL
Marc Kupietzf6bb6cb2023-11-18 17:17:50 +010094
Marc Kupietz6ac65072023-11-17 20:31:39 +010095 df %>%
Marc Kupietzf6bb6cb2023-11-18 17:17:50 +010096 mutate(corpus = corpus %>% str_replace("referTo *", "") %>% str_replace(" *& *$", "") %>%
97 str_replace_all("[)()]", "") %>%
98 str_replace("^ *$", "DeReKo-KorAP"))
Marc Kupietz6ac65072023-11-17 20:31:39 +010099
100 }
101
102
103 output$country <- renderHighchart({
Marc Kupietzf6bb6cb2023-11-18 17:17:50 +0100104 corpus <- vcFromString(input$cq)
Marc Kupietz6ac65072023-11-17 20:31:39 +0100105
106 countries <- c("DE", "AT", "CH", "IT", "BE", "LU") %>% sort()
107
108 df <- expand_grid(corpus=corpus, country=countries) %>%
109 mutate(vc = sprintf("%spubPlaceKey=%s", corpus, country)) %>%
110 prettifyCorpusNames() %>%
111 bind_cols(corpusStats(kco, .$vc) %>% select(-vc))
112
113 highchart() %>%
114 hc_add_series(type = "column", data = df, hcaes(x=country, y=tokens, group=corpus)) %>%
115 hc_xAxis(categories = df$country) %>%
Marc Kupietz6ac65072023-11-17 20:31:39 +0100116 hc_legend(enabled=T) %>%
117 hc_title(text="Land")
Marc Kupietz6ac65072023-11-17 20:31:39 +0100118 })
119
120 output$domain <- renderHighchart({
Marc Kupietzf6bb6cb2023-11-18 17:17:50 +0100121 corpus <- vcFromString(input$cq)
122
Marc Kupietz6ac65072023-11-17 20:31:39 +0100123 topics <-
124 c(
125 "freizeit-unterhaltung",
126 "gesundheit-ernaehrung",
127 "kultur",
128 "politik",
129 "sport",
130 "staat-gesellschaft",
131 "technik-industrie",
132 "wissenschaft",
133 "wirtschaft-finanzen",
134 "natur-umwelt",
135 "fiktion"
136 )
137
138 df <- expand_grid(corpus=corpus, domain=topics) %>%
139 mutate(vc = sprintf("%stextClass=%s", corpus, domain)) %>%
140 bind_cols(corpusStats(kco, .$vc)%>% select(-vc)) %>%
141 prettifyCorpusNames()
142
143 highchart() %>%
144 hc_add_series(type = "bar", data = df, hcaes(domain, tokens, group=corpus)) %>%
145 hc_xAxis(categories = df$domain %>% str_to_title(locale = "en") )%>%
Marc Kupietz6ac65072023-11-17 20:31:39 +0100146 hc_title(text="Thema")
147
148 })
149
150 output$decade <- renderHighchart({
Marc Kupietzf6bb6cb2023-11-18 17:17:50 +0100151 corpus <- vcFromString(input$cq)
Marc Kupietz6ac65072023-11-17 20:31:39 +0100152 decades <-
153 c(1951, 1961, 1971, 1981, 1991, 2001, 2011, 2021)
154 decade_labels <- function(start_year) {
155 sprintf("%d-%d", start_year, start_year+9)
156 }
157
158 df <- expand_grid(corpus=corpus, decade=decades) %>%
159 mutate(vc = sprintf("%spubDate since %d & pubDate until %d", corpus, decade, decade+9)) %>%
160 bind_cols(corpusStats(kco, .$vc) %>% select(-vc)) %>%
161 mutate(decade = decade_labels(decade)) %>%
162 prettifyCorpusNames()
163
164 highchart() %>%
165 hc_add_series(type = "bar", data = df, hcaes(decade, tokens, group=corpus)) %>%
166 hc_xAxis(categories = df$decade )%>%
Marc Kupietz6ac65072023-11-17 20:31:39 +0100167 hc_title(text="Dekade")
168 })
169
170 output$texttype <- renderHighchart({
Marc Kupietzf6bb6cb2023-11-18 17:17:50 +0100171 corpus <- vcFromString(input$cq)
172
Marc Kupietz6ac65072023-11-17 20:31:39 +0100173 texttypes <-
Rainer Perkuhn74c98c82023-12-06 10:28:55 +0100174 c("/[^:]*[Zz]eitung.*/", "/(Zeitschrift|Magazin).*/", "/Agenturmeldung.*/", "/Enzyklopädie.*/", "/.*Diskussion.*/", "/.*[Rr]oman([^z].*|$)/", "/Newsgroup.*/", "/Tagebuch.*/", "/.*Sachbuch.*/")
Marc Kupietz6ac65072023-11-17 20:31:39 +0100175
176 df <- expand_grid(corpus=corpus, texttype=texttypes) %>%
177 mutate(vc = sprintf("%stextType=%s", corpus, texttype)) %>%
178 bind_cols(corpusStats(kco, .$vc) %>% select(-vc)) %>%
179 prettifyCorpusNames()
180
181 hc <- highchart() %>%
182 hc_add_series(type = "bar", data = df, hcaes(texttype, tokens, group=corpus)) %>%
Marc Kupietz660bff82023-12-05 14:45:53 +0100183 hc_xAxis(categories = df$texttype %>%
184 str_replace_all("Zz", "Z") %>%
185 str_replace_all("[/.*)():^\\[\\]]", "") %>% str_replace_all("\\|", "/")) %>%
Marc Kupietz6ac65072023-11-17 20:31:39 +0100186 hc_title(text="Texttyp")
187 hc
188 })
189
190}
191
192shinyApp(ui, server)