Get rid of unnecessary ifelses

Change-Id: Ifb1c9013b2c8be7363f216adbb2dab1c08f517eb
diff --git a/R/KorAPConnection.R b/R/KorAPConnection.R
index 70a7f97..dd796b8 100644
--- a/R/KorAPConnection.R
+++ b/R/KorAPConnection.R
@@ -79,6 +79,7 @@
             .Object
           })
 
+
 apiTokenServiceName <- "RKorAPClientAPIToken"
 
 setGeneric("persistApiToken", function(kco, apiToken) standardGeneric("persistApiToken") )
@@ -157,15 +158,17 @@
   }
   parsed <- jsonlite::fromJSON(content(resp, "text"))
   if (!is.null(parsed$warnings)) {
-    message <- ifelse (nrow(parsed$warnings) > 1,
-                       sapply(parsed$warnings, function(warning) paste(sprintf("%s: %s", warning[1], warning[2]), sep="\n")),
-                       sprintf("%s: %s", parsed$warnings[1], parsed$warnings[2]))
+    message <- if (nrow(parsed$warnings) > 1)
+      sapply(parsed$warnings, function(warning) paste(sprintf("%s: %s", warning[1], warning[2]), sep="\n"))
+    else
+      sprintf("%s: %s", parsed$warnings[1], parsed$warnings[2])
     warning(message, call. = FALSE)
   }
   if (status_code(resp) != 200) {
-    message <- ifelse (!is.null(parsed$errors),
-                       sapply(parsed$errors, function(error) paste0(sprintf("\n%s: KorAP API request failed: %s", error[1], error[2]))),
-                       message <- sprintf("%s: KorAP API request failed.", status_code(resp)))
+    message <- if (!is.null(parsed$errors))
+                 sapply(parsed$errors, function(error) paste0(sprintf("\n%s: KorAP API request failed: %s", error[1], error[2])))
+               else
+                 message <- sprintf("%s: KorAP API request failed.", status_code(resp))
     stop(message, call. = FALSE)
   }
   if (kco@cache) {
diff --git a/R/KorAPQuery.R b/R/KorAPQuery.R
index d9b589c..f337d4a 100644
--- a/R/KorAPQuery.R
+++ b/R/KorAPQuery.R
@@ -134,58 +134,84 @@
 #' @aliases corpusQuery
 #' @export
 setMethod("corpusQuery", "KorAPConnection",
-  function(kco,
-           query = ifelse(missing(KorAPUrl),
-                     stop("At least one of the parameters query and KorAPUrl must be specified.", call. = FALSE),
-                     httr::parse_url(KorAPUrl)$query$q),
-           vc = ifelse(missing(KorAPUrl), "", httr::parse_url(KorAPUrl)$query$cq),
-           KorAPUrl,
-           metadataOnly = TRUE,
-           ql = ifelse(missing(KorAPUrl), "poliqarp", httr::parse_url(KorAPUrl)$query$ql),
-           fields = c("corpusSigle", "textSigle", "pubDate",  "pubPlace",
-                      "availability", "textClass", "snippet"),
-           accessRewriteFatal = TRUE,
-           verbose = kco@verbose,
-           expand = length(vc) != length(query),
-           as.df = FALSE) {
-    ifelse(length(query) > 1 || length(vc) > 1, {
-        grid <- { if (expand)  expand_grid(query=query, vc=vc) else tibble(query=query, vc=vc) }
+          function(kco,
+                   query = if (missing(KorAPUrl))
+                     stop("At least one of the parameters query and KorAPUrl must be specified.", call. = FALSE)
+                   else
+                     httr::parse_url(KorAPUrl)$query$q,
+                   vc = if (missing(KorAPUrl)) "" else httr::parse_url(KorAPUrl)$query$cq,
+                   KorAPUrl,
+                   metadataOnly = TRUE,
+                   ql = if (missing(KorAPUrl)) "poliqarp" else httr::parse_url(KorAPUrl)$query$ql,
+                   fields = c(
+                     "corpusSigle",
+                     "textSigle",
+                     "pubDate",
+                     "pubPlace",
+                     "availability",
+                     "textClass",
+                     "snippet"
+                   ),
+                   accessRewriteFatal = TRUE,
+                   verbose = kco@verbose,
+                   expand = length(vc) != length(query),
+          as.df = FALSE) {
+  if (length(query) > 1 || length(vc) > 1) {
+
+    grid <- {
+      if (expand)
+        expand_grid(query=query, vc=vc) else tibble(query=query, vc=vc) }
         return(
              do.call(rbind,
                      Map(function(q, cq) corpusQuery(kco, query=q, vc=cq, ql=ql,
                                                      verbose=verbose, as.df = TRUE), grid$query, grid$vc)) %>%
                remove_rownames()
-           )}, {
-             contentFields <- c("snippet")
-             if(metadataOnly) {
-                fields <- fields[!fields %in% contentFields]
-             }
-             request <- paste0('?q=', URLencode(query, reserved=TRUE),
-                      ifelse(vc != '', paste0('&cq=', URLencode(vc, reserved=TRUE)), ''), '&ql=', ql)
-             webUIRequestUrl <- paste0(kco@KorAPUrl, request)
-             requestUrl <- paste0(kco@apiUrl, 'search', request,
-                                  '&fields=', paste(fields, collapse = ","),
-                                  ifelse(metadataOnly, '&access-rewrite-disabled=true', ''))
-             log.info(verbose, "Searching \"", query, "\" in \"", vc, "\"", sep="")
-             res = apiCall(kco, paste0(requestUrl, '&count=0'))
-             log.info(verbose, " took ", res$meta$benchmark, "\n", sep="")
-             ifelse(as.df,
-                    return(data.frame(query=query,
-                                      totalResults=res$meta$totalResults,
-                                      vc=vc,
-                                      webUIRequestUrl=webUIRequestUrl, stringsAsFactors = FALSE)),
-                    return(KorAPQuery(
-                      korapConnection = kco,
-                      nextStartIndex = 0,
-                      fields = fields,
-                      requestUrl = requestUrl,
-                      request = request,
-                      totalResults = res$meta$totalResults,
-                      vc = vc,
-                      apiResponse = res,
-                      webUIRequestUrl = webUIRequestUrl,
-                      hasMoreMatches = (res$meta$totalResults > 0),
-                    )))})
+           )
+    } else {
+      contentFields <- c("snippet")
+      if (metadataOnly) {
+        fields <- fields[!fields %in% contentFields]
+      }
+      request <-
+        paste0('?q=',
+               URLencode(query, reserved = TRUE),
+               if (vc != '') paste0('&cq=', URLencode(vc, reserved = TRUE)) else '', '&ql=', ql)
+      webUIRequestUrl <- paste0(kco@KorAPUrl, request)
+      requestUrl <- paste0(
+        kco@apiUrl,
+        'search',
+        request,
+        '&fields=',
+        paste(fields, collapse = ","),
+        if (metadataOnly) '&access-rewrite-disabled=true' else ''
+      )
+      log.info(verbose, "Searching \"", query, "\" in \"", vc, "\"", sep =
+                 "")
+      res = apiCall(kco, paste0(requestUrl, '&count=0'))
+      log.info(verbose, " took ", res$meta$benchmark, "\n", sep =
+                 "")
+      if (as.df)
+        data.frame(
+          query = query,
+          totalResults = res$meta$totalResults,
+          vc = vc,
+          webUIRequestUrl = webUIRequestUrl,
+          stringsAsFactors = FALSE
+        )
+      else
+        KorAPQuery(
+          korapConnection = kco,
+          nextStartIndex = 0,
+          fields = fields,
+          requestUrl = requestUrl,
+          request = request,
+          totalResults = res$meta$totalResults,
+          vc = vc,
+          apiResponse = res,
+          webUIRequestUrl = webUIRequestUrl,
+          hasMoreMatches = (res$meta$totalResults > 0),
+        )
+    }
   })
 
 #' Fetch the next bunch of results of a KorAP query.
@@ -217,7 +243,7 @@
   collectedMatches <- kqo@collectedMatches
 
   repeat {
-    res <- apiCall(kqo@korapConnection, paste0(kqo@requestUrl, '&count=', min(ifelse(!is.na(maxFetch), maxFetch - results, maxResultsPerPage), maxResultsPerPage) ,'&offset=', offset + results))
+    res <- apiCall(kqo@korapConnection, paste0(kqo@requestUrl, '&count=', min(if (!is.na(maxFetch)) maxFetch - results else maxResultsPerPage, maxResultsPerPage) ,'&offset=', offset + results))
     if (res$meta$totalResults == 0) { return(kqo) }
     for (field in kqo@fields) {
       if (!field %in% colnames(res$matches)) {
diff --git a/man/corpusQuery-KorAPConnection-method.Rd b/man/corpusQuery-KorAPConnection-method.Rd
index 6f77d34..d527802 100644
--- a/man/corpusQuery-KorAPConnection-method.Rd
+++ b/man/corpusQuery-KorAPConnection-method.Rd
@@ -6,17 +6,16 @@
 \alias{corpusQuery}
 \title{Method corpusQuery}
 \usage{
-\S4method{corpusQuery}{KorAPConnection}(kco,
-  query = ifelse(missing(KorAPUrl),
+\S4method{corpusQuery}{KorAPConnection}(kco, query = if
+  (missing(KorAPUrl))
   stop("At least one of the parameters query and KorAPUrl must be specified.",
-  call. = FALSE), httr::parse_url(KorAPUrl)$query$q),
-  vc = ifelse(missing(KorAPUrl), "", httr::parse_url(KorAPUrl)$query$cq),
-  KorAPUrl, metadataOnly = TRUE, ql = ifelse(missing(KorAPUrl),
-  "poliqarp", httr::parse_url(KorAPUrl)$query$ql),
-  fields = c("corpusSigle", "textSigle", "pubDate", "pubPlace",
-  "availability", "textClass", "snippet"), accessRewriteFatal = TRUE,
-  verbose = kco@verbose, expand = length(vc) != length(query),
-  as.df = FALSE)
+  call. = FALSE) else httr::parse_url(KorAPUrl)$query$q, vc = if
+  (missing(KorAPUrl)) "" else httr::parse_url(KorAPUrl)$query$cq, KorAPUrl,
+  metadataOnly = TRUE, ql = if (missing(KorAPUrl)) "poliqarp" else
+  httr::parse_url(KorAPUrl)$query$ql, fields = c("corpusSigle",
+  "textSigle", "pubDate", "pubPlace", "availability", "textClass",
+  "snippet"), accessRewriteFatal = TRUE, verbose = kco@verbose,
+  expand = length(vc) != length(query), as.df = FALSE)
 }
 \arguments{
 \item{kco}{\code{\link{KorAPConnection}} object (obtained e.g. from \code{new("KorAPConnection")}}