All valid values of an argument of a function in R
Asked Answered
E

4

21

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?

Endocarditis answered 3/12, 2013 at 13:45 Comment(3)
This is not possible in general as validity is often determined by the logic of the function in question.Newsprint
Due to the way ggplot2 is developed, you can get the list using this: sub("geom_","",apropos("^geom_"))Newsprint
Perhaps parsing the help page to get the Usage? Would not be 100%.Chronogram
C
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.

Comedo answered 23/1, 2014 at 12:16 Comment(0)
I
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.

Insecticide answered 14/10, 2021 at 19:53 Comment(1)
There is no elif keyword in R. You are thinking about Python.Pegmatite
E
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"            
Espadrille answered 27/7, 2023 at 18:26 Comment(0)
M
0

str(function_name) can be very informative e.g.

str(acf)
function (x, lag.max = NULL, type = c("correlation", "covariance", "partial"), plot = TRUE, na.action = na.fail, 
    demean = TRUE, ...)  
Macdonald answered 12/2 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.