ivo welch
2013-Feb-09 00:01 UTC
[R] character strings with embedded commands: perl "/gee" ?
dear R experts---I am trying to replicate a perl feature. I want to be able to embed R commands inside a character string, and have the string be printed with the command executed. my perl equivalent is my $a=10; my $teststring = "the expression, $a+1, is ::$a+1::, but add one more for ::$a+2::\n"; $teststring =~ s/::(.*?)::/$1/gee; print $teststring; of course, R does not use '$' for variable names. my ultimate goal is to write something like cat("d is a ::class(d):: with names ::names(d)::") of course, I know I can write this as cat("d is a", class(d), "with names", names(d)), but I also want to be define %or% so that I can write (is.data.frame(d)) %or% "d is a ::class(d):: with names ::names(d)::" ; operators don't take variable arguments afaik. :-(. advice appreciated. regards, /iaw ---- Ivo Welch (ivo.welch at gmail.com)
ivo welch
2013-Feb-09 19:03 UTC
[R] character strings with embedded commands: perl "/gee" ?
joshua wiley told me how to do this. so, here is something I find quite useful: estring <- function (e2) { syntax <- regmatches(e2, gregexpr("(?<=\\{\\{).*?(?=\\}\\})", e2, perl=TRUE))[[1]] res <- lapply(syntax, function(p) eval(parse(text=p))) res <- lapply(1:length(res), function(i) { e2 <<- gsub(paste0("{{", syntax[i], "}}"), res[i], e2, fixed=TRUE) }) e2 } abort.estring <- function(s) abort(estring(s)) abort <- function (...) { cat(..., file=stderr) stop(simpleError("Aborting --- use options(error=recover) and traceback() for debugging\n") } die <- abort `%or%` <- function(e1, e2) { if (!e1) { if (is.character(e2)) abort.estring(e2) else eval(e2) }} `%and%` <- function(e1, e2) { if (e1) { if (is.character(e2)) abort.estring(e2) else eval(e2) }} together, this is useful for such constructs as f <- function(a1) { (is.data.frame(a1)) %or% "a1 is not a data frame but a {{class(a1)}}" (exists("column", a1)) %or% "sorry, but column named 'column' does not exist. choose from {{names(a1)}} instead" (is.numeric(a1$column)) %or% "column a1 is not numeric" } I also find it useful to have an optional length argument for is.numeric or is.character, which requires the argument to be of length x, but this is another story. /iaw On Fri, Feb 8, 2013 at 4:01 PM, ivo welch <ivo.welch at anderson.ucla.edu> wrote:> dear R experts---I am trying to replicate a perl feature. I want to be > able to embed R commands inside a character string, and have the > string be printed with the command executed. my perl equivalent is > > my $a=10; > my $teststring = "the expression, $a+1, is ::$a+1::, but add one > more for ::$a+2::\n"; > $teststring =~ s/::(.*?)::/$1/gee; > print $teststring; > > of course, R does not use '$' for variable names. my ultimate goal is > to write something like > > cat("d is a ::class(d):: with names ::names(d)::") > > of course, I know I can write this as cat("d is a", class(d), "with > names", names(d)), but I also want to be define %or% so that I can > write > > (is.data.frame(d)) %or% "d is a ::class(d):: with names ::names(d)::" ; > > operators don't take variable arguments afaik. :-(. > > advice appreciated. > > regards, > > /iaw > ---- > Ivo Welch (ivo.welch at gmail.com)
Greg Snow
2013-Feb-14 00:31 UTC
[R] character strings with embedded commands: perl "/gee" ?
Look at the gsubfn package, it has functionality to do this. Here is an example from the vignette:> fn$cat("pi = $pi, exp = `exp(1)`\n")pi = 3.14159265358979, exp = 2.71828182845905 The formula syntax to function conversion in the package may help with the last part. On Fri, Feb 8, 2013 at 5:01 PM, ivo welch <ivo.welch@anderson.ucla.edu>wrote:> dear R experts---I am trying to replicate a perl feature. I want to be > able to embed R commands inside a character string, and have the > string be printed with the command executed. my perl equivalent is > > my $a=10; > my $teststring = "the expression, $a+1, is ::$a+1::, but add one > more for ::$a+2::\n"; > $teststring =~ s/::(.*?)::/$1/gee; > print $teststring; > > of course, R does not use '$' for variable names. my ultimate goal is > to write something like > > cat("d is a ::class(d):: with names ::names(d)::") > > of course, I know I can write this as cat("d is a", class(d), "with > names", names(d)), but I also want to be define %or% so that I can > write > > (is.data.frame(d)) %or% "d is a ::class(d):: with names ::names(d)::" ; > > operators don't take variable arguments afaik. :-(. > > advice appreciated. > > regards, > > /iaw > ---- > Ivo Welch (ivo.welch@gmail.com) > > ______________________________________________ > R-help@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. >-- Gregory (Greg) L. Snow Ph.D. 538280@gmail.com [[alternative HTML version deleted]]