[R] Dynamic regex/sub changes to function
Daniel Fuka
drf28 at cornell.edu
Mon Sep 22 15:16:54 CEST 2014
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?
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))]])))
More information about the R-help
mailing list