0

I am trying to use a function to modify another function default settings through formals but my problem is that when I check my function defaults afterwards then nothing has changed. My code (minus unrelated stuff) is:

ScouringSettings <- function(min.MAF=NULL, eq.thresh=NULL){
  if (is.null(min.MAF) && is.null(eq.thresh)){
    maf <- paste0("Minimum MAF criterion is: ", formals(GeneScour)$min.maf)
    eq  <- paste0("Chi² HW equilibrium threshold: ", formals(GeneScour)$min.eq)
    cat(paste(maf, eq, sep="\n"))
  } else if (is.null(eq.thresh)) {
    formals(GeneScour) <- alist(gene=, min.maf = min.MAF, min.eq = formals(GeneScour)$min.eq)
  } else if (is.null()){
    formals(GeneScour) <- alist(gene=, min.maf = formals(GeneScour)$min.maf, min.eq = eq.thresh)
  } else {
    formals(GeneScour) <- alist(gene=, min.maf = min.maf, min.eq = eq.thresh)
  }
}

I thought that maybe it was because of a problem of scope or something so I tried printing out the defaults while still being in my first function and it printed :

$gene

$min.maf
min.MAF

$min.eq
formals(GeneScour)$min.eq

And even when I forcefully type

formals(GeneScour) <- alist(gene=, min.maf = 2, min.eq = formals(GeneScour)$min.eq)

The modification is not carried over outside of the ScouringSettings.

I am a bit lost, how could I manage that ?

  • 3
    You need to return `GeneScour` and assign it. Alternatively you could use `assign` within `ScouringSettings` to assign from there, but side effects are bad practice. – Roland Jan 14 '16 at 15:47
  • 1
    See this [section on returning values from functions](http://adv-r.had.co.nz/Functions.html#return-values) in Hadley's helpful book. – neerajt Jan 14 '16 at 18:09
  • 1
    In addition to the excellent comments so far, you should also construct complete examples. We have no idea what your `GeneScour` function looks like. I would not have imagined that assigning a value to a local copy of any function would be effective unless you: a) returned the full function AND b) reassigned that value to its name within the same namespace. – IRTFM Jan 15 '16 at 02:35

0 Answers0