R/hc_add_onclick_korap_search.R

Defines functions hc_add_onclick_korap_search

Documented in hc_add_onclick_korap_search

#' Add KorAP search click events to highchart plots
#'
#' @description
#' `r lifecycle::badge("experimental")`
#'
#' Adds on-click events to data points of highcharts that were constructed with
#' [frequencyQuery()] or [collocationScoreQuery()]. Clicks on data points
#' then launch KorAP web UI queries for the given query term and virtual corpus in
#' a separate tab.
#'
#' @family highcharter-helpers
#'
#' @param hc      A highchart htmlwidget object generated by e.g. [frequencyQuery()].
#' @return The input highchart object with added on-click events.
#' @export
#'
#' @examples
#' \dontrun{
#'
#' library(highcharter)
#' library(tidyr)
#'
#' new("KorAPConnection", verbose = TRUE) %>%
#'   collocationScoreQuery("Team", "agil", vc = paste("pubDate in", c(2014:2018)),
#'                         lemmatizeNodeQuery = TRUE, lemmatizeCollocateQuery = TRUE) %>%
#'                          pivot_longer(c("O", "E")) %>%
#'   hchart(type="spline", hcaes(label, value, group=name)) %>%
#'   hc_add_onclick_korap_search()
#' }
#'
hc_add_onclick_korap_search <- function(hc) {
  js <- JS("function() { window.open(this.webUIRequestUrl, 'korap'); }")
  point_events <- list(events = list(click = js))
  onclick_hc_options <- list(cursor = 'pointer', point = point_events)
  hc_plotOptions(
    hc,
    series = list(enabled = TRUE),
    line = onclick_hc_options,
    spline = onclick_hc_options,
    column = onclick_hc_options,
    bar = onclick_hc_options,
    scatter = onclick_hc_options,
    pie = onclick_hc_options)
}
KorAP/RKorAPClient documentation built on Feb. 6, 2024, 2:28 p.m.