[R] Dynamic regex/sub changes to function

Daniel Fuka drf28 at cornell.edu
Mon Sep 22 19:11:31 CEST 2014


Unfortunately in this specific case the owner/maintainer is a complete
idiot and a major jerk... he is.. well.. me in this case. But, this is
something I have also been wanting to figure out for some time as it
is often the case when a quick and simple regex based "patch" to a
function can be useful. I do not see why even when I "noquote()" the
assignment into a line of a "body()" it keeps adding the quotes and
associated escapes.



On Mon, Sep 22, 2014 at 12:01 PM, William Dunlap <wdunlap at tibco.com> wrote:
> If you really want to continue to use the function in the supported
> package, then you could try asking the maintainer of the package to
> make the problematic URL an argument to the function.  I thnk that
> changing the function on the fly, no matter how you do it, is likely
> to cause problems when the maintainer changes the function in a future
> release of the package.
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>
> On Mon, Sep 22, 2014 at 8:34 AM, Daniel Fuka <drf28 at cornell.edu> wrote:
>> Howdy Duncan,
>>
>> Thanks for the quick reply!  I must be missing something
>> simple/obvious. I need to have the "sub()" not return quoted and
>> escaped characters to "just edit the language expression". In my
>> problem, there is a function that is supported from a different
>> package. So I always want to use the supported function as my base...
>> but a url in the supported function needs to be changed dynamically
>> for my application, which is easiest using "sub()".
>>
>> I am trying to do what you correctly indicate I would need to do:
>> "just edit the language expression that body(fsong) gives you, and
>> assign it back"
>> BUT, using sub, I get back a quoted string in my example if I just use sed:
>>
>>> fsong
>> function(x){
>>  song=paste("my name is fuka,",x)
>>  return(song)
>> }
>> # Using "sub()" becomes:
>>> nsong
>> function (x)
>> {
>>     "song = paste(\"my name is muka,\", x)"
>>     return(song)
>> }
>>
>> Thanks again for the quick reply and help you are giving me!
>> dan
>>
>> On Mon, Sep 22, 2014 at 10:37 AM, Duncan Murdoch
>> <murdoch.duncan at gmail.com> wrote:
>>> On 22/09/2014 9:16 AM, Daniel Fuka wrote:
>>>>
>>>> Howdy,
>>>>
>>>> I have searched the lists and can not seem to find a solution to my
>>>> problem. I need to be able to dynamically modify a string inside a
>>>> function to build a new function. "sub" replaces with a quoted
>>>> string... and "parse" of "sub" returns expression... How can I get an
>>>> unquoted string from a regex to stick into a "body" of a function?
>>>
>>>
>>> It's possible to do what you want, though you don't want to be using
>>> parse(), you can just edit the language expression that body(fsong) gives
>>> you, and assign it back.  But that's a messy way to solve your problem.
>>>
>>> Why not create a new function containing the new string?  e.g.
>>>
>>> makefsong <- function(name = "fuka") {
>>>   line1 <- paste("my name is", name)
>>>   function(x) {
>>>     song <- paste(line1, x)
>>>     return(song)
>>>   }
>>> }
>>>
>>> f1 <- makefsong()
>>> f1("I live on the second floor")
>>> f2 <- makefsong("muka")
>>> f2("I live on the second floor")
>>>
>>> Duncan Murdoch
>>>
>>>>
>>>> Thanks for your help!
>>>> dan
>>>>
>>>> # Original Function
>>>> fsong=function(x){
>>>>   song=paste("my name is fuka,",x)
>>>>   return(song)
>>>> }
>>>> fsong("I live on the second floor")
>>>> #
>>>> # Copy and modify using "sub" returns quoted string with escaped quotes
>>>> #   internally... as expected.. which can not be evaluated.
>>>> nsong=fsong
>>>> body(nsong)[[grep("fuka",body(nsong))]]=
>>>>     sub("fuka","muka",list(body(fsong)[[grep("fuka",body(fsong))]]))
>>>>
>>>> nsong("I live on the second floor") # broken
>>>>
>>>> #
>>>> # Copy and modify with "parse" of  "sub",  returns expression.. but
>>>> without quotes,
>>>> # o getting closer.
>>>> #
>>>> nsong=fsong
>>>> body(nsong)[[grep("fuka",body(nsong))]]=
>>>>
>>>> parse(text=sub("fuka","muka",list(body(fsong)[[grep("fuka",body(fsong))]])))
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> 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
>> 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.



More information about the R-help mailing list