20

Suppose we have an R function whose arguments must be selected out of a finite set of elements. Like qplot(..., geom=""). And geom can take only some values, like bar or point.

How can I find out all the valid values the argument of a given function may take? Apart from docs or Internet, which often miss all possible values. Perhaps, some R function can help?

Anton Tarasenko
  • 8,099
  • 11
  • 66
  • 91
  • 5
    This is not possible in general as validity is often determined by the logic of the function in question. – James Dec 03 '13 at 14:25
  • 6
    Due to the way ggplot2 is developed, you can get the list using this: `sub("geom_","",apropos("^geom_"))` – James Dec 03 '13 at 14:29
  • Perhaps parsing the help page to get the Usage? Would not be 100%. – IRTFM Aug 29 '15 at 21:12

3 Answers3

7

If the function of interest is defined like

f <- function(a = c("foo","bar")) {
    match.arg(a)
}

i.e. when the options are defined as a vector to be later checked with match.arg function, then you could use the formals function which would give you a list of arguments with the values like in the following example

> formals(f)
$a
c("foo", "bar")

Otherwise I don't think it is possible to get all valid argument values without RTFSing.

Pavel Obraztcov
  • 338
  • 1
  • 6
0

To expand on @Pavel Obraztcov's excellent answer (and @James' comment above), take this function f as an example:

f <- function(x) {
  if(x == "foo") {
    return("you said 'foo'")
  } else if(x == "bar") {
    return("you said 'bar'")
  } else if(x == "xyz") {
    stop("'xyz' is not a valid option")
  }
}

(Not necessarily a good function, but there's nothing to stop someone from writing a function like this.)

How would we want to define "valid values" for the argument x? We could say the valid values are foo, bar, and xyz, since f explicitly handles those three values. But it's clearly insufficient to just write a piece of code that parses a series of simple if-else statements; not all functions are structured this way.

And f handles xyz by throwing an error, so maybe only foo and bar are valid values? But in that case, we need a piece of code that will examine the internal logic of f and determine that xyz always leads to an error. That's not a trivial task - and I'm no CS expert, but it occurs to me to wonder whether the halting problem comes into play here.

Also, f does nothing for values other than foo, bar, and xyz - that's probably bad in this context, but are there contexts where both "return something" and "do nothing" would be acceptable ("valid") outcomes?

Things would be even worse if f didn't handle x directly, but instead passed it to some other function. Now we need to trace the internal logic of f and any functions it calls.

So no, there's no general algorithmic way to get all valid options for some argument. When documentation and the internet fail, reading the source code (as a human) is the general way.

A. S. K.
  • 2,504
  • 13
  • 22
0

The answer to this particular question is probably

apropos("^geom_") |> gsub(pattern = "geom_", replace = "")

but this is a special case: to know this you need to know that qplot (which is now deprecated) looks up a geom_ function: this internal code line shows what it's doing (g is a loop variable set to the values in geom):

p <- p + do.call(paste0("geom_", g), params)
 [1] "abline"            "area"              "bar"              
 [4] "bin_2d"            "bin2d"             "blank"            
 [7] "boxplot"           "col"               "contour"          
[10] "contour_filled"    "count"             "crossbar"         
[13] "curve"             "density"           "density_2d"       
[16] "density_2d_filled" "density2d"         "density2d_filled" 
[19] "dotplot"           "errorbar"          "errorbarh"        
[22] "freqpoly"          "function"          "hex"              
[25] "histogram"         "hline"             "jitter"           
[28] "label"             "line"              "linerange"        
[31] "map"               "path"              "point"            
[34] "pointrange"        "polygon"           "qq"               
[37] "qq_line"           "quantile"          "raster"           
[40] "rect"              "ribbon"            "rug"              
[43] "segment"           "sf"                "sf_label"         
[46] "sf_text"           "smooth"            "spoke"            
[49] "step"              "text"              "tile"             
[52] "violin"            "vline"            
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453