Your issue is related to non-standard evaluation
.
The least possible example of doing this is:
verificar_coluna <- function(data, coluna){
coluna_texto <- deparse(substitute(coluna))
coluna_texto %in% names(data)
}
> verificar_coluna(mtcars, mpg)
[1] TRUE
The substitute
function captures the expression entered by the user, and the deparse
function turns it into a string.
The problem with this approach is if your function is called from within another:
verificar_coluna2 <- function(data, coluna){
verificar_coluna(data, coluna)
}
> verificar_coluna2(mtcars, mpg)
[1] FALSE
So a safer approach is proposed by the lazyeval
package. The vignette is very explanatory.
In practice, it's best to write a function:
verificar_coluna <- function(data, coluna){
coluna_texto <- lazyeval::expr_text(coluna)
coluna_texto %in% names(data)
}
So the two functions will work.
> verificar_coluna(mtcars, mpg)
[1] TRUE
> verificar_coluna2(mtcars, mpg)
[1] TRUE