0

Lets say for now considering base package. How can I search all types of operators within the function list of a package

To see the list of functions in base package, we can do

 ls("package:base")

So the above list would also have operators like Arithmetic operators, Logical operators, Infix operators, Binary operators etc.

So Basically how can I search the operators from the list using grep() or any other alternative function?

Example, for base package searching functions with = sign in it..

  grep("\\=",ls("package:base"),value=T)
  # [1] "!=" "<=" "="  "==" ">="

But it would be good if we get all types of operators at once.

How to get list of all types of operators present in R, so that we grep over that list of operators.

Any help is highly appreciated.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30
  • 1
    How would you guess an operator you don't know about ? The idea in itself sounds really strange. – Tensibai Sep 14 '16 at 16:09
  • I am not guessing, I know bunch of types of operators, but How to get them together and do multiple patterns matching using grep – Sowmya S. Manian Sep 14 '16 at 16:12
  • Hum, maybe `?Ops` to get the operators. As they're all functions anyway... If the Q is about to grep multiples patterns, I'm sure you can find it with a few search (mainly using `(pattern|pattern|pattern)`) – Tensibai Sep 14 '16 at 16:14
  • How about using `grep(paste(strsplit(intToUtf8(c(33:47, 58:64, 91:96)), "")[[1]], collapse="|"), ls("package:base"), value = TRUE)` – akrun Sep 14 '16 at 16:16
  • 1
    Your last 4 questions within 2 days ago. I see some coincidences ;) [THIS](http://stackoverflow.com/questions/39446901/extract-list-of-objects-created-inside-a-function-with-their-code), [THIS](http://stackoverflow.com/questions/39464205/how-to-get-the-list-of-in-built-functions-used-within-a-function-in-r) and [THIS](http://stackoverflow.com/questions/39483717/searching-functions-using-grep-over-multiple-loaded-packages-in-r) – 989 Sep 14 '16 at 16:18
  • @m0h3n: Popping in my head all kinds of questions. – Sowmya S. Manian Sep 14 '16 at 16:20
  • @akrun Will Try in sometime. Apologies. Will be back in sometime – Sowmya S. Manian Sep 14 '16 at 16:20
  • @akrun The above comment it is not showing the operators but I guess all the functions. – Sowmya S. Manian Sep 14 '16 at 16:34
  • 1
    I meant that you can select the operator based on `intToUtf8(c(33:47, 58:64, 91:96)` by changing the numbers, I don't know which all you need. So was showing a general case – akrun Sep 14 '16 at 16:35

1 Answers1

1

i'm not sure there is a way to know a function is an opreator but maybe this will be helpful:

grep(pattern = "^[^a-zA-Z]", x = ls("package:base"), value = T)

EDIT: maybe this will be more helpfull:

getOps <- function() {
    objs <- mget(ls("package:base"), inherits = TRUE)
    funcs <- objs[sapply(objs, is.function)]
    prim <- funcs[sapply(funcs, is.primitive)]
    ops <- prim[grep(pattern = "^[^a-zA-Z]", x = names(prim))]
    ops
}

getOps()
cccmir
  • 953
  • 6
  • 12
  • 1
    Or use `grep(pattern = "^[[:punct:]]", x = ls("package:base"), value = TRUE)` – akrun Sep 14 '16 at 16:15
  • Wow nice start to it!! But I mean if you open `Arithmetic` operators you have a list, and same for `logical`, `assignment`, `binary`, `unary`, `Infix` etc. I have no clue how many other types of operator are there. – Sowmya S. Manian Sep 14 '16 at 16:15
  • In the output some other functions are also coming which are not operators actually if we have reference list of all types of operators in R then that would help – Sowmya S. Manian Sep 14 '16 at 16:16
  • This will miss `%in%` (out of others) – Tensibai Sep 14 '16 at 16:18
  • Yes this `getOps()` seems helpful, but how would I know if I have covered R's all operators? Actually I am looking into all types of operators in R. I can see maximum of them in `getOps()`. Thank you. – Sowmya S. Manian Sep 14 '16 at 16:36
  • 1
    its a little problematic because in R operators are just like other functions. i don't know any way do identify them uniqly – cccmir Sep 14 '16 at 16:38
  • @cccmir Could you please tell me what this `getOps()` function is doing in one to two lines. – Sowmya S. Manian Sep 15 '16 at 15:14
  • 1
    return all r primitive functions that dont start with a letter – cccmir Sep 15 '16 at 15:15