Get point variable name arguments based on point in function R (deparse)

I am creating an automatic plotter based on some dummy variables. I installed it so that:

plotter <- function(...) { } 

will build all the mannequins that I feed him.

However, I would like it to be able to add labels to the plot, namely the names of the variables. I know that

 deparse(substitute(variablename)) 

will give

 "variablename" 

which is the beginning, but how to do it in case of several arguments? Is it possible? Is there a workaround?

+4
source share
1 answer

names(list(...)) will provide you with a character vector containing the names of the provided arguments that were absorbed ... :

 plotter <- function(...) {names(list(...))} plotter(x=1:4, y=11:14) # [1] "x" "y" 

Alternatively, if you want to pass unnamed arguments, try this (which extends @baptiste's answer is now deleted):

 plotter <- function(..., pch=16, col="red") { nms <- setdiff(as.character(match.call(expand.dots=TRUE)), as.character(match.call(expand.dots=FALSE))) nms } x <- 1:4 y <- 1:14 plotter(x, y, col="green") # [1] "x" "y" 
+9
source

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


All Articles