4 min read

Test function printing

Function printing works as desired if the function is defined within the document. The issue is if it is defined externally, and loaded via source. It appears that if I use options(keep.source = TRUE) before the call to load.project this will be done correctly.

Function setup:

myfunc <- function(x, y) {
  # Here's a function with a comment and custom spacing in the source code
  switch(x, 
    one = 1,
    two = if (y) {
            3
          } else {
            2
          }
  )
}

Print using print

print(myfunc)
function(x, y) {
  # Here's a function with a comment and custom spacing in the source code
  switch(x, 
    one = 1,
    two = if (y) {
            3
          } else {
            2
          }
  )
}

Print using print.function

print.function(myfunc)
function(x, y) {
  # Here's a function with a comment and custom spacing in the source code
  switch(x, 
    one = 1,
    two = if (y) {
            3
          } else {
            2
          }
  )
}
getOption("keep.source")
[1] FALSE
print(start_params)
function(x, dist, ...) {
  x_orig <- x
  if (dim(as.matrix(x))[2] == 2) {
    x <- apply(x, 1, mean) # set each value to the middle of its interval
  } else if (dim(as.matrix(x))[2] > 2) {
    stop("x must be a vector or two-column matrix")
  }
  

  start_pars <- switch(dist,
    hnorm = list(sigma = sqrt(mean(x^2))),
    invgauss = list(mean = mean(x),
                    shape = mean(x)^3 / var(x)),
    gengamma = start_gengamma(x_orig, ...),
    NULL
  )
  if (is.null(start_pars)) {
    warning("No method exists for setting start values for ", dist)
  }
  return(start_pars)
}
withAutoprint(start_params)
> start_params
function(x, dist, ...) {
  x_orig <- x
  if (dim(as.matrix(x))[2] == 2) {
    x <- apply(x, 1, mean) # set each value to the middle of its interval
  } else if (dim(as.matrix(x))[2] > 2) {
    stop("x must be a vector or two-column matrix")
  }
  

  start_pars <- switch(dist,
    hnorm = list(sigma = sqrt(mean(x^2))),
    invgauss = list(mean = mean(x),
                    shape = mean(x)^3 / var(x)),
    gengamma = start_gengamma(x_orig, ...),
    NULL
  )
  if (is.null(start_pars)) {
    warning("No method exists for setting start values for ", dist)
  }
  return(start_pars)
}
rm("start_params")
source("lib/helpers.R", keep.source = TRUE)
print(start_params)
function(x, dist, ...) {
  x_orig <- x
  if (dim(as.matrix(x))[2] == 2) {
    x <- apply(x, 1, mean) # set each value to the middle of its interval
  } else if (dim(as.matrix(x))[2] > 2) {
    stop("x must be a vector or two-column matrix")
  }
  

  start_pars <- switch(dist,
    hnorm = list(sigma = sqrt(mean(x^2))),
    invgauss = list(mean = mean(x),
                    shape = mean(x)^3 / var(x)),
    gengamma = start_gengamma(x_orig, ...),
    NULL
  )
  if (is.null(start_pars)) {
    warning("No method exists for setting start values for ", dist)
  }
  return(start_pars)
}
withAutoprint(start_params)
> start_params
function(x, dist, ...) {
  x_orig <- x
  if (dim(as.matrix(x))[2] == 2) {
    x <- apply(x, 1, mean) # set each value to the middle of its interval
  } else if (dim(as.matrix(x))[2] > 2) {
    stop("x must be a vector or two-column matrix")
  }
  

  start_pars <- switch(dist,
    hnorm = list(sigma = sqrt(mean(x^2))),
    invgauss = list(mean = mean(x),
                    shape = mean(x)^3 / var(x)),
    gengamma = start_gengamma(x_orig, ...),
    NULL
  )
  if (is.null(start_pars)) {
    warning("No method exists for setting start values for ", dist)
  }
  return(start_pars)
}
rm("start_params")
start_params <- function(x, dist, ...) {
  x_orig <- x
  if (dim(as.matrix(x))[2] == 2) {
    x <- apply(x, 1, mean) # set each value to the middle of its interval
  } else if (dim(as.matrix(x))[2] > 2) {
    stop("x must be a vector or two-column matrix")
  }
  

  start_pars <- switch(dist,
    hnorm = list(sigma = sqrt(mean(x^2))),
    invgauss = list(mean = mean(x),
                    shape = mean(x)^3 / var(x)),
    gengamma = start_gengamma(x_orig, ...),
    NULL
  )
  if (is.null(start_pars)) {
    warning("No method exists for setting start values for ", dist)
  }
  return(start_pars)
}

print(start_params)
function(x, dist, ...) {
  x_orig <- x
  if (dim(as.matrix(x))[2] == 2) {
    x <- apply(x, 1, mean) # set each value to the middle of its interval
  } else if (dim(as.matrix(x))[2] > 2) {
    stop("x must be a vector or two-column matrix")
  }
  

  start_pars <- switch(dist,
    hnorm = list(sigma = sqrt(mean(x^2))),
    invgauss = list(mean = mean(x),
                    shape = mean(x)^3 / var(x)),
    gengamma = start_gengamma(x_orig, ...),
    NULL
  )
  if (is.null(start_pars)) {
    warning("No method exists for setting start values for ", dist)
  }
  return(start_pars)
}