How to automatically update class S4 slot in R

I played with S4 objects in R and wondered about the following:

Suppose the following simplified example: we have two classes S4 inside R, one of which is called the Client and one more Order . We define them with the following slots:

Customer <- setClass(Class = "Customer",slots = c(CustomerID = "numeric", Name = "character", OrderHistory = "data.frame"),
                     prototype = list(CustomerID = 0,Name = "",OderHistory = data.frame()))

Order <- setClass(Class = "Order",slots = c(CustomerID = "numeric", Description = "character",
                                               Cost = "numeric"), 
                     prototype = list(CustomerID = 0,Description = "",Cost = 0))


# constructor

Customer <- function(CustomerID, Name, OrderHistory=data.frame()){
  #drop sanity checks
  new("Customer",CustomerID = CustomerID, Name = Name, OrderHistory = OrderHistory)
}

Order <- function(CustomerID, Description = "",Cost = 0){
  #drop sanity checks
  new("Order",CustomerID = CustomerID, Description = "", Cost = 0)
}

#create two objects

firstCustomer <- Customer(1,"test")

firstOrder <- Order(1,"new iPhone", 145)

Obviously, firstCustomer and firstOrder are connected through CustomerID. Is it possible to automatically update the Client OrderHistory slot after creating a new order instance? Assuming OrderHistory has two columns, Description and Cost, how can I automatically update an instance of a new order? Is there an elegant / general way to do this? Most likely, the Order class needs a "Client" slot. Thank you very much in advance

+4
2

, , . :

Customer <- setClass(
  "Customer", 
  slots=c(
    CustomerID="numeric", 
    Name="character", 
    OrderHistory="list"
  ),
  prototype=list(OrderHistory = list())
)
Order <- setClass(
  Class="Order", 
  slot =c(
    Description="character", Cost="numeric"
) )

setGeneric(
  "add<-", 
  function(object, value, ...) StandardGeneric("add<-")
)
setMethod("add<-", c("Customer", "Order"), 
  function(object, value) {
    object@OrderHistory <- append(object@OrderHistory, value)
    object    
  }
)
setMethod("show", "Customer", 
  function(object) {
    cat("** Customer #", object@CustomerID, ": ", object@Name, "\n\n", sep="")
    for(i in object@OrderHistory) cat("\t", i@Description, "\t", i@Cost, "\n", sep="")
  }
)

firstCustomer <- new("Customer", CustomerID=1, Name="test")
add(firstCustomer) <- new("Order", Description="new iPhone", Cost=145)
add(firstCustomer) <- new("Order", Description="macbook", Cost=999)

firstCustomer

:

** Customer #1: test

  new iPhone  145
  macbook 999
+3

@BrodieG, , , , , .., c. , , , , , ( , S4 R, , , ).

## Customers -- analogous to a data.frame or data base table
setClass(Class = "Customers",
  slots = c(CustomerId = "integer", Name = "character"))

## Items -- analogous to a data.frame or data base table
setClass(Class = "Items",
  slots = c(ItemId = "integer", Description = "character", Cost = "numeric"))

## Transactions -- analogous to a data.frame or data base table
setClass(Class="Transactions",
  slots = c(TransactionId="integer", CustomerId="integer", ItemId="integer"))

, -

## Business -- analogous to a data *base*
Business = setClass(Class = "Business",
  slots = c(Customers="Customers", Items="Items", Transactions="Transactions"))

,

.nextid <- function(x, slotName, n=1L)
    max(0L, slot(x, slotName)) + seq_len(n)

.update <- function(x, ...) {
    args <- list(...)
    for (nm in names(args))
        args[[nm]] <- c(slot(x, nm), args[[nm]])
    do.call("initialize", c(list(x), args))
}

add_customers <- function(business, customerNames)
{
    customers <- slot(business, "Customers")
    len <- length(customerNames)
    initialize(business,
               Customers=.update(customers,
                 CustomerId=.nextid(customers, "CustomerId", len),
                 Name=customerNames))
}

add_items <- function(business, descriptions, costs)
{
    items <- slot(business, "Items")
    len <- length(descriptions)
    initialize(business,
               Items=.update(items,
                 ItemId=.nextid(items, "ItemId", len),
                 Description=descriptions, Cost=costs))
}

, , ; , , purchase() .

.purchase <- function(business, customerId, itemIds)
{
    transactions <- slot(business, "Transactions")
    len <- length(itemIds)
    initialize(business,
               Transactions=.update(transactions,
                 TransactionId=rep(.nextid(transactions, "TransactionId"), len),
                 CustomerId=rep(customerId, len),
                 ItemId=itemIds))
}

bus <- Business()
bus <- add_customers(bus, c("Fred", "Barney"))
bus <- add_items(bus, c("Phone", "Tablet"), c(200, 250))
bus <- .purchase(bus, 1L, 1:2)  # Fred buys Phone, Tablet
bus <- .purchase(bus, 2L, 2L)   # Barney buys Tablet

( )

> sum(bus@Items@Cost[bus@Transactions@ItemId])
[1] 700

R , , , ; , back-end SQL.

+2

Source: https://habr.com/ru/post/1583835/


All Articles