[R] How to extract function arguments literally

Shigeru Mase mase at is.titech.ac.jp
Sat Apr 30 05:24:09 CEST 2005


Dear all,

One of my friends asked me if it is possible to extract actual R
function arguments literally (precisely, as strings). The reason is
simple. He feels sometimes awkward to attach quotation marks :-). What
he actually wants is to pass R command arguments to XLisp subroutines
(He has been an enthusiastic XLisp user for a long time and still tends
to use R as a wrapper to XLisp). Is it possible, or should I advise him
not to be so lazy? The following is his simple example to explain the
situation.

R function "lc" which passes its argument to an Xlisp function:

 lc=function(cmd){
    cmd=as.character(substitute(cmd))
    .XLisp("lcommand", cmd)}

Corresponding XLisp function "lcommand":

 (defun lcommand (str) (eval (with-input-from-string (s str) (read s))))

If the argument cmd is a string (i.e. with quotation marks) or has no
space, it works fine.

> as.character(substitute(abc))
[1] "abc"

But, if it has no quotation marks or contains spaces, it yileds an error.

> as.character(substitute((def x1 x2))

Error: syntax error

He wants to use the syntax like lc((def x1 x2)), not like
lc("(def x1 x2)").

Thanks in advance.

==============================================================
Shigeru MASE <mase at is.titech.ac.jp>
        Tokyo Institute of Technology
        Dept. of Math. and Comp. Sciences
        O-Okayama 2-12-1-W8-28, Tokyo, 152-8550, Japan




More information about the R-help mailing list