Marc Kupietz | a6e4ee6 | 2021-03-05 09:00:15 +0100 | [diff] [blame^] | 1 | #' Helper functions for producing highcharts |
| 2 | #' |
| 3 | #' @param hc highchart |
| 4 | #' |
| 5 | #' @name highcharter-helpers |
| 6 | NULL |
| 7 | #' NULL |
| 8 | |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 9 | #' Experimental: Plot interactive frequency by year graphs with confidence intervals using highcharter |
| 10 | #' |
| 11 | #' Experimental convenience function for plotting typical frequency by year graphs with confidence intervals using highcharter. |
| 12 | #' \bold{Warning:} This function may be moved to a new package. |
| 13 | #' |
Marc Kupietz | a6e4ee6 | 2021-03-05 09:00:15 +0100 | [diff] [blame^] | 14 | #' @rdname highcharter-helpers |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 15 | #' @import highcharter |
Marc Kupietz | b7e7f72 | 2020-06-02 12:29:18 +0200 | [diff] [blame] | 16 | #' @importFrom tibble add_column |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 17 | #' @export |
| 18 | #' |
| 19 | #' @param df data frame like the value of a \code{\link{frequencyQuery}} |
Marc Kupietz | 43a6ade | 2020-02-18 17:01:44 +0100 | [diff] [blame] | 20 | #' @param as.alternatives boolean decides whether queries should be treated as mutually exclusive and exhaustive wrt. to some meaningful class (e.g. spelling variants of a certain word form). |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 21 | #' @param ylabel defaults to \% if \code{as.alternatives} is \code{true} and to "ipm" otherwise. |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 22 | #' @param smooth boolean decides whether the graph is smoothed using the highcharts plot types spline and areasplinerange. |
Marc Kupietz | 5b503f4 | 2020-04-09 15:26:00 +0200 | [diff] [blame] | 23 | #' @param ... additional arguments passed to \code{\link{hc_add_series}} |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 24 | #' |
| 25 | #' @examples |
Marc Kupietz | 657d8e7 | 2020-02-25 18:31:50 +0100 | [diff] [blame] | 26 | #' \donttest{year <- c(1990:2018)}\dontshow{year <- c(2013:2013)} |
| 27 | #' \donttest{alternatives <- c("macht []{0,3} Sinn", "ergibt []{0,3} Sinn")}\dontshow{alternatives <- c("macht []{0,3} Sinn")} |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 28 | #' new("KorAPConnection", verbose = TRUE) %>% |
Marc Kupietz | 657d8e7 | 2020-02-25 18:31:50 +0100 | [diff] [blame] | 29 | #' frequencyQuery(query = alternatives, |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 30 | #' vc = paste("textType = /Zeit.*/ & pubDate in", year), |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 31 | #' as.alternatives = TRUE) %>% |
| 32 | #' hc_freq_by_year_ci(as.alternatives = TRUE) |
| 33 | #' |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 34 | #' \donttest{ |
Marc Kupietz | 10f65c4 | 2020-01-31 15:18:24 +0100 | [diff] [blame] | 35 | #' kco <- new("KorAPConnection", verbose = TRUE) |
| 36 | #' expand_grid( |
| 37 | #' condition = c("textDomain = /Wirtschaft.*/", "textDomain != /Wirtschaft.*/"), |
| 38 | #' year = (2005:2011) |
| 39 | #' ) %>% |
| 40 | #' cbind(frequencyQuery( |
| 41 | #' kco, |
| 42 | #' "[tt/l=Heuschrecke]", |
| 43 | #' paste0(.$condition, " & pubDate in ", .$year) |
| 44 | #' )) %>% |
| 45 | #' hc_freq_by_year_ci() |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 46 | #' } |
Marc Kupietz | 10f65c4 | 2020-01-31 15:18:24 +0100 | [diff] [blame] | 47 | #' |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 48 | hc_freq_by_year_ci <- function(df, as.alternatives = FALSE, |
| 49 | ylabel = if(as.alternatives) "%" else "ipm", |
| 50 | smooth = FALSE, |
| 51 | ...) { |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 52 | title <- "" |
| 53 | df <- df %>% |
| 54 | { if(! as.alternatives) ipm(.) else RKorAPClient::percent(.) } |
| 55 | |
| 56 | if (!"year" %in% colnames(df)) { |
Marc Kupietz | b7e7f72 | 2020-06-02 12:29:18 +0200 | [diff] [blame] | 57 | df <- df %>% add_column(year = as.integer(queryStringToLabel(df$vc, pubDateOnly = TRUE))) |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 58 | } |
| 59 | if (!"condition" %in% colnames(df)) { |
| 60 | if (length(base::unique(df$query)) > 1) { |
| 61 | df <- df %>% mutate(condition = query) |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 62 | if(length(base::unique(queryStringToLabel(df$vc, excludePubDate = TRUE ))) > 1) { |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 63 | df <- df %>% mutate(condition = paste(condition, " & ", |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 64 | queryStringToLabel(vc, excludePubDate = TRUE ))) |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 65 | } |
| 66 | } else { |
Marc Kupietz | 5d70ffe | 2020-03-12 11:16:43 +0100 | [diff] [blame] | 67 | if (length(base::unique(queryStringToLabel(df$vc, excludePubDate = TRUE ))) > 1) { |
| 68 | title <- base::unique(df$query) |
Marc Kupietz | b7e7f72 | 2020-06-02 12:29:18 +0200 | [diff] [blame] | 69 | df <- df %>% add_column(condition = queryStringToLabel(vc, excludePubDate = TRUE )) |
Marc Kupietz | 5d70ffe | 2020-03-12 11:16:43 +0100 | [diff] [blame] | 70 | } else { |
| 71 | df <- df %>% mutate(condition = query) |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | } |
| 75 | # use the D3 palette which provides 20 attractive and distinguishable colours |
| 76 | palette <- c("#1F77B4", "#FF7F0E", "#2CA02C", "#D62728", "#9467BD", "#8C564B", "#E377C2", "#7F7F7F", "#BCBD22", "#17BECF", "#AEC7E8", "#FFBB78", "#98DF8A", "#FF9896", "#C5B0D5", "#C49C94", "#F7B6D2", "#C7C7C7", "#DBDB8D", "#9EDAE5") |
| 77 | highcharter::highchart() %>% |
| 78 | hc_title(text=title) %>% |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 79 | hc_yAxis( |
| 80 | title = list(text = if (as.alternatives) "" else ylabel), |
| 81 | ceiling = if (as.alternatives) 100 else NULL, |
| 82 | floor = 0, |
| 83 | labels = if(as.alternatives) list(format = paste0("{value}\U2009", ylabel)) else NULL |
| 84 | ) %>% |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 85 | hc_xAxis(allowDecimals=FALSE) %>% |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 86 | hc_add_theme(hc_theme_google(colors=palette)) %>% |
Marc Kupietz | e203832 | 2021-03-04 18:24:02 +0100 | [diff] [blame] | 87 | hc_add_onclick_korap_search() %>% |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 88 | hc_credits(enabled = TRUE, |
Marc Kupietz | 900d052 | 2020-03-19 13:36:45 +0100 | [diff] [blame] | 89 | text = "KorAP R Client Package", |
| 90 | href = "https://github.com/KorAP/RKorAPClient/") %>% |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 91 | hc_exporting(enabled = TRUE) %>% |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 92 | hc_tooltip( |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 93 | headerFormat = '<span style="font-size: 10pt">{point.key}</span><br/>', |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 94 | formatter = JS(paste0("function (tooltip) { |
| 95 | var str = tooltip.defaultFormatter.call(this, tooltip); |
| 96 | if(Array.isArray(str)) { |
| 97 | str = str.join(''); |
| 98 | } |
| 99 | for (var i = 0; i < this.points.length; i++) { |
| 100 | str = str.replace(/([0-9.,]+.?)", ylabel, "/, this.points[i].point.count+' ($1@)'); |
| 101 | } |
| 102 | return str.replace(/@/g, '", ylabel, "') |
| 103 | } ")), |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 104 | crosshairs = TRUE, |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 105 | valueDecimals = 2, |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 106 | shared = TRUE, |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 107 | valueSuffix = paste0('\U2009', ylabel) |
| 108 | ) %>% |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 109 | hc_add_series_korap_frequencies(df, smooth, as.alternatives, ...) |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Marc Kupietz | a4f3653 | 2020-02-03 22:50:08 +0100 | [diff] [blame] | 112 | ## Mute notes: "no visible binding for global variable:" |
| 113 | globalVariables(c("value", "query", "condition", "vc")) |
| 114 | |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 115 | hc_add_series_korap_frequencies <- function(hc, df, smooth = FALSE, |
| 116 | as.alternatives = FALSE, |
| 117 | ...) { |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 118 | index <- 0 |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 119 | type <- ifelse(smooth, "spline", "line") |
| 120 | areatype <- ifelse(smooth, "areasplinerange", "arearange") |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 121 | for(q in unique(df$condition)) { |
| 122 | dat <- df[df$condition==q,] |
| 123 | hc <- hc %>% hc_add_series( |
| 124 | marker = list(radius = 2), |
| 125 | name = q, |
| 126 | data = data.frame( |
| 127 | year = dat$year, |
| 128 | value = if (as.alternatives) dat$f else dat$ipm, |
| 129 | count = dat$totalResults, |
Marc Kupietz | e203832 | 2021-03-04 18:24:02 +0100 | [diff] [blame] | 130 | webUIRequestUrl = dat$webUIRequestUrl |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 131 | ), |
| 132 | hcaes(year, value), |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 133 | type = type, |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 134 | colorIndex = index, |
Marc Kupietz | 5b503f4 | 2020-04-09 15:26:00 +0200 | [diff] [blame] | 135 | zIndex = 1, |
| 136 | ... |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 137 | ) %>% |
| 138 | hc_add_series( |
| 139 | name = "ci", |
| 140 | data = dat[,c('year', 'conf.low', 'conf.high')], |
| 141 | hcaes(x = year, low = conf.low, high = conf.high), |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 142 | type = areatype, |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 143 | fillOpacity = 0.3, |
| 144 | lineWidth = 0, |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 145 | marker = list(enabled = FALSE), |
| 146 | enableMouseTracking = FALSE, |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 147 | linkedTo= ':previous', |
| 148 | colorIndex = index, |
| 149 | zIndex = 0 |
| 150 | ) |
| 151 | index <- index+1 |
| 152 | } |
| 153 | hc |
| 154 | } |
Marc Kupietz | 882f08c | 2020-03-18 13:30:31 +0100 | [diff] [blame] | 155 | |
Marc Kupietz | e203832 | 2021-03-04 18:24:02 +0100 | [diff] [blame] | 156 | #' Add KorAP search click events to highchart |
| 157 | #' |
| 158 | #' @description |
| 159 | #' Adds on-click events to data points of highcarts that were constructed with |
Marc Kupietz | a6e4ee6 | 2021-03-05 09:00:15 +0100 | [diff] [blame^] | 160 | #' \code{\link{frequencyQuery}} or \code{\link{collocationScoreQuery}}. Clicks on data points |
Marc Kupietz | e203832 | 2021-03-04 18:24:02 +0100 | [diff] [blame] | 161 | #' then launch KorAP web UI queries for the given query term and virtual corpus in |
Marc Kupietz | a6e4ee6 | 2021-03-05 09:00:15 +0100 | [diff] [blame^] | 162 | #' a separate tab. |
Marc Kupietz | e203832 | 2021-03-04 18:24:02 +0100 | [diff] [blame] | 163 | #' |
Marc Kupietz | a6e4ee6 | 2021-03-05 09:00:15 +0100 | [diff] [blame^] | 164 | #' @rdname highcharter-helpers |
Marc Kupietz | e203832 | 2021-03-04 18:24:02 +0100 | [diff] [blame] | 165 | #' @export |
| 166 | #' |
| 167 | #' @examples |
| 168 | #' \donttest{ |
| 169 | #' library(highcharter) |
| 170 | #' library(tidyr) |
| 171 | #' |
| 172 | #' new("KorAPConnection", verbose = TRUE) %>% |
| 173 | #' collocationScoreQuery("Team", "agil", vc = paste("pubDate in", c(2014:2018)), |
| 174 | #' lemmatizeNodeQuery = TRUE, lemmatizeCollocateQuery = TRUE) %>% |
| 175 | #' pivot_longer(c("O", "E")) %>% |
| 176 | #' hchart(type="spline", hcaes(label, value, group=name)) %>% |
| 177 | #' hc_add_onclick_korap_search() |
| 178 | #' } |
| 179 | #' |
| 180 | hc_add_onclick_korap_search <- function(hc) { |
| 181 | hc_plotOptions( |
| 182 | hc, |
| 183 | series = list(enabled = TRUE), |
| 184 | spline = list(cursor = 'pointer', point = list(events = list( |
| 185 | click = JS("function() { window.open(this.webUIRequestUrl, 'korap'); }") |
| 186 | ))), |
| 187 | line = list(cursor = 'pointer', point = list(events = list( |
| 188 | click = JS("function() { window.open(this.webUIRequestUrl, 'korap'); }") |
| 189 | )))) |
| 190 | } |
| 191 | |
Marc Kupietz | 882f08c | 2020-03-18 13:30:31 +0100 | [diff] [blame] | 192 | .onAttach <- function(libname = find.package("RKorAPClient"), |
| 193 | pkgname = "RKorAPClient") { |
| 194 | packageStartupMessage( |
| 195 | "If you intend to use the Highcharts plot options, please note that Highcharts (www.highcharts.com) is a Highsoft software product which is not free for commercial and governmental use." |
| 196 | ) |
| 197 | } |
| 198 | |