Dynamic variable names in graphs, files, and loop compatibility

I am trying to write a function that creates a plot and automatically saves it to a file. The trick I fight with it is to dynamically make [plotname = varname and filename = varname &], and make it compatible with calling it from a loop.

# Create data my_df = cbind(uni=runif (100),norm=rnorm (100),bino=rbinom(100,20, 0.5)); head (my_df) my_vec = my_df[,'uni']; # How to make plot and file-name meaningful if you call the variable in a loop? # if you call by name, the plotname is telling. It is similar what I would like to see. hist(my_df[,'bino']) for (plotit in colnames(my_df)) { hist(my_df[,plotit]) print (plotit) # this is already not meaningful } # step 2 write it into files hist_auto <- function(variable, col ="gold1", ...) { if ( length (variable) > 0 ) { plotname = paste(substitute(variable), sep="", collapse = "_"); print (plotname); is (plotname) # I would like to define plotname, and later tune it according to my needs FnP = paste (getwd(),'/',plotname, '.hist.pdf', collapse = "", sep=""); print (FnP) hist (variable, main = plotname) #this is apparently not working: I do not get my_df[, "bino"] or anything similar dev.copy2pdf (file=FnP ) } else { print ("var empty") } } hist_auto (my_vec) # name works, and is meaningful [as much as the var name ... ] hist_auto (my_df[,'bino']) # name sort of works, but falls apart assign (plotit, my_df[,'bino']) hist_auto (get(plotit)) # name works, but meaningless # Now in a loop for (plotit in colnames(my_df)) { my_df[,plotit] hist(my_df[,plotit]) ## name works, but meaningless and NOT UNIQUE > overwritten by next } for (plotit in colnames(my_df)) { hist_auto(my_df[,plotit]) ## name works, but meaningless and NOT UNIQUE > overwritten by next } for (plotit in colnames(my_df)) { assign (plotit, my_df[,plotit]) hist_auto (get(plotit)) ## name works, but meaningless and NOT UNIQUE > overwritten by next } 

My goal is to have a function that iterates, for example. columns of the matrix, graphics and stores each with a unique and meaningful name.

The solution will probably include a clever combination of substitute () parse () eval () and paste (), but without a clear understanding, I could not understand.

My experiment basis was: how to dynamically call a variable?

+5
source share
2 answers

How about something like that? You may need to install.packages("ggplot2")

 library(ggplot2) my_df <- data.frame(uni=runif(100), norm=rnorm(100), bino=rbinom(100, 20, 0.5)) get_histogram <- function(df, varname, binwidth=1, save=T) { stopifnot(varname %in% names(df)) title <- sprintf("Histogram of %s", varname) p <- (ggplot(df, aes_string(x=varname)) + geom_histogram(binwidth=binwidth) + ggtitle(title)) if(save) { filename <- sprintf("histogram_%s.png", gsub(" ", "_", varname)) ggsave(filename, p, width=10, height=8) } return(p) } for(var in names(my_df)) get_histogram(my_df, var, binwidth=0.5) # If you want to save them get_histogram(my_df, "uni", binwidth=0.1, save=F) # If you want to look at a specific one 
+1
source

So, I got 2 functions, one of which can iterate over data frames, and the other for one vector. Using parts of the Adrian solution [thanks!]:

hist_dataframe <- function(variable, col ="gold1", ...) { stopifnot(colName %in% colnames(df)) variable = df[,colName] stopifnot(length (variable) >1 ) plotname = paste(substitute(df),'__', colName, sep="") FnP = paste (getwd(),'/',plotname, '.hist.pdf', collapse = "", sep=""); print (FnP) hist (variable, main = plotname) dev.copy2pdf (file=FnP ) }

And the one that for simple vectors remains the same as in Q.

0
source

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


All Articles