You're handing your function data structures, and you'd like it to be able to know the names of those data structures so that it can create a file name. I don't know how (or if) a function can know internally, the name of the variable it was called with, however, a simple solution would be to supply another argument for the file name.

analyze(experimentA, genesA, pdfname="experimentA_genesA_plot.pdf")

Or supply them as strings so that you can use paste() to create a name.

analyze(experimentA, genesA, "experimentA", "genesA")

Alternatively, there are functions that allow you to "get" data into a variable of a specific name, or "assign" a known name to a variable. This can be useful, for instance, if your data is in a file with a given name and you'd like to create data structures based on that name, or if you have have the names of data structures in your code, and you want to read data into a structure with that name. Look at the help for get() and assign(). In fact that might help you in this case:

# some data from experiment A
experimentA <- rnorm(5)

# function to analyze data
analyze <- function(x){
   # get the data named by the string x
    y <- get(x)
    cat(y, "n")
    cat("mean: ", mean(y), "n")
    # x is just a string so I can create a file name with it
    plotname <- paste0(x, "_plot.pdf")
    cat(plotname, "n")
}

# analyze data in a variable called experiment A
analyze("experimentA")

Result:

-0.0193227 0.7361533 1.334033 -0.1113091 0.2921196 
mean: 0.4463348 
experimentA_plot.pdf



Source link