Laura Jamison
Laura Jamison

Reputation: 133

Print method in R for function that prints summary even when function output is assigned to an object

I am creating a new R package and am adding in a function that will produce a list. See an example of how this function is working below. I have assigned a print method to this function that provides a separate interpretation/descriptive summary of the output. See an example below.

fun_example <- function(xvar, yvar){
  # Plots
  plot1 = ggplot2::ggplot(data.frame(xvar, yvar), ggplot2::aes(x = xvar, y = yvar)) + ggplot2::geom_line()
  plot2 = ggplot2::ggplot(data.frame(xvar, yvar), ggplot2::aes(x = xvar, y = yvar)) + ggplot2::geom_line()
  plots <- list(plot1, plot2)
  
  # Correlation
  Cor <- cor(xvar, yvar)
  
  result <- list(plots, Cor)
  names(result) <- c("plots", "Cor")
  class(result) <- "fun_example"
  suppressMessages(return(result))
}

#-------------------------------------------
## S3Methods print() // Updated 06.02.2021
#-------------------------------------------

#' S3Methods for Printing
#'
#' @name prints
#'
#' @aliases
#' print.fun_example
#'
#' @usage
#' \method{print}{fun_example}(x, ...)
#'
#' @description Prints for \code{package_example} objects
#'
#' @param x Object from \code{package_example} package
#'
#' @param ... Additional arguments
#'
#' @return Prints \code{package_example} object
#'
# Print fun_example
#' @export
print.fun_example <- function(x, ...){
  cat("Visually inspect each plot in the plots object to check for linearity.\n")
  if(x$Cor > 0.8){
    cat("A correlation greater than 0.8 was found.")
  } else {
    cat("A correlation less than or equal to 0.8 was found.")
  }
}

ISSUE:

When the function is run by itself and not assigned to an object, the print function works as expected. The actual output, a list, does not appear in the console. However, users of the function should save this into an object.

> fun_example(xvar = 1:3, yvar = 4:6)
Visually inspect each plot in the plots object to check for linearity.
A correlation greater than 0.8 was found.

When the function's output is assigned to an object, the output (list) is assigned to an object BUT the print method does not show anything in the console.

> test <- fun_example(xvar = 1:3, yvar = 4:6)
> 

WHAT I WANT TO SEE:

What I want to do is specify the print method in such a way that the list will be assigned to an object and the print will show up in the console, such as:

> test <- fun_example(xvar = 1:3, yvar = 4:6)
Visually inspect each plot in the plots object to check for linearity.
A correlation greater than 0.8 was found.

Users can manually type the example below by putting the assignment in parantheses, but I'd rather have this automatically done by the functions.

> (test <- fun_example(xvar = 1:3, yvar = 4:6))
Visually inspect each plot in the plots object to check for linearity.
A correlation greater than 0.8 was found.

Specifying the prints within the function rather than using a print method does not work.

Any help would be appreciated.

Upvotes: 1

Views: 850

Answers (1)

Laura Jamison
Laura Jamison

Reputation: 133

per @MrFlick's comment, I changed the function to:

fun_example <- function(xvar, yvar){
  # Plots
  plot1 = ggplot2::ggplot(data.frame(xvar, yvar), ggplot2::aes(x = xvar, y = yvar)) + ggplot2::geom_line()
  plot2 = ggplot2::ggplot(data.frame(xvar, yvar), ggplot2::aes(x = xvar, y = yvar)) + ggplot2::geom_line()
  plots <- list(plot1, plot2)
  
  # Correlation
  Cor <- cor(xvar, yvar)

message("Visually inspect each plot in the plots object to check for linearity.\n")
  if(x$Cor > 0.8){
    message("A correlation greater than 0.8 was found.")
  } else {
    message("A correlation less than or equal to 0.8 was found.")
  }
  
  result <- list(plots, Cor)
  names(result) <- c("plots", "Cor")
  class(result) <- "fun_example"
  suppressMessages(return(result))
}

Note that this didn't work for me if the messages did not come before the return() function.

Upvotes: 1

Related Questions