0

I am facing the following exception:

library(tidyverse)
library(dplyr)
library(rlang)

data(mtcars)

select_expr = "mpg , cyl"

mtcars %>% select_(select_expr)

Error in parse(text = x): <text>:1:5: unexpected ','
1: mpg ,
        ^

What I am doing wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
user8270077
  • 4,621
  • 17
  • 75
  • 140
  • 2
    dplyr's `*_` functions are being slowly deprecated [in favor of rlang](https://dplyr.tidyverse.org/articles/programming.html). The equivalent of the above might be `select_expr <- c("mpg", "cyl"); mtcars %>% select(!!!syms(select_expr))`, though in this case you can also use the tidyselect helpers: `mtcars %>% select(one_of(select_expr))` – alistaire Jun 17 '18 at 17:23
  • 2
    Storing multiple names in a single string is not a good idea, though, as it's not easy to transform back into runnable code. A vector of names (whether strings, expressions, quosures, or whatever) is easier to work with. – alistaire Jun 17 '18 at 17:28

1 Answers1

3

You made a mistake I often make as well, just put 2 extract accents in the string

library(tidyverse)
library(dplyr)
library(rlang)

data(mtcars)

select_expr = c("mpg" , "cyl")

mtcars %>% select(select_expr)
M.Punt
  • 101
  • 4