gusl: (Default)
[personal profile] gusl
inspect <- function(stuff){
  q <- as.character(match.call()[2])
  if (length(stuff)==1 && !is.data.frame(stuff)) ##not a proper vector / matrix / complex object
    jCat(q, " = ", stuff)
  else 
    jCat(q, " = "); print(stuff)
}

> inspect(nv)  ## calls jCat to print everything in one line
nv = 14

> inspect(ranking)
ranking = 
 [1] "A1~C4" "C5~C6" "B1~C2" "A1~A3" "A2~A3" "B1~B4" "C3~C6" "C2~C5" "A1~C2"
[10] "B3~C6" "B1~C6"

> inspect(nv+5)
nv + 5 = 19


The obvious drawback is the need to write quotes around the expression, so I'm wondering if we can define macros in R in the style of Common Lisp's defmacro, i.e. something that will stop evaluation. Thanks [livejournal.com profile] serapio!

deparse + quote

Date: 2010-05-26 08:04 am (UTC)
From: [identity profile] gustavolacerda.livejournal.com
> deparse(quote(4+5))
[1] "4 + 5"
> dq <- function(x) deparse(quote(x))
> dq(4+5)
[1] "x"

(no subject)

Date: 2010-05-26 08:11 am (UTC)
From: [identity profile] gustavolacerda.livejournal.com
> compose <- function(f,g) function(x) f(g(x))
> dq <- compose(deparse,quote)
> dq(4+5)
[1] "x"
> 
> dq <- function(x) sapply(x, function(y) deparse(quote(y)))
> dq(4+5)
[1] "y"

(no subject)

Date: 2010-05-26 08:59 am (UTC)
From: [identity profile] serapio.livejournal.com
That's pretty cool. You can get the input as a string using match.call()
expr = as.character(match.call())[2]

(no subject)

Date: 2010-05-26 09:34 am (UTC)
From: [identity profile] gustavolacerda.livejournal.com
sweet! updated!

(no subject)

Date: 2010-05-26 09:48 am (UTC)
From: [identity profile] gustavolacerda.livejournal.com
I renamed it to "stuff", since we really shouldn't think of it as an expression.

'expr' should refer to 'q', if anything.

(no subject)

Date: 2010-05-26 09:51 am (UTC)
From: [identity profile] gustavolacerda.livejournal.com
and now, without calling 'eval', this should work anywhere, without having to worry about passing the environment!

February 2020

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526272829

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags