Start tidyverse migration

Change-Id: Ifc972de789c22e2ff43159740d7f1fa7125e1ef2
diff --git a/R/KorAPConnection.R b/R/KorAPConnection.R
index 7eface5..fa8f1f5 100644
--- a/R/KorAPConnection.R
+++ b/R/KorAPConnection.R
@@ -4,10 +4,11 @@
 #' New \code{KorAPConnection} objects can be created by \code{KorAPConnection()}.
 #'
 #' @import R.cache
-#' @import jsonlite
 #' @import utils
 #' @import methods
-#'
+#' @import dplyr
+#' @import purrr
+#' @import tidyr
 #'
 
 #' @export
@@ -64,6 +65,8 @@
 #' @rdname KorAPConnection-class
 #' @param kco KorAPConnection object
 #' @param url request url
+#' @importFrom jsonlite fromJSON
+#' @export
 setMethod("apiCall", "KorAPConnection",  function(kco, url) {
   if (kco@cache) {
     parsed <- R.cache::loadCache(dir=KorAPCacheSubDir(), key=list(url))
diff --git a/R/KorAPQuery.R b/R/KorAPQuery.R
index 0b9ee7c..3cc0b37 100644
--- a/R/KorAPQuery.R
+++ b/R/KorAPQuery.R
@@ -4,7 +4,8 @@
 #' New \code{KorAPQuery} objects are typically created by the \code{\link{corpusQuery}} method.
 #'
 #' @include KorAPConnection.R
-#' @import jsonlite
+#' @import tidyr
+#' @import dplyr
 #' @import httr
 #'
 #'
@@ -98,28 +99,30 @@
 #'
 #' @examples
 #' # Fetch metadata of every query hit for "Ameisenplage" and show a summary
-#' kco <- new("KorAPConnection")
-#' kqo <- corpusQuery(kco, "Ameisenplage")
-#' kqo <- fetchAll(kqo)
-#' kqo
+#' new("KorAPConnection") %>% corpusQuery("Ameisenplage") %>% fetchAll()
 #'
 #' # Use the copy of a KorAP-web-frontend URL for an API query of "Ameise" in a virtual corpus
 #' # and show the number of query hits (but don't fetch them).
-#' kco <- new("KorAPConnection")
-#' kqo <- corpusQuery(kco,
-#'        KorAPUrl = "https://korap.ids-mannheim.de/?q=Ameise&cq=pubDate+since+2017&ql=poliqarp")
-#' kqo
+#'
+#' new("KorAPConnection", verbose = TRUE) %>%
+#'  corpusQuery(KorAPUrl =
+#'    "https://korap.ids-mannheim.de/?q=Ameise&cq=pubDate+since+2017&ql=poliqarp")
 #'
 #' # Plot the time/frequency curve of "Ameisenplage"
-#' kco <- new("KorAPConnection", verbose=TRUE)
-#' q <- fetchAll(corpusQuery(kco, "Ameisenplage"))
-#' df <- as.data.frame(table(as.numeric(format(q@collectedMatches$pubDate,"%Y")), dnn="year"),
-#'                     stringsAsFactors = FALSE)
-#' df$Freq <- mapply(function(f, y) f / corpusStats(kco, paste("pubDate in", y))@tokens,
-#'                   df$Freq, df$year)
-#' df <- merge(data.frame(year=min(df$year):max(df$year)), df, all = TRUE)
-#' df[is.na(df$Freq),]$Freq <- 0
-#' plot(df, type="l")
+#' new("KorAPConnection", verbose=TRUE) %>%
+#'   { . ->> kco } %>%
+#'   corpusQuery("Ameisenplage") %>%
+#'   fetchAll() %>%
+#'   slot("collectedMatches") %>%
+#'   mutate(year = lubridate::year(pubDate)) %>%
+#'   dplyr::select(year) %>%
+#'   group_by(year) %>%
+#'   summarise(Count = n()) %>%
+#'   mutate(Freq = mapply(function(f, y)
+#'     f / corpusStats(kco, paste("pubDate in", y))@tokens, Count, year)) %>%
+#'   dplyr::select(-Count) %>%
+#'   complete(year = min(year):max(year), fill = list(Freq = 0)) %>%
+#'   plot(type = "l")
 #'
 #' @seealso \code{\link{KorAPConnection}}, \code{\link{fetchNext}}, \code{\link{fetchRest}}, \code{\link{fetchAll}}, \code{\link{corpusStats}}
 #'
@@ -203,10 +206,14 @@
         res$matches[, field] <- NA
       }
     }
-    currentMatches <- res$matches[kqo@fields]
+    currentMatches <-
+      kqo@fields %>%
+      map_dfr( ~tibble(!!.x := logical() ) ) %>%
+      bind_rows(res$matches) %>%
+      select(kqo@fields)
     if ("pubDate" %in% kqo@fields) {
-      currentMatches$pubDate = as.Date(currentMatches$pubDate, format = "%Y-%m-%d")
-      factorCols <- colnames(subset(currentMatches, select=-c(pubDate)))
+      currentMatches$pubDate <-  currentMatches$pubDate %>% as.Date(format = "%Y-%m-%d")
+      factorCols <- currentMatches %>% select(-pubDate) %>% colnames()
     } else {
       factorCols <- colnames(currentMatches)
     }
@@ -242,7 +249,7 @@
 #' Fetch all results of a KorAP query.
 #'
 #' @examples
-#' q <- fetchAll(corpusQuery(new("KorAPConnection"), "Ameisenplage"))
+#' q <- new("KorAPConnection") %>% corpusQuery("Ameisenplage") %>% fetchAll()
 #' q@collectedMatches
 #'
 #' @aliases fetchAll
@@ -255,7 +262,7 @@
 #' Fetches the remaining results of a KorAP query.
 #'
 #' @examples
-#' q <- fetchRest(fetchNext(corpusQuery(new("KorAPConnection"), "Ameisenplage")))
+#' q <- new("KorAPConnection") %>% corpusQuery("Ameisenplage") %>% fetchAll()
 #' q@collectedMatches
 #'
 #' @aliases fetchRest
diff --git a/R/reexports.R b/R/reexports.R
new file mode 100644
index 0000000..f6624ba
--- /dev/null
+++ b/R/reexports.R
@@ -0,0 +1,30 @@
+
+#' Pipe operator
+#'
+#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details.
+#'
+#' @name %>%
+#' @rdname pipe
+#' @keywords internal
+#' @export
+#' @importFrom magrittr %>%
+#' @usage lhs \%>\% rhs
+NULL
+#' @importFrom dplyr mutate
+#' @export
+dplyr::mutate
+#' @importFrom dplyr select
+#' @export
+dplyr::select
+#' @importFrom dplyr group_by
+#' @export
+dplyr::group_by
+#' @importFrom dplyr summarise
+#' @export
+dplyr::summarise
+#' @importFrom tidyr complete
+#' @export
+tidyr::complete
+#' @importFrom lubridate year
+#' @export
+lubridate::year