Which R functions are not suitable for programmatic use?

眉间皱痕 提交于 2019-12-10 02:08:17

问题


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?


回答1:


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




回答2:


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 


来源:https://stackoverflow.com/questions/23322436/which-r-functions-are-not-suitable-for-programmatic-use

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!