function - plotting routine for n number of arguments in r -
i have following parameters wish plot:
weight <- c(102,20,30,04,022,01,220,10) height <- c(102,20,30,04,022,01,220,10) catg <- c(102,20,30,04,022,01,220,10) catg <- matrix(height,nrow = 2)
and these should plotted in pdf file saved in path:
figurefolder <- "c:\\..."
i attempting write function takes n number of input parameters , saves them file (in case file if defined figurefolder). have following function:
plotting_function <- function( ...,figurefolder){ # find number of input arguments nargin <- length(as.list(match.call())) -1 nargin <- nargin - 2 variable_list <- list(...) variable_list <- variable_list[1:nargin] (i in 1:length(variable_list)){ if (variable_list[i] == "catg") routine 1 go here if(variable_list[i] != "catg") routine 2 go here } }
so, here trying make function work thatif variable 'catg' inserted function plotting routine follow routine 1 (contour in case), otherwise follow routine 2 (line plot in y case).
however, problem i'm having variable_list returns number , not name of variable inserted e.g.
plotting_function <- function(weight,height,catg,figurefolder)
variable_list 1,2, , 3 not weight, height, , catg, therefore cannot use if statement have shown. suggest method make work?
if understand you're trying do, work:
plotting_function <- function( ... , figurefolder) { v_names <- as.list(match.call()) #variable_list <- list(...) variable_list <- v_names[2:(length(v_names)-1)] (i in 1:length(variable_list)) { if (variable_list[i] == "catg") print("category") # example #routine 1 go here if (variable_list[i] != "catg") print(as.character(variable_list[i][[1]])) # example #routine 2 go here } }
Comments
Post a Comment