Re: [Rd] In-string variable/symbol substitution: What formats/syntax is out there?

2013-12-17 Thread Gabor Grothendieck
On Tue, Dec 17, 2013 at 4:44 PM, Henrik Bengtsson  wrote:
> Hi,
>
> I'm try to collect a list of methods/packages available in R for doing
> in-string variable/symbol substitution, e.g. someFcn("pi=${pi}"),
> anotherFcn("pi=@pi@") and so on becomes "pi=3.141593".  I am aware of
> the following:
>
> ** gsubfn() in the 'gsubfn' package, e.g.
>> gsubfn( , , "pi = $pi, 2pi = `2*pi`")
> [1] "pi = 3.14159265358979, 2pi = 6.28318530717959"
>
>
> ** gstring() in the 'R.utils' package, e.g.
>> gstring("pi = ${pi}, 2pi = ${`2*pi`}")
> [1] "pi = 3.14159265358979, 2pi = 6.28318530717959"
>
>
> I'm sure there are other approaches - do you know of any in R?  They
> don't have to support in-line calculations such as in the first two
> examples, but if they do, it's a bonus.  I'm looking for simpler
> functions and not full blown literate programming methods (e.g.
> Sweave, noweb, knitr, brew, RSP, ...).  It should also be *in-string*
> substitution out of the box, so sub(), sprintf() and friends does not
> count.
>
> Thanks
>
> Henrik
>
> PS. The following is on the borderline because it does not do
> automatic variable look up, but since others may bring it up and/or
> know of a neater approach, I mention it too:
>
> ** copySubstitute() in the 'Biobase' package (with some efforts), e.g.
>> bbsubst <- function(fmt, ...) {
>   args <- lapply(list(...), FUN=as.character)
>   in <- textConnection(fmt)
>   out <- textConnection("res", open="w")
>   on.exit({ close(in); close(out) })
>   copySubstitute(in, out, symbolValues=args)
>   res
> }
>> bbsubst("pi = @pi@", pi=pi)
> [1] "pi = 3.14159265358979"

Note that the gsubfn example above is the default only but by
specifying the pattern argument (first arg) it can be changed. e.g.

library(gsubfn)

pat <- "[$]([[:alpha:]][[:alnum:].]*)|[$][{]([^}]*)[}]"
gsubfn(pat,, "pi=$pi 2pi=${2*pi}")

pat2 <- "@([^@]*)@"
gsubfn(pat2,, "pi=@pi@ 2pi=@2*pi@")

pat3 <- "%([^%]*)%"
gsubfn(pat3,, "pi=%pi% 2pi=%2*pi%")

pat4 <- "{{(.*?)}}"
gsubfn(pat4,, "pi={{pi}} 2pi={{2*pi}}")



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] In-string variable/symbol substitution: What formats/syntax is out there?

2013-12-17 Thread Peter Meilstrup
There's also knit_expand() in knitr, which uses the form "pi = {{pi}}"
with general expressions.

Peter

On Tue, Dec 17, 2013 at 1:44 PM, Henrik Bengtsson  wrote:
> Hi,
>
> I'm try to collect a list of methods/packages available in R for doing
> in-string variable/symbol substitution, e.g. someFcn("pi=${pi}"),
> anotherFcn("pi=@pi@") and so on becomes "pi=3.141593".  I am aware of
> the following:
>
> ** gsubfn() in the 'gsubfn' package, e.g.
>> gsubfn( , , "pi = $pi, 2pi = `2*pi`")
> [1] "pi = 3.14159265358979, 2pi = 6.28318530717959"
>
>
> ** gstring() in the 'R.utils' package, e.g.
>> gstring("pi = ${pi}, 2pi = ${`2*pi`}")
> [1] "pi = 3.14159265358979, 2pi = 6.28318530717959"
>
>
> I'm sure there are other approaches - do you know of any in R?  They
> don't have to support in-line calculations such as in the first two
> examples, but if they do, it's a bonus.  I'm looking for simpler
> functions and not full blown literate programming methods (e.g.
> Sweave, noweb, knitr, brew, RSP, ...).  It should also be *in-string*
> substitution out of the box, so sub(), sprintf() and friends does not
> count.
>
> Thanks
>
> Henrik
>
> PS. The following is on the borderline because it does not do
> automatic variable look up, but since others may bring it up and/or
> know of a neater approach, I mention it too:
>
> ** copySubstitute() in the 'Biobase' package (with some efforts), e.g.
>> bbsubst <- function(fmt, ...) {
>   args <- lapply(list(...), FUN=as.character)
>   in <- textConnection(fmt)
>   out <- textConnection("res", open="w")
>   on.exit({ close(in); close(out) })
>   copySubstitute(in, out, symbolValues=args)
>   res
> }
>> bbsubst("pi = @pi@", pi=pi)
> [1] "pi = 3.14159265358979"
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] In-string variable/symbol substitution: What formats/syntax is out there?

2013-12-17 Thread Duncan Murdoch

On 13-12-17 4:44 PM, Henrik Bengtsson wrote:

Hi,

I'm try to collect a list of methods/packages available in R for doing
in-string variable/symbol substitution, e.g. someFcn("pi=${pi}"),
anotherFcn("pi=@pi@") and so on becomes "pi=3.141593".  I am aware of
the following:

** gsubfn() in the 'gsubfn' package, e.g.

gsubfn( , , "pi = $pi, 2pi = `2*pi`")

[1] "pi = 3.14159265358979, 2pi = 6.28318530717959"


** gstring() in the 'R.utils' package, e.g.

gstring("pi = ${pi}, 2pi = ${`2*pi`}")

[1] "pi = 3.14159265358979, 2pi = 6.28318530717959"


I'm sure there are other approaches - do you know of any in R?  They
don't have to support in-line calculations such as in the first two
examples, but if they do, it's a bonus.  I'm looking for simpler
functions and not full blown literate programming methods (e.g.
Sweave, noweb, knitr, brew, RSP, ...).  It should also be *in-string*
substitution out of the box, so sub(), sprintf() and friends does not
count.


rgl has a function subst() used internally, mainly for substitutions in 
the Javascript that writeWebGL writes.


The equivalent of your example above would be

rgl:::subst("pi = %pi%, 2pi = %twopi%", pi = pi, twopi = 2*pi)

i.e. general expressions aren't supported, just specific named 
substitutions.


Duncan Murdoch



Thanks

Henrik

PS. The following is on the borderline because it does not do
automatic variable look up, but since others may bring it up and/or
know of a neater approach, I mention it too:

** copySubstitute() in the 'Biobase' package (with some efforts), e.g.

bbsubst <- function(fmt, ...) {

   args <- lapply(list(...), FUN=as.character)
   in <- textConnection(fmt)
   out <- textConnection("res", open="w")
   on.exit({ close(in); close(out) })
   copySubstitute(in, out, symbolValues=args)
   res
}

bbsubst("pi = @pi@", pi=pi)

[1] "pi = 3.14159265358979"

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel



__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] In-string variable/symbol substitution: What formats/syntax is out there?

2013-12-17 Thread Henrik Bengtsson
Hi,

I'm try to collect a list of methods/packages available in R for doing
in-string variable/symbol substitution, e.g. someFcn("pi=${pi}"),
anotherFcn("pi=@pi@") and so on becomes "pi=3.141593".  I am aware of
the following:

** gsubfn() in the 'gsubfn' package, e.g.
> gsubfn( , , "pi = $pi, 2pi = `2*pi`")
[1] "pi = 3.14159265358979, 2pi = 6.28318530717959"


** gstring() in the 'R.utils' package, e.g.
> gstring("pi = ${pi}, 2pi = ${`2*pi`}")
[1] "pi = 3.14159265358979, 2pi = 6.28318530717959"


I'm sure there are other approaches - do you know of any in R?  They
don't have to support in-line calculations such as in the first two
examples, but if they do, it's a bonus.  I'm looking for simpler
functions and not full blown literate programming methods (e.g.
Sweave, noweb, knitr, brew, RSP, ...).  It should also be *in-string*
substitution out of the box, so sub(), sprintf() and friends does not
count.

Thanks

Henrik

PS. The following is on the borderline because it does not do
automatic variable look up, but since others may bring it up and/or
know of a neater approach, I mention it too:

** copySubstitute() in the 'Biobase' package (with some efforts), e.g.
> bbsubst <- function(fmt, ...) {
  args <- lapply(list(...), FUN=as.character)
  in <- textConnection(fmt)
  out <- textConnection("res", open="w")
  on.exit({ close(in); close(out) })
  copySubstitute(in, out, symbolValues=args)
  res
}
> bbsubst("pi = @pi@", pi=pi)
[1] "pi = 3.14159265358979"

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel