Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting default values to a function/package R

I've made a package with a function that asks for 3 values, like so:

example.foo("directory",vA1,vA2)

Now, I want to set a default value for both vA1 and vA2. I wrote the following piece of code, but I don't know why it ain't working

  if (!exists("vA1")) {
    vA1 = 2
  }

2 being the default value for vA1. When I run the package I get an "argument "vA1" missing, with no default". How do I set the default for this function? Thanks!

like image 652
Gabriel Hernandez Avatar asked Dec 17 '25 14:12

Gabriel Hernandez


1 Answers

As Pascal points out, defaults to functions are provided by providing named arguments. For instance:

 fun <- function(x="Hello World") print(x)

 fun()
 [1] "Hello World"

 fun("and good bye")
 [1] "and good bye"
like image 118
coffeinjunky Avatar answered Dec 19 '25 07:12

coffeinjunky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!