R/rsort.rbolaget.R

Defines functions rsort.rbolaget

Documented in rsort.rbolaget

#' Sorting function for a object of class rbolaget
#' @description \code{rsort} is used to sort the assortment of all product in Systembolaget given specific parameters.
#' @param data an object of class \code{\link{rbolaget}}.
#' @param Category a character with the category/categories.
#' @param IsKosher a boolean.
#' @param IsOrganic a boolean.
#' @param IsEthical a boolean.
#' @param AlcoholPercentage a numeric value given in a list with min and max value.
#' @param Volume a numeric value given in a list with min and max value.
#' @param Price a numeric value given in a list with min and max value.
#' @param Country a character with the country/countries.
#' @param ... Further arguments passed to or from other methods
#' @return \code{rsort} return a data frame of class "rbolaget" and "data.frame" with the given criteria given by the parameters.
#' @examples
#' rsort(data = rget(), Category = "Öl")
#' @export
#' @import dplyr

rsort.rbolaget <- function(data, Category = NULL, IsKosher = NULL, IsOrganic = NULL,
                           IsEthical = NULL, AlcoholPercentage = list(min = NULL, max=NULL),
                           Volume = list(min = NULL, max = NULL),
                           Price = list(min = NULL, max = NULL), Country = NULL, ...){

  if(identical(x = colnames(data), y =c("ProductNameBold", "ProductNumber", "ProductNumberShort",
                                         "Category", "SubCategory", "Type", "Style", "Price", "AlcoholPercentage",
                                         "Volume", "BottleTextShort", "RecycleFee", "Country", "ProducerName",
                                         "IsKosher", "IsOrganic", "IsEthical", "SellStartDate", "Taste", "Usage", "APK"))) {

  } else if (identical(x = colnames(data), y =c("ProductNameBold", "ProductNumber", "ProductNumberShort",
                                                "Category", "SubCategory", "Type", "Style", "Price", "AlcoholPercentage",
                                                "Volume", "BottleTextShort", "RecycleFee", "Country", "ProducerName",
                                                "IsKosher", "IsOrganic", "IsEthical", "SellStartDate", "Taste", "Usage", "APK", "link"))) {
  } else {
    stop(paste("Not all variables exists. Data columns should be the output of rget()"))
  }

  sorted_data <- data %>% filter(
    # Category
    Category %in% if(is.null(!!Category)){unique(data$Category)} else {!!Category},
    # IsKosher
    IsKosher %in% if(is.null(!!IsKosher)){unique(data$IsKosher)} else {!!IsKosher},
    # IsOrganic
    IsOrganic %in% if(is.null(!!IsOrganic)){unique(data$IsOrganic)} else {!!IsOrganic},
    # IsEthical
    IsEthical %in% if(is.null(!!IsEthical)){unique(data$IsEthical)} else {!!IsEthical},
    # Country
    Country %in% if(is.null(!!Country)){unique(data$Country)} else {!!Country},
    # AlcoholPercentage
    between(x=AlcoholPercentage,
            left=if(is.null(!!AlcoholPercentage$min)){min(data$AlcoholPercentage)
            } else {!!AlcoholPercentage$min} ,
            right = if(is.null(!!AlcoholPercentage$max)){max(data$AlcoholPercentage)
            } else {!!AlcoholPercentage$max}),
    # Volume
    between(x=Volume,
            left=if(is.null(!!Volume$min)){min(data$Volume) } else {!!Volume$min} ,
            right = if(is.null(!!Volume$max)){max(data$Volume) } else {!!Volume$max}),
    # Price
    between(x=Price,
            left=if(is.null(!!Price$min)){min(data$Price) } else {!!Price$min} ,
            right = if(is.null(!!Price$max)){max(data$Price) } else {!!Price$max})
  )
  return(sorted_data)
}
Sidryd/rbolaget documentation built on Oct. 2, 2020, 10:27 a.m.