R/setters_getters.R

Defines functions set_text set_id check_setting set set.selector

Documented in set_id set_text

#' Set the text attribute of a selector
#'
#' Given a selector object, set its text attribute
#'
#' @param selector An object of class selector
#' @param text A non-null character vector (or coercible to one). The vector must be
#' of the same length as the data stored in the selector object
#'
#' @return A selector object
#' @export
#'
#' @examples my_selector %>% set_text(c(1,2,3))
set_text <- function(selector, text){
  #checks
  check_setting(selector, text)
  text <- as.character(text)
  set(selector, 'text', text)
}

#' Set the id attribute of a selector
#'
#' Given a selector object, set its id attribute
#'
#' @param selector An object of class selector
#' @param id A non-null character vector, or coercible to one
#'
#' @return A selector object
#' @export
#'
#' @examples my_selector %>% set_id(c(1,2,3))
set_id <- function(selector, id){
  check_setting(selector, id)
  id <- as.character(id)
  set(selector, 'id', id)
}


check_setting <- function(selector, col){
  if (!inherits(selector, 'selector')){
    stop('Error: I need a selector object in the first argument')
  }
  if (!is.vector(col)){
    stop('Error: text must be a vector')
  }
  if (length(col) != nrow(selector$data)){
    stop('The provided text vector does not have the same length as the
         number of rows in the data')
  }
}

#Setters and getters generics

set <- function(selector, attribute, value){
  UseMethod('set', selector)
}

set.selector <- function(selector, attribute, value){
  selector[[attribute]] <- value
  selector
}
riccardopinosio/selectR documentation built on May 14, 2019, 11:13 a.m.