I have data from three experiments. I want to import the data into R, perform some commands to clean/sort/transform the data etc., then plot a figure and save as a pdf. I need to do this for each of the three experiments. I know enough to know that copy-pasting the code 3 times is a bad idea. I assume the best way to go is to write a function for my cleaning/sorting/transforming and plotting, and then write a loop to call this function 3 times for each of my experiments.
What are the 'best practices' for handling a situation like this in R - for calling the same code for multiple experiments?
How do I define the name of my experiment as a variable, so that I can incorporate it into the name of the saved output file?
For example, if I write a function:
analyze <- function(experiment, genes) {
# other commands here...
# then plot and save:
pdf("experiment_and_genes_plot.pdf")
my_plot <- Heatmap(my_cleaned_data)
print(my_plot)
dev.off()
}
How do I incorporate the name of my experiment as a variable so that when I call
analyze(experimentA, genesA)
I get an output of experimentA_and_genesA_plot.pdf
Thanks so much, but when I try that I get
Thank you, but now it's printing the first entries from
experiment
andgenes
into the pdf name.I think the use of
experiment
andgenes
is calling these data frames, while I just want to call their names. Any ideas how to solve this?you can print the full name to see what it looks like
Any idea how I would do that?
print(paste0(experiment, "_and_", genes, "_plot.pdf"))
?What are your inputs? we have no idea what you did with them before that