R/cq.R

Defines functions ccq cq

Documented in ccq cq

#' Easy quote characters - fancy version
#'
#' @param ... Unquoted variables (seperated by commas) that you wish to quote;
#'   empty arguments (e.g. third item in `one,two,,four`) will be returned as
#'   blanks.
#'
#' @return a character vector
#'
#' @examples
#' cq(dale, audrey, laura, hawk)
#'
#' @author Tim Taylor
#'
#' @importFrom utils capture.output
#' @export
cq <- function(...) vapply(substitute(list(...)), deparse, character(1))[-1]


#' Easy quote characters - fancy version
#'
#' @param x Either an unquoted variable or a quoted string which you wish to
#'   split on spaces; all white space will be trimmed, then normalized to a
#'   single space.
#' @param ... Unquoted variables (seperated by commas) that you wish to quote;
#'   empty arguments (e.g. third item in `one,two,,four`) will be returned as
#'   blanks. If `x` is a quoted string then `...` should be empty
#' @param .clip Should the code to generate the constructed character vector be
#'   copied to your system clipboard; defaults to TRUE.
#'
#' @note The system clipboard will only be used in interactive sessions.
#'
#' @return a character vector
#'
#' @examples
#' cq("  dale    audrey   laura hawk ")
#' cq(dale, audrey, laura, hawk)
#' # clipboard will contain c("dale", "audrey", "laura", "hawk")
#'
#' @author Tim Taylor and Carl Pearson
#'
#' @importFrom utils capture.output
#' @export
ccq <- function(x, ..., .clip = TRUE) {
  if (is.character(substitute(x))) {
    stopifnot("When `x` is a quoted string `...` should be empty" = ...length() == 0)
    res <- unlist(strsplit(trimws(x), "[[:space:]]+"))
  } else {
    res <- vapply(substitute(list(x, ...)), deparse, character(1))
    res <- res[-1]
  }
  if (interactive() && .clip && clipr::clipr_available()) {
    clipr::write_clip(capture.output(dput(res, control = "all")))
  }
  res
}
tjtnew/coffee documentation built on Dec. 23, 2021, 11 a.m.