0

We have uploaded an R package to github that hosts several functions accompanied by print methods functions. When we use devtools::install_github() to install the package and run one of the functions, its appropriate print function does not run. If the functions are imported directly into the Global Environment they work properly. To demonstrate, we have created a package called samplePackage with a function called sampleFunction which takes the mean of a given vector. The repository can be found on github at lj5yn/samplePackage.

See below the desired output when the sampleFunction is correctly paired with a print method.

> x <- c(1,2,3,4,5)
> sampleFunction(x)
  The mean of x is  3

The incorrect output we get is as follows:

> x <- c(1,2,3,4,5)
> sampleFunction(x)
$xMean
[1] 3

attr(,"class")
[1] "samplePackage_sampleFunction"

Below find two different specifications of the functions we have tried in github.

Try 1

#' Sample function
#'
#' Sample function that provides the mean.
#'
#' @param x The variable to use to compute the mean.
#'
#' @return The mean of x.
#'
#' @export

sampleFunction <- function(x) {

# x variable must be integer or numeric
if (!(typeof(x) %in% c("double", "integer", "numeric"))) {
stop("x variables must be type integer or numeric.", call. = FALSE)
return(NULL)
}

xMean <- sum(x)/length(x)

# Output Results

# Variable names added to dataset
resNames <- (list(
"xMean" = xMean))
class(resNames) <- "samplePackage_sampleFunction"
return(resNames)

}

print.samplePackage_sampleFunction <- function(x){
cat("The mean of x is ", x$xMean)
}

Try 2

Try 2 involves separating sampleFunction and print.samplePackage_sampleFunction into separate files. print.samplePackage_sampleFunction goes into a file called prints and is grouped with the other R code function files in github.

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

#' S3Methods for Printing
#'
#' @name prints
#'
#' @aliases
#' print.samplePackage_sampleFunction
#'
#' @usage
#' \method{print}{sampleFunction}(x,  ...)
#'
#' @description Prints for \code{samplePackage} objects
#'
#' @param x Object from \code{samplePackage} package
#'
#' @param ... Additional arguments
#'
#' @return Prints \code{samplePackage} object
#'
# Print sampleFunction
#' @export
print.samplePackage_sampleFunction <- function(x){
cat("The mean of x is ", x$xMean)
}

Based on Try2 method currently on lj5yn/samplePackage on github, please find the information contained in the NAMESPACE file below:

# Generated by roxygen2: do not edit by hand

S3method(print,samplePackage_sampleFunction)
export(sampleFunction)

We are hoping to get advice on how to properly specify the print function in github. Please let me know if any clarification or further information is needed.

  • From the help page `?NextMethod`: "So methods for a generic function need to be available in the environment of the call to the generic, or they must be registered." When you define the method at the console it is in the correct environment, but when you load it from your package it doesn't get registered and is not in the `global.env()`. – IRTFM Nov 04 '21 at 02:10
  • 1
    I don't understand what this has to do with GitHub. It doesn't make any difference whether you install an R package directly from your local copy or via GitHub. It's the same code (provided you keep them synced). Perhaps I am misunderstanding? – Maurits Evers Nov 04 '21 at 02:11
  • 1
    I think the question is answered in https://stackoverflow.com/questions/48855993/defining-custom-print-methods-for-arrays-and-atomic-vectors . What's currently happening is that there is no registration and the `print.default` function is being accessed, so you see the value printed as a list. – IRTFM Nov 04 '21 at 02:29
  • If that link does not answer the question, then you should [edit] your question to include the current NAMESPACE file. I removed the github tag since I agree completely with @MauritsEvers that this has nothing to do with github and everything to do with how to build an R package. – IRTFM Nov 04 '21 at 02:46
  • I have included information on where this samplePackage is hosted on github as well as the text currently contained in the NAMESPACE file. I will take a look at stackoverflow.com/questions/48855993/… . – Laura Jamison Nov 04 '21 at 17:50

0 Answers0