Domanda

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?

È stato utile?

Soluzione

(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.

Altri suggerimenti

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 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top