Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which R functions are not suitable for programmatic use?

Tags:

r

interactive

Some functions like browser only make sense when used interactively.

It is widely regarded that the subset function should only be used interactively.

Similarly, sapply isn't good for programmatic use since it doesn't simplify the result for zero length inputs.

I'm trying to make an exhaustive list of functions that are only not suitable for programmatic use.

The plan is to make a tool for package checking to see if any of these functions are called and give a warning.

There are other functions like file.choose and readline that require interactivity, but these are OK to include in packages, since the end use will be interactive. I don't care too much about these for this use case but feel free to add them to the list.

Which functions have I missed?

like image 632
Richie Cotton Avatar asked Apr 27 '14 10:04

Richie Cotton


People also ask

What is an R function?

In R, a function is an object so the R interpreter is able to pass control to the function, along with arguments that may be necessary for the function to accomplish the actions. The function in turn performs its task and returns control to the interpreter as well as any result which may be stored in other objects.

How do I create a function list in R?

How to Create Lists in R? We can use the list() function to create a list. Another way to create a list is to use the c() function. The c() function coerces elements into the same type, so, if there is a list amongst the elements, then all elements are turned into components of a list.


2 Answers

(Feel free to edit.)

The following functions should be handled with care (which does not necessarily mean they are not suitable for programming):

  • Functions whose outputs do not have a consistent output class depending on the inputs: sapply, mapply (by default)

  • Functions whose internal behavior is different depending on the input length: sample, seq

  • Functions that evaluate some of their arguments within environments: $, subset, with, within, transform.

  • Functions that go against normal environment usage: attach, detach, assign, <<-

  • Functions that allow partial matching: $

  • Functions that only make sense in interactive usage: browser, recover, debug, debugonce, edit, fix, menu, select.list

  • Functions that can be a threat (virus) if used with user inputs: source, eval(parse(text=...)), system.

Also, to some extent, every function that generates warnings rather than errors. I recommend using options(warn = 2) to turn all warnings into errors in a programming application. Specific cases can then be allowed via suppressWarnings or try.

like image 85
flodel Avatar answered Sep 28 '22 00:09

flodel


This is in answer to the comment after the question by the poster. This function inputs a function and returns the bad functions found with their line number. It can generate false positives but they are only warnings anways so that does not seem too bad. Modify bad to suit.

badLines <- function(func) {
    bad <- c("sapply", "subset", "attach")
    regex <- paste0("\\b", bad, "\\b")
    result <- sort(unlist(sapply(regex, FUN = grep, body(func), simplify = FALSE)))
    setNames(result, gsub("\\b", "", names(result), fixed = TRUE))
}
badLines(badLines)

## sapply1  subset  attach sapply2 
##       2       2       2       4 
like image 44
G. Grothendieck Avatar answered Sep 28 '22 00:09

G. Grothendieck