Hi all, I want to do this kind of function In R enviroment : For example : R <- 4 testString <- "I love $R" then search this testString, when find "$R",replace "$R" to R ,and because the value of R is 4 So the final string I want to get is "I love 4" How can I implement? Thanks advance Michael
peng shen <michael_shen <at> hotmail.com> writes: : : Hi all, : I want to do this kind of function In R enviroment : : For example : : R <- 4 : testString <- "I love $R" : then search this testString, when find "$R",replace "$R" to R ,and because : the value of R is 4 : So the final string I want to get is "I love 4" : How can I implement? Thanks advance : Here is one way to do string interpolation: R> interp <- function(x, e = parent.frame(), pre = "\\$", post = "" ) { + for(el in ls(e)) { + tag <- paste(pre, el, post, sep = "") + if (length(grep(tag, x))) x <- gsub(tag, eval(parse(text = el), e), x) + } + x + } R> # a test R> R <- 4 R> x <- "I love $R" R> interp(x) [1] "I love 4" R> # another test R> y <- "I love ${R}" R> interp(y, pre = "\\${", post = "}") [1] "I love 4"
On Sun, 6 Mar 2005, peng shen wrote:> Hi all, > I want to do this kind of function In R enviroment : > For example : > R <- 4 > testString <- "I love $R" > then search this testString, when find "$R",replace "$R" to R ,and because > the value of R is 4 > So the final string I want to get is "I love 4" > How can I implement? Thanks advancesub("$R", R, testString, fixed = TRUE) -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
peng shen [mailto:michael_shen at hotmail.com] wrote:> R <- 4 > testString <- "I love $R" > So the final string I want to get is "I love 4". How can I implement?I've written an interpolater function "g.p" with these additional features: - Loops through all occurences of the escape character ($) rather than all external variables. - Variable names are terminated by any non-alphanumeric char or a "silent" $$. - Variables may come from the parent environment or from named arguments. - Pastes together its unnamed arguments with sep="", useful for long strings. Example: R> R <- 4 R> testString <- "I love $R" R> g.p(testString) [1] "I love 4" Fancier example: R> var1 <- 7 R> var2 <- 5 R> g.p("Add $var1 to $var2 to calc", "ulate $var3", var3=var1+var2) [1] "Add 7 to 5 to calculate 12" Function definition: g.p <- function(..., esc="\\$", sep="", collapse=" ", parent=1) { a <- lapply(list(...), as.character) n <- names(a); if (is.null(n)) n <- rep("", length(a)) s <- do.call("paste", c(a[n==""], sep=sep, collapse=collapse)) for (i in which(n != "")) s <- gsub(paste(esc,n[i],sep=""), a[[i]], s) while ((r <- regexpr(paste(esc,"\\w*",sep=""), s)) > 0) { v <- substring(s, r+1, r+attr(r,"match.length")-1) s <- if (v=="") paste(substring(s,1,r-1), substring(s,r+2), sep="") else gsub(paste(esc,v,sep=""), as.character(eval.parent(parse(text=v), parent)), s) } s } -- David Brahm (brahm at alum.mit.edu)