Replace old displayKwics demo
Change-Id: Ie4abf629bedd8093555d574976673db6b755b30a
diff --git a/demo/displayKwics.R b/demo/displayKwics.R
index b57aae7..4da6540 100644
--- a/demo/displayKwics.R
+++ b/demo/displayKwics.R
@@ -1,79 +1,24 @@
library(RKorAPClient)
+library(httr)
+library(kableExtra)
+library(dplyr)
-displayKwics <- function(q, htmlFile = file.path(tempfile(fileext = ".html"))) {
- cat(
- '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <!DOCTYPE html>
- <html>
- <header>
- <style>
- body {
- font-family: "Lato", sans-serif;
- font-size: 10pt;
- }
- div.kwic {
- display: table-row;
- line-height: 180%;
- }
- div.kwic:nth-child(even) {background: #EEE}
- div.kwic:nth-child(odd) {background: #FFF}
- .sigle {
- font-size: 90%;
- font-family: "Courier New", monospace;
- display: table-cell;
- text-align: right;
- padding-right: 1ex;
- }
- .context-left {
- display: table-cell;
- text-align: right;
- overflow: hidden;
- white-space: nowrap;
- }
- .context-right {
- display: table-cell;
- text-align: left;
- white-space: nowrap;
- overflow: hidden;
- }
- .match {
- display: table-cell;
- padding-left: 1ex;
- padding-right: 1ex;
- text-align: center;
- }
- </style>
- </header>
- <body>
- <table>',
- paste0(
- "<div class='kwic'><span class='sigle'>",
- q@collectedMatches$textSigle,
- "</span> ",
- as.character(q@collectedMatches$snippet),
- "</div>"
- ),
- ' </table>
- </body>
-</html>',
- file = htmlFile,
- sep = "\n"
- )
- viewer <- getOption("viewer")
- viewer(htmlFile)
-}
+query = 'V\u00F6ner' # "Portable packages must use only ASCII characters in their demos."
-kco <- new("KorAPConnection", verbose = TRUE)
-if (is.null(kco@accessToken)) {
- message(
- paste0(
- "In order to receive KWICSs also from corpora with restricted licenses, you need an access token.\n",
- "To generate an access token, login to KorAP and navigite to KorAP's OAuth settings <",
- kco@KorAPUrl,
- "settings/oauth#page-top>"
- )
- )
-}
-q <- corpusQuery(kco, "Ameisenplage", metadataOnly = F) %>%
+korap_app <-oauth_app("test-korap-client", key = "773NHGM76N7P9b6rLfmpM4", secret = NULL)
+korap_endpoint <- oauth_endpoint(NULL,
+ "settings/oauth/authorize",
+ "api/v1.0/oauth2/token",
+ base_url = "https://korap.ids-mannheim.de")
+token_bundle = oauth2.0_token(korap_endpoint, korap_app, scope = "search match_info", cache = FALSE)
+
+new("KorAPConnection", verbose = TRUE, accessToken = token_bundle[["credentials"]][["access_token"]]) %>%
+ corpusQuery(query, fields = c("textSigle", "pubDate", "corpusTitle", "snippet"),
+ metadataOnly = FALSE) %>%
fetchAll() %>%
- displayKwics()
+ slot("collectedMatches") %>%
+ dplyr::arrange(pubDate) %>%
+ dplyr::rename("kwic" = "snippet") %>%
+ kable(escape = FALSE, caption = paste0("Query hits for '", query, "' ordered by date of publication")) %>%
+ kable_styling() %>%
+ print()