I was wondering if anyone could help me use a variable name within a function.
I've put together a dot plot that sorts variables and then produces a bitmap, but I can't get R
to pass the variable name to the plot
title.
Example data
id<-c(1,2,3)
blood<-c(1,2,10)
weight<-c(1,2,13)
mydata<-as.data.frame(cbind(id,blood,weight))
mydata$blood
#######SORTED DOT PLOT####
Dplotter<-function (id,x,Title=""){
if (is.null(Title)) {Title=""} else {Title=Title}
DIR<-paste("C:/temp/WholePlots/New/",Title,".bmp",sep="")
D<-as.data.frame(cbind(id,x))
x1<-as.data.frame(D[order(x),])
bmp(DIR)
dotchart(x1$x,labels=id,main=Title,pch=16)
dev.off()
}
###############
Dplotter(mydata$id,mydata$blood,"Blood")
Dplotter(mydata$id,mydata$weight,"Weight")
In the second line of the function, I'd like to pass on the variable name, something like
`if (is.null(Title)) {Title=varname(x)} else {Title=Title}`
so that I don't have to put "Blood" in the function Title field (e.g. Dplotter(mydata$id,mydata$blood)
Basically, how does one paste in the variable name in a function? It would be even better if one could take out the dataset name from the Title (without attaching the dataset, which I've been told is bad practice), so that instead of getting
mydata$blood
, you just get "blood" in the title.I've failed to find an easy solution to paste in a variable name in a function. As you can guess, putting the variable name in a
paste()
function returns the values of the variable (so that the plot title is filled with values rather the variable name).I'd also like to automate the function even further, so that I can just put the dataset and the ID,and then have the function repeated for each variable in the dataset. Obviously this requires solving question 1 first, otherwise both title and filenames will encounter problems.