Marc Kupietz | 6ac6507 | 2023-11-17 20:31:39 +0100 | [diff] [blame^] | 1 | library(shiny) |
| 2 | library(shinythemes) |
| 3 | library(highcharter) |
| 4 | library(RKorAPClient) |
| 5 | library(tidyverse) |
| 6 | library(idsThemeR) |
| 7 | |
| 8 | ui <- fluidPage( |
| 9 | |
| 10 | theme = shinytheme("paper"), |
| 11 | fluidRow( |
| 12 | column(width = 6, highchartOutput("country")), |
| 13 | column(width = 6, highchartOutput("domain")), |
| 14 | column(width = 6, highchartOutput("decade")), |
| 15 | column(width = 6, highchartOutput("texttype")), |
| 16 | ) %>% tagAppendAttributes(class="hc-link-legend") |
| 17 | |
| 18 | ) |
| 19 | |
| 20 | server <- function(input, output, session) { |
| 21 | |
| 22 | observe({ |
| 23 | query <- parseQueryString(session$clientData$url_search) |
| 24 | if (!is.null(query[['cq']])) { |
| 25 | message(query[['cq']]) |
| 26 | } |
| 27 | }) |
| 28 | |
| 29 | sharelegend = JS('function(event){ |
| 30 | var vis = this.visible; |
| 31 | var conall = $(this.chart.container).parents(".hc-link-legend").find("div.highchart"); |
| 32 | for(var i = 0; i < conall.length; i++){ |
| 33 | var hc = $(conall[i]).highcharts(); |
| 34 | var series = hc.series[this.index]; |
| 35 | if(series){ |
| 36 | if(vis){ |
| 37 | series.hide(); |
| 38 | } else{ |
| 39 | series.show(); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | return false; |
| 44 | }') |
| 45 | |
| 46 | corpus=c("", "referTo ratskorpus-2023-1 & ", "referTo drukola.20180909.1b_words & ") |
| 47 | kco <- new("KorAPConnection", verbose=TRUE) |
| 48 | highchart <- function(...) { |
| 49 | highcharter::highchart() %>% |
| 50 | hc_add_theme(hc_theme_ids_light()) %>% |
| 51 | hc_add_onclick_korap_search() %>% |
| 52 | hc_plotOptions(series = list(events = list(legendItemClick = sharelegend))) |
| 53 | } |
| 54 | |
| 55 | prettifyCorpusNames <- function(df) { |
| 56 | rownames(df) = NULL |
| 57 | df %>% |
| 58 | mutate(corpus = corpus %>% str_replace("referTo *", "") %>% str_replace(" *& *$", "") |> str_replace("^ *$", "DeReKo-KorAP")) |
| 59 | |
| 60 | } |
| 61 | |
| 62 | |
| 63 | output$country <- renderHighchart({ |
| 64 | |
| 65 | countries <- c("DE", "AT", "CH", "IT", "BE", "LU") %>% sort() |
| 66 | |
| 67 | df <- expand_grid(corpus=corpus, country=countries) %>% |
| 68 | mutate(vc = sprintf("%spubPlaceKey=%s", corpus, country)) %>% |
| 69 | prettifyCorpusNames() %>% |
| 70 | bind_cols(corpusStats(kco, .$vc) %>% select(-vc)) |
| 71 | |
| 72 | highchart() %>% |
| 73 | hc_add_series(type = "column", data = df, hcaes(x=country, y=tokens, group=corpus)) %>% |
| 74 | hc_xAxis(categories = df$country) %>% |
| 75 | hc_yAxis(type = "logarithmic") %>% |
| 76 | hc_legend(enabled=T) %>% |
| 77 | hc_title(text="Land") |
| 78 | |
| 79 | }) |
| 80 | |
| 81 | output$domain <- renderHighchart({ |
| 82 | topics <- |
| 83 | c( |
| 84 | "freizeit-unterhaltung", |
| 85 | "gesundheit-ernaehrung", |
| 86 | "kultur", |
| 87 | "politik", |
| 88 | "sport", |
| 89 | "staat-gesellschaft", |
| 90 | "technik-industrie", |
| 91 | "wissenschaft", |
| 92 | "wirtschaft-finanzen", |
| 93 | "natur-umwelt", |
| 94 | "fiktion" |
| 95 | ) |
| 96 | |
| 97 | df <- expand_grid(corpus=corpus, domain=topics) %>% |
| 98 | mutate(vc = sprintf("%stextClass=%s", corpus, domain)) %>% |
| 99 | bind_cols(corpusStats(kco, .$vc)%>% select(-vc)) %>% |
| 100 | prettifyCorpusNames() |
| 101 | |
| 102 | highchart() %>% |
| 103 | hc_add_series(type = "bar", data = df, hcaes(domain, tokens, group=corpus)) %>% |
| 104 | hc_xAxis(categories = df$domain %>% str_to_title(locale = "en") )%>% |
| 105 | hc_yAxis(type = "logarithmic") %>% |
| 106 | hc_legend(enabled=F) %>% |
| 107 | hc_title(text="Thema") |
| 108 | |
| 109 | }) |
| 110 | |
| 111 | output$decade <- renderHighchart({ |
| 112 | decades <- |
| 113 | c(1951, 1961, 1971, 1981, 1991, 2001, 2011, 2021) |
| 114 | decade_labels <- function(start_year) { |
| 115 | sprintf("%d-%d", start_year, start_year+9) |
| 116 | } |
| 117 | |
| 118 | df <- expand_grid(corpus=corpus, decade=decades) %>% |
| 119 | mutate(vc = sprintf("%spubDate since %d & pubDate until %d", corpus, decade, decade+9)) %>% |
| 120 | bind_cols(corpusStats(kco, .$vc) %>% select(-vc)) %>% |
| 121 | mutate(decade = decade_labels(decade)) %>% |
| 122 | prettifyCorpusNames() |
| 123 | |
| 124 | highchart() %>% |
| 125 | hc_add_series(type = "bar", data = df, hcaes(decade, tokens, group=corpus)) %>% |
| 126 | hc_xAxis(categories = df$decade )%>% |
| 127 | hc_yAxis(type = "logarithmic") %>% |
| 128 | hc_legend(enabled=F) %>% |
| 129 | hc_title(text="Dekade") |
| 130 | }) |
| 131 | |
| 132 | output$texttype <- renderHighchart({ |
| 133 | texttypes <- |
| 134 | c("/Zeitung.*/", "/(Zeitschrift|Magazin).*/", "/Agenturmeldung.*/", "/Enzyklopädie.*/", "/.*Diskussion.*/", "/Roman.*/", "/Newsgroup.*/", "/Tagebuch.*/", "/Sachbuch.*/") |
| 135 | |
| 136 | df <- expand_grid(corpus=corpus, texttype=texttypes) %>% |
| 137 | mutate(vc = sprintf("%stextType=%s", corpus, texttype)) %>% |
| 138 | bind_cols(corpusStats(kco, .$vc) %>% select(-vc)) %>% |
| 139 | prettifyCorpusNames() |
| 140 | |
| 141 | hc <- highchart() %>% |
| 142 | hc_add_series(type = "bar", data = df, hcaes(texttype, tokens, group=corpus)) %>% |
| 143 | hc_xAxis(categories = df$texttype %>% str_replace_all("[/.*)()]", "") %>% str_replace_all("\\|", "/")) %>% |
| 144 | hc_yAxis(type = "logarithmic") %>% |
| 145 | hc_legend(enabled=F) %>% |
| 146 | hc_title(text="Texttyp") |
| 147 | hc |
| 148 | }) |
| 149 | |
| 150 | } |
| 151 | |
| 152 | shinyApp(ui, server) |