1

I have a large dataset and am trying to search through it for keywords. Doing this interactively, I've been using grep alike so:

fee <- grep("fi", fo$fum)
View(fi$fum[fee, ])

This works well enough for my purposes but it has a lot of repetitive typing. I figured I would speed up the process a little by writing a function:

giant_search <- function(x, y) {
    y <- grep(quote(x), fo$fum)
    return(y)
    View(fo$fum[y, ])
}

When I use this function, however, y returns no values (so, of course, View doesn't show anything either). However if I write the exact same code outside of the function it works exactly as I want/expect it to.

I assume the issue is with how R deals with function arguments or strings within a function, but I cannot figure out how to fix the problem.

Turtlegods
  • 82
  • 4
  • 2
    No need for `quote(x)` as `grep` takes a string argument. Anything after `return` will be ignored so move `return` to the end of your function. Finally, if you will use `y` just inside the function then no need to passe it as an argument. – A. Suliman Jan 07 '19 at 21:06
  • How exactly are you calling the `giant_search()` function? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 07 '19 at 21:18
  • MrFlick, I was calling it as `giant_search(giant_name)`. Running A. Suliman's solution below, I realize that part of my problem was that the arguments weren't being passed into the function as strings (i.e. I should have done `giant_search(x='giant_name')` instead). – Turtlegods Jan 08 '19 at 15:45

1 Answers1

1
giant_search <- function(x, y){
y <- grep(x, iris$Species)
View(iris[y, ])
return(y)
}
giant_search(x='setosa')
A. Suliman
  • 12,923
  • 5
  • 24
  • 37