0

I have a function that reads in multiple fields from a large csv file then does some calculations and plots the points according to the specified fields. I want to label the axes of the output plot with the field name entered in the function parameters. For example, you would choose for the sales field to be pulled from the csv and plotted on the y-axis and labeled sales.

QAD<-function(x.axis,y.axis,pt.size,quad.div.x,quad.div.y){
  dfull<-read.csv("QADR.csv",header=TRUE)
  ...
  plot(xd, yd, cex=size, xlab=deparse(substitute(x.axis))
    ,ylab=deparse(substitute(y.axis)))
  ...
}

deparse(substitute(x.axis)) is the closest solution I have found (How to convert variable (object) name into String) but that labels it y.axis instead of the field name. Is there any way I can store the input y.axis as a string?

Community
  • 1
  • 1
Walton
  • 101
  • 1
  • this looks like it would work, can you show an example of how you are calling that function? – Rorschach Aug 10 '15 at 16:06
  • Why don't you just enter the strings as strings to the function call and pass them to plot? They are meant to be quoted anyway, and it's much easier on the brain. – Rich Scriven Aug 10 '15 at 16:16
  • @nongkrong `QAD(margin,sales,amp,1000,5000)` – Walton Aug 10 '15 at 16:23
  • @RichardScriven there are lots of calculations performed on the field elements (grouping, summing, averages, etc.) so the modified dataset that is plotted `(xd,yd)` is not what is entered, but it is based on the field elements enetered. – Walton Aug 10 '15 at 16:26
  • Try using `deparse(substitute(x))` immediately (first line) after the argument list in the function, save it to a new variable name, then pass that variable to `plot()` later. – Rich Scriven Aug 10 '15 at 16:31
  • You'll need to show a reproducible example, otherwise we're just guessing – Rich Scriven Aug 10 '15 at 16:34
  • Well, the code I posted in the OP now accomplishes what I wanted. Thanks for your help though! – Walton Aug 10 '15 at 16:41

0 Answers0