[R] Turning a variable name into a function argument

David Winsemius dwinsemius at comcast.net
Wed Jun 29 07:04:51 CEST 2016


> On Jun 28, 2016, at 6:19 PM, KMNanus <kmnanus at gmail.com> wrote:
> 
> You’ve stated my intent perfectly.  I tried depress(substitute(x)) within ggplot and it didn’t work.
> 
> However, the solution (which I discovered about 10 minutes ago), turned out to remarkably easy - I just assigned the new variable and it ran perfectly.  It looks like this - 
> 
> myfun<- function(z, q = “new.name){

You have used "smart-quotes". Not valid R syntax. Throws an error with the parser. Also would need a closing double quote and proper double quotes below as well.

> function(new.name){return(as.character(substitute(new.name)))}
> ggplot(df, aes(x,y))+
> geom_point()+
> ggtitle(paste(q, “quick brown fox….”))

Presumably you have also failed to include the final "}".

> 
> Not sure why assigning “new.name” to q makes the difference, but it did.

As soon as this function is called, (and not when it is defined), the name `q` is given a (character) value of "new.name". When than value is later looked up by `paste`, it is able to be evaluated and "new.name" is appended to the other character values. The "new.name" value, and the `q` name will be garbage-collectable after the function call.

The line:

 function(new.name){return(as.character(substitute(new.name)))}

... does ... absolutely  ... nothing ... as was implied in my response to your duplicate message that appeared on the server this morning.

-- 
David


> 
> Thanks.
> 
> 
> 
>>>>>> myfun <- function(z){
>>>>>> ggplot(df, aes(x,y)+
>>>>>> geom_point() +
>>>>>> ggtitle (“_______ quick brown fox jumped over the lazy dog”)}
> 
> 
> 
> 
> Ken
> kmnanus at gmail.com
> 914-450-0816 (tel)
> 347-730-4813 (fax)
> 
> 
> 
>> On Jun 28, 2016, at 8:53 PM, Jim Lemon <drjimlemon at gmail.com> wrote:
>> 
>> Hi Ken,
>> As far as I can see, ggtitle accepts a single string. The help page is
>> a bit obscure, implying that you can change the title with the "labs"
>> function(?), but using the same explicit string in the "ggtitle" line,
>> perhaps for didactic purposes. You seem to be asking to substitute
>> your own version of a string that is popping out in ggplot
>> automatically, e.g. "Tue" -> "Tuesday". The help page doesn't discuss
>> whether it is possible to access the string that will be automatically
>> used as the title. If ggtitle automatically uses the name of the
>> object that you are plotting, something like this may work:
>> 
>> my_x<-1:5
>> myfun<-function(x,mytitle) {
>> plot(x)
>> old_title<-deparse(substitute(x))
>> title(gsub("x",mytitle,old_title))
>> }
>> myfun(my_x,"wonderful X")
>> 
>> The reason that I have done this in base graphics is that I could not
>> get an equivalent plot in ggplot.
>> 
>> Jim
>> 
>> 
>> On Wed, Jun 29, 2016 at 9:48 AM, Bert Gunter <bgunter.4567 at gmail.com> wrote:
>>> I frankly don't know what the heck you are doing but,
>>> 
>>> (inline below)
>>> Bert Gunter
>>> 
>>> "The trouble with having an open mind is that people keep coming along
>>> and sticking things into it."
>>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>> 
>>> 
>>> On Tue, Jun 28, 2016 at 1:46 PM, KMNanus <kmnanus at gmail.com> wrote:
>>>> Thanks for getting back to me.  None of them work, because I’m trying to use the string in the function  call - myfun(z, “string”) to replace the empty space in ggtitle.
>>>> 
>>>> ***When I call myfun(z, gsub(“______”, “string (or any word)”, myfun), I get an error msg because gsub is looking for a data frame, not a function.***
>>> 
>>> That is absolutely, unequivocally, positively, FALSE. See ?gsub for
>>> what gsub() *does* want.
>>> 
>>> This suggests to me that you may also not understand functions and/or
>>> function arguments, so I would recommend that you try a web tutorial
>>> or two on R function to see where your confusion may lie. However, I
>>> freely admit (see my initial remark) that I may not understand what
>>> you are trying to do, so maybe that's not it. I will say if I wanted
>>> to give an arbitrary character string to a function that called a
>>> title function, titleFUN, I'd do it like this:
>>> 
>>> myfun <- function(..., mytitle){
>>> 
>>> ## lots of stuff
>>> 
>>> titleFUN(mytitle)
>>> 
>>> ## more stuff
>>> 
>>> }
>>> 
>>> and call it by:
>>> 
>>> myfun(..., "myReallyCuteTitle")
>>> 
>>> 
>>> I do not use ggplot and so do not know its detailed syntax; but I
>>> would be surprised if it did not accept something along these lines...
>>> 
>>> Cheers,
>>> Bert
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>>> 
>>>> Paste or paste0 doesn’t work, either because I still have to replace the space in ggtitle with something.
>>>> 
>>>> In short, I want to put another argument into the function that will enable me to call it and fill that space.
>>>> 
>>>> I’m stumped.
>>>> 
>>>> 
>>>> Ken
>>>> kmnanus at gmail.com
>>>> 914-450-0816 (tel)
>>>> 347-730-4813 (fax)
>>>> 
>>>> 
>>>> 
>>>>> On Jun 28, 2016, at 3:42 PM, Greg Snow <538280 at gmail.com> wrote:
>>>>> 
>>>>> There are several options.  The option that is most like search and
>>>>> replace is to use the `sub` or `gsub` function (or similar functions
>>>>> in added packages).  But you may be able to accomplish what you want
>>>>> even simpler by using the `paste`, `paste0`, or `sprintf` functions.
>>>>> 
>>>>> On Tue, Jun 28, 2016 at 12:10 PM, KMNanus <kmnanus at gmail.com> wrote:
>>>>>> Thanks for getting back to me, I’m sorry if I was unclear.
>>>>>> 
>>>>>> What I’m trying to figure out is the equivalent of “find and replace” in Word.
>>>>>> 
>>>>>> I have a function -
>>>>>> 
>>>>>> myfun <- function(z){
>>>>>> ggplot(df, aes(x,y)+
>>>>>> geom_point() +
>>>>>> ggtitle (“_______ quick brown fox jumped over the lazy dog”)}
>>>>>> 
>>>>>> Calling myfun(z) works perfectly.  What I’m trying to do is add a string to myfun so that it would read:  function(z, “string”){
>>>>>> 
>>>>>> Then I could call myfun(z, “string”) to replace the space in ggtitle.  Is there a straightforward way to do that?
>>>>>> 
>>>>>> Ken
>>>>>> kmnanus at gmail.com
>>>>>> 914-450-0816 (tel)
>>>>>> 347-730-4813 (fax)
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>>> On Jun 28, 2016, at 12:20 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>>>>>>> 
>>>>>>>> 
>>>>>>>> On Jun 27, 2016, at 6:12 PM, KMNanus <kmnanus at gmail.com> wrote:
>>>>>>>> 
>>>>>>>> I’m inexperience but am trying to get my head around using functions to make a number of ggplots easier to do.
>>>>>>>> 
>>>>>>>> I have a function that creates a ggplot taking one input variable as an argument. The variable name is shorthand for the actual variable (variable name = tue, Actual name = Tuesday).  Since I want to use the actual variable name in ylab and ggtitle, I’d like to add a second argument, new.name, to the function which would allow me to utilize both inputs as arguments but have not been successful.  I tried creating a function within the function to accomplish this, using deparse(substitute(new.name))and also using the code you see below.
>>>>>>>> 
>>>>>>>> 
>>>>>>>> myfun <- function(myvar, new.name){
>>>>>>>> function(new.name){return(as.character(substitute(new.name)))}
>>>>>>>> ggplot(b12.2, aes(x= games,  y = myvar, col = Group))+
>>>>>>>> geom_point() +
>>>>>>>> geom_line()+
>>>>>>>> xlab("Minimum Games" ) +
>>>>>>>> ylab(paste(new.name, “Average Change"))+
>>>>>>>> ggtitle(new.name, "Change \n as a Function of Minimum Number of Games”)+
>>>>>>>> theme_bw()
>>>>>>>> 
>>>>>>>> When call myfun(myvar, new.name), I get an error msg “new.name is not found” whether I call new.name or Tuesday.
>>>>>>> 
>>>>>>> Q1: At the moment we have no idea _how_ you might be "calling" this function. We also do not know what might be assigned to `myvar` or `new.name` in the calling environment. Sounds unlikely that you are typing:
>>>>>>> 
>>>>>>> myfun(myvar, new.name)  ## ?, so was there a loop/lapply calling method?
>>>>>>> 
>>>>>>> 
>>>>>>> Q2: You should not imagine that the inner anonymous function would be altering the value of `new.name`. (That function is only defined and never called, and even if it were called, it would not change the value of the `new.name` in the calling environment.)
>>>>>>> 
>>>>>>> Since more than 12 hours have passed with no response, we can surmise that many people have passed the question over after concluding there was an incomplete problem description. You should post code that can be cut-pasted into a session and produce the error you are getting. It would include data setup and a loop or loop equivalent to show how the function is being called.
>>>>>>> 
>>>>>>> --
>>>>>>> David.
>>>>>>> 
>>>>>>> 
>>>>>>>> 
>>>>>>>> I want ggplot to automatically insert Tuesday into ylab and ggtitle.
>>>>>>>> 
>>>>>>>> Can anyone help me with this?  Thanks for your patience.
>>>>>>>> 
>>>>>>>> Ken
>>>>>>>> kmnanus at gmail.com <mailto:kmnanus at gmail.com>
>>>>>>>> 914-450-0816 (tel)
>>>>>>>> 347-730-4813 (fax)
>>>>>>>> 
>>>>>>>> 
>>>>>>>> 
>>>>>>>> ______________________________________________
>>>>>>>> R-help at r-project.org <mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see
>>>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help <https://stat.ethz.ch/mailman/listinfo/r-help>
>>>>>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html <http://www.r-project.org/posting-guide.html>
>>>>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>>> 
>>>>>>> David Winsemius
>>>>>>> Alameda, CA, USA
>>>>>> 
>>>>>> ______________________________________________
>>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>> 
>>>>> 
>>>>> 
>>>>> --
>>>>> Gregory (Greg) L. Snow Ph.D.
>>>>> 538280 at gmail.com
>>>> 
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>> 
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list