Help with building a package and presenting results in RMarkdown

Hi! :smile:

I am working on a project where I'm trying to create my own package, mylm, which is meant to replicate most of the functionality that the standard lm() objects in R have. This works fine, both when testing in the R file in my project, but also in a RMarkdown file, where I use chunks to present what the different functions can do. The rmd files are also where the problems arise, when I try to knit the document. In a regular R session it works fine to just use print(mylm_model) or summary(mylm_model), but whenever I knit it seems to use some standard R function for the summary, instead of my self-written one.

However, the knitting works fine when I use summary.mylm(mylm-model), and this might have to be my emergency solution if I don't figure this out within the next couple of days. Any help is highly appreciated!

The structure of the R file in the package looks like this:

# Select Build, Build and reload to build and lode into the R-session.

mylm <- function(formula, data = list(), contrasts = NULL, ...){
  # Extract model matrix & responses
  mf <- model.frame(formula = formula, data = data)
  X  <- model.matrix(attr(mf, "terms"), data = mf, contrasts.arg = contrasts)
  y  <- model.response(mf)
  terms <- attr(mf, "terms")
  
  
  # Add code here to calculate coefficients, residuals, fitted values, etc...
  # and store the results in the list est
  est <- list(terms = terms, model = mf)
  
  # Store call and formula used
  est$call <- match.call()
  est$formula <- formula
  
  # Set class name. This is very important!
  class(est) <- 'mylm'
  
  # Return the object with all results
  return(est)
}

print.mylm <- function(object, ...){
  # Code here is used when print(object) is used on objects of class "mylm"
  # Useful functions include cat, print.default and format
  cat('Info about object\n')
}

summary.mylm <- function(object, ...){
  # Code here is used when summary(object) is used on objects of class "mylm"
  # Useful functions include cat, print.default and format
  cat('Summary of object\n')
}

plot.mylm <- function(object, ...){
  # Code here is used when plot(object) is used on objects of class "mylm"
  
}



# This part is optional! You do not have to implement anova
anova.mylm <- function(object, ...){
  # Code here is used when anova(object) is used on objects of class "mylm"
  
  # Components to test
  comp <- attr(object$terms, "term.labels")
  
  # Name of response
  response <- deparse(object$terms[[2]])
  
  # Fit the sequence of models
  txtFormula <- paste(response, "~", sep = "")
  model <- list()
  for(numComp in 1:length(comp)){
    if(numComp == 1){
      txtFormula <- paste(txtFormula, comp[numComp])
    }
    else{
      txtFormula <- paste(txtFormula, comp[numComp], sep = "+")
    }
    formula <- formula(txtFormula)
    model[[numComp]] <- lm(formula = formula, data = object$model)
  }
  
  # Print Analysis of Variance Table
  cat('Analysis of Variance Table\n')
  cat(c('Response: ', response, '\n'), sep = '')
  cat('          Df  Sum sq X2 value Pr(>X2)\n')
  for(numComp in 1:length(comp)){
    # Add code to print the line for each model tested
  }
  
  return(model)
  
}```
With some added code here and there, within the functions, to actually calculate/return the things I need.

I figured it out! So if someone else ever runs into the same problem, it might help to look in the file "Namespace" in your project folder, and update it like the following:

S3method(print, mylm)

S3method(summary, mylm)

S3method(plot, mylm)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.