R/wrappers.R

Defines functions print.commented summary.commented mutate.commented select.commented filter.commented arrange.commented group_by.commented summarise.commented

Documented in arrange.commented filter.commented group_by.commented mutate.commented print.commented select.commented summarise.commented summary.commented

##############################
#
# Wrapper functions
#
##############################

## PRINT
#' print commented Function
#'
#' This function suppresses the class and comments attributes in the default print function.
#' @param x R object to summarize
#' @param ... other pass variables
#' @param notes flag to show or hide notes accosiated with the R object
#' @param showtimestamps boolean to indicate if timestamps for each comment should be shown or hidden
#' @return print output
#' @keywords print commented
#' @export
print.commented <- function(x, ..., notes = F, showtimestamps = T) {
  notes_x <- x
  if(notes) {
    notes(x, showtimestamps = showtimestamps)
  }
  # if this is a basic vector or list, suppress additional class output 2
  if(is.data.frame(x)) {
    NextMethod(x, ...)
  }  else if (is.list(x)) {
    x <- list(unlist(x))
    NextMethod(x, ...)
  } else if (is.numeric(x)) {
    x <- c(x)
    NextMethod(x, ...)
  } else {
    NextMethod(x, ...)
  }
  invisible(notes_x)
}


## SUMMARY
#' summary commented Function
#'
#' This function appends the comments output to the standard summary function.
#' @param object R object to summarize
#' @param ... other pass variables
#' @return summary output
#' @keywords summary commented
#' @export
summary.commented <- function(object, ...) {
  notes(object, showtimestamps = T)
  NextMethod(object, ...)
}

## DPLYR

#' mutate commented Function
#'
#' This function allows the dplyr mutate function to work with commented objects.
#' @param x R object to mutate
#' @param ... other pass variables
#' @return mutated object
#' @keywords mutate commented
#' @export
mutate.commented <- function(x, ...) {
  comments <- attr(x,'comments')
  cl <- class(x)
  y <- NextMethod(x, ...)
  class(y) <- cl
  attr(y, 'comments') <-  comments
  invisible(y)
}

#' select commented Function
#'
#' This function allows the dplyr select function to work with commented objects.
#' @param x R object to mutate
#' @param ... other pass variables
#' @return mutated object
#' @keywords mutate commented
#' @export
select.commented <- function(x, ...) {
  comments <- attr(x,'comments')
  cl <- class(x)
  y <- NextMethod(x, ...)
  class(y) <- cl
  attr(y, 'comments') <-  comments
  invisible(y)
}

#' filter commented Function
#'
#' This function allows the dplyr filter function to work with commented objects.
#' @param x R object to mutate
#' @param ... other pass variables
#' @return filtered object
#' @keywords mutate commented
#' @export
filter.commented <- function(x, ...) {
  comments <- attr(x,'comments')
  cl <- class(x)
  y <- NextMethod(x, ...)
  class(y) <- cl
  attr(y, 'comments') <-  comments
  invisible(y)
}

#' arrange commented Function
#'
#' This function allows the dplyr arrange function to work with commented objects.
#' @param x R object
#' @param ... other pass variables
#' @return arrangedobject
#' @keywords mutate commented
#' @export
arrange.commented <- function(x, ...) {
  comments <- attr(x,'comments')
  cl <- class(x)
  y <- NextMethod(x, ...)
  class(y) <- cl
  attr(y, 'comments') <-  comments
  invisible(y)
}



#' group_by commented Function
#'
#' This function allows the dplyr group_by function to work with commented objects.
#' @param x R object
#' @param ... other pass variables
#' @return  grouped object
#' @keywords mutate commented
#' @export
group_by.commented <- function(x, ...) {
  comments <- attr(x,'comments')
  cl <- class(x)
  y <- NextMethod(x, ...)
  class(y) <- cl
  attr(y, 'comments') <-  comments
  invisible(y)
}

#' summarise commented Function
#'
#' This function allows the dplyr summarise function to work with commented objects.
#' @param x R object
#' @param ... other pass variables
#' @return  grouped object
#' @keywords mutate commented
#' @export
summarise.commented <- function(x, ...) {
  comments <- attr(x,'comments')
  cl <- class(x)
  y <- NextMethod(x, ...)
  class(y) <- cl
  attr(y, 'comments') <-  comments
  invisible(y)
}
mandpd/comments documentation built on May 5, 2019, 7:57 p.m.