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
Bill.Venables@csiro.au
2005-Apr-30 04:09 UTC
[R] How to extract function arguments literally
instead of as.character(substitute(arg)) use deparse(substitute(arg)) : -----Original Message----- : From: r-help-bounces at stat.math.ethz.ch : [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Shigeru Mase : Sent: Saturday, 30 April 2005 1:24 PM : To: r-help at stat.math.ethz.ch : Subject: [R] How to extract function arguments literally : : : 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 : : ______________________________________________ : R-help at stat.math.ethz.ch mailing list : https://stat.ethz.ch/mailman/listinfo/r-help : PLEASE do read the posting guide! : http://www.R-project.org/posting-guide.html :
Shigeru Mase wrote:> 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)").No, that wouldn't be possible. Since lc((def x1 x2)) is not legal S language, the parser is going to complain about it. The only solution here would be to parse the whole thing yourself (i.e. parse the R source), rather than using the R parser. Duncan Murdoch
Shigeru Mase <mase at is.titech.ac.jp> writes:> 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)").This is not possible. R expressions must follow R syntax. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907