0

Is there a way to

  1. Run a script on package load?
  2. Within that package, change default function values automatically?

For example, have R detect whether you are using Windows, or Mac. Then R would change some default function input values.

MadmanLee
  • 478
  • 1
  • 5
  • 18
  • ad(2): You could use an if statement to decide what parameters to pass to a function, if that's what you mean. E.g. `if (Sys.info()["sysname"] == "Windows") utils::write.table(x, "clipboard") else write.xclip(x)`. You can include that in a function you write yourself, or wrap an existing function with your own function that then passes on different defaults to the internal function. – user12728748 Apr 06 '20 at 23:21
  • 1
    ad(1): You can use `.onLoad()` to execute code at the moment your package is loaded. See `?.onLoad()` or http://r-pkgs.had.co.nz/r.html#r-differences. – hplieninger Apr 07 '20 at 06:58

1 Answers1

0

Thank you all for the great guidance. Following this post, and this post, i was able to come up with this answer. Note, for this to work you need to assign these values to the .GlobaEnv using the <<-.

formals returns the function input names and values. Here I show how to change the defaults values of these different functions

# This will run on starup, detect your device,
# and then change different function default values.
.onLoad <- function(libname, pkgname){
    # First detect what the system is
    # If it is not on windows change default values
    # of function that require changes.
    systemType <- Sys.info()[1]
    if( systemType != "Windows" ){
        formals(tcd)$info <<- F
        formals(tcd)$bcex <<- 0.5

        windows <<- cairoDevice::Cairo
        formals(windows)$pointsize <<- 7

        formals(PeakFunc7)$bcex <<- 1.5
        formals(RDView)$wh <<- 11
        formals(RDView)$hh <<- 6
    }
}
MadmanLee
  • 478
  • 1
  • 5
  • 18