Avoid inverse characters with dplyr

How to write select argument without inverse characters? I would like to do this to pass this argument from a variable as a character string.

 df <- dat[["__Table"]] %>% select(`__ID` ) %>% mutate(fk_table = "__Table", val = 1) 

Changing the select argument to "__ID" gives this error:

 Error: All select() inputs must resolve to integer column positions. The following do not: * "__ID" 

Unfortunately, the _ characters in the column names cannot be avoided, since the data is loaded from the relational database (FileMaker) via ODBC and must be written back to the database while storing the column names.

Ideally, I would like to do the following:

 colName <- "__ID" df <- dat[["__Table"]] %>% select(colName) %>% mutate(fk_table = "__Table", val = 1) 

I also tried eval(parse()) :

 df <- dat[["__Table"]] %>% select( eval(parse(text="__ID")) ) %>% mutate(fk_table = "__Table", val = 1) 

It throws this error:

 Error in parse(text = "__ID") : <text>:1:1: unexpected input 1: _ ^ 

By the way, the following works, but then I go back to the square (still with the return line symbol).

 eval(parse(text="`__ID`") 

Reverse character references in R :

+1
source share
1 answer

You can use as.name() with select_() :

 colName <- "__ID" df <- data.frame(`__ID` = c(1,2,3), `123` = c(4,5,6), check.names = FALSE) select_(df, as.name(colName)) 
+3
source

Source: https://habr.com/ru/post/1258525/


All Articles