2

I'm trying to parse an argument from a custom function through stringr::str_extract but I'm not able to make it work.

Here's what I've tried so far:

library(tidyverse)

extr_var <- function(data, variable) {

      res <- data %>% 
      mutate(kk = str_extract(variable, 'setosa'))

      return(res)
}


extr_var(iris, "Species")

and the top 6 rows from the output:

#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species   kk
#1          5.1         3.5          1.4         0.2  setosa <NA>
#2          4.9         3.0          1.4         0.2  setosa <NA>
#3          4.7         3.2          1.3         0.2  setosa <NA>
#4          4.6         3.1          1.5         0.2  setosa <NA>
#5          5.0         3.6          1.4         0.2  setosa <NA>
#6          5.4         3.9          1.7         0.4  setosa <NA>

I've tried using match.call, substitute and other but I couldn't make it work. Any help is appreciated.

patL
  • 2,259
  • 1
  • 17
  • 38
  • 1
    This is about non-standard evaluation in `dplyr`, you just need to wrap `variable` in `!!sym()`. This post answers your question: [Programming with dplyr using string as input](https://stackoverflow.com/questions/44121728/programming-with-dplyr-using-string-as-input) and [Programming with dplyr](https://dplyr.tidyverse.org/articles/programming.html) is a good resource. – caldwellst May 06 '20 at 11:40
  • Yes it does! Thank you@caldwellst. I google it and couldn't find any answer. – patL May 06 '20 at 11:42

0 Answers0