Reputation: 521
At end of question is script (plus on github). The GitHub repo is here and can be installed with
install.packages("devtools")
devtools::install_github("ArtieLadie/RanglaPunjab")
MergePalette(name,name2)
takes two names. If person only passes one name, I want the following to execute and then exit the function,
Enter 2 valid palettes. Run ListPalette() for list of palettes.
Unfortunately, when I execute MergePalette("Teej")
, I get
Error in MergePalette("Teej") :
argument "name2" is missing, with no default
How to fix this
MergePalette <- function(name,name2){
pal <- RanglaPunjab(name)
if (is.null(name2)){
stop("Enter 2 valid palettes. Run ListPalette() for list of palettes.")
}
pal2 <- RanglaPunjab(name2)
new_pal <-unique(c(pal,pal2))
new_pal
}
Upvotes: 0
Views: 259
Reputation: 160447
Two methods:
Preferred: Check for missingness:
MergePalette <- function(name,name2){
if (missing(name2)) { stop(...) }
Define a default value of NULL
in the formals, and the function works:
MergePalette <- function(name,name2=NULL){
if (is.null(name2)) { stop(...) }
If the user (accidentally) provides an argument to name2
that is itself NULL
, the error message will be confusing. As @Moody_Mudskipper noted, by setting the default to NULL
, you are implicitly telling the user that this argument is optional and/or that NULL
is okay. With that, I suggest that this option is not appropriate for this question/use.
Upvotes: 2