找回密码
 注册
查看: 3635|回复: 0

R语言:substitute()函数中文帮助文档(中英文对照)

[复制链接]
发表于 2012-2-16 19:02:17 | 显示全部楼层 |阅读模式
substitute(base)
substitute()所属R语言包:base

                                        Substituting and Quoting Expressions
                                         代和竞标表达

                                         译者:生物统计家园网 机器人LoveR

描述----------Description----------

substitute returns the parse tree for the (unevaluated) expression expr, substituting any variables bound in env.
substitute(未计算)的表达expr返回的解析树代env约束的任何变量。

quote simply returns its argument. The argument is not evaluated and can be any R expression.
quote只是简单地返回它的参数。的说法是不计算的,可以是任何R的表达。

enquote is a simple one-line utility which transforms a call of the form Foo(....) into the call quote(Foo(....)).  This is typically used to protect a call from early evaluation.
enquote是一个简单的行实用工具,它转换调用形式Foo(....)呼叫quote(Foo(....))。这通常是用来保护call从早期评估。


用法----------Usage----------


substitute(expr, env)
quote(expr)
enquote(cl)



参数----------Arguments----------

参数:expr
any syntactically valid R expression
任何语法上有效的R表达


参数:cl
a call, i.e., an R object of class (and mode) "call".
call,即,R对象(class和mode)"call"。


参数:env
an environment or a list object.  Defaults to the current evaluation environment.
环境或一个列表对象。默认为当前的评估环境。


Details

详情----------Details----------

The typical use of substitute is to create informative labels for data sets and plots. The myplot example below shows a simple use of this facility. It uses the functions deparse and substitute to create labels for a plot which are character string versions of the actual arguments to the function myplot.
substitute典型用途是创建数据集和图的信息标签。 myplot下面的例子显示了该设施的使用简单。它使用的功能deparse和substitute标签创建一个图,这是实际参数的字符串版本的功能myplot。

Substitution takes place by examining each component of the parse tree as follows: If it is not a bound symbol in env, it is unchanged.  If it is a promise object, i.e., a formal argument to a function or explicitly created using delayedAssign(), the expression slot of the promise replaces the symbol.  If it is an ordinary variable, its value is substituted, unless env is .GlobalEnv in which case the symbol is left unchanged.
换人需要通过检查每个组件的解析树的地方如下:如果不是在env束缚的象征,它是不变的。如果它是一个承诺的对象,也就是说,一个功能或使用delayedAssign()“的承诺,表达插槽替换符号明确创建一个正式的说法。如果它是一个普通的变量,其值将被替换,除非env是.GlobalEnv在这种情况下,留下的符号不变。

Both quote and substitute are "special" primitive functions which do not evaluate their arguments.
既quote和substitute是特殊的原始功能不评价他们的论据。


值----------Value----------

The mode of the result is generally "call" but may in principle be any type. In particular, single-variable expressions have mode "name" and constants have the appropriate base mode.
mode结果一般是"call"但可能会在原则上是任何类型。尤其是单变量表达式模式"name"和常量有适当的基本模式。


注意----------Note----------

substitute works on a purely lexical basis.  There is no guarantee that the resulting expression makes any sense.
substitute一个纯粹的词汇的基础上工作。有没有保证,导致表达任何意义。

Substituting and quoting often cause confusion when the argument is expression(...).  The result is a call to the expression constructor function and needs to be evaluated with eval to give the actual expression object.
代和报价时,往往会造成混乱的说法是expression(...)。其结果是一个expression构造函数的调用,需要用eval评估,给予实际的表达对象。


参考文献----------References----------

The New S Language. Wadsworth & Brooks/Cole.

参见----------See Also----------

missing for argument "missingness", bquote for partial substitution, sQuote and dQuote for adding quotation marks to strings,
missing参数missingness,bquote部分替代,sQuote和dQuote加引号字符串,

all.names to retrieve the symbol names from an expression or call.
all.names检索从一个表达式或调用的符号名。


举例----------Examples----------


require(graphics)
(s.e <- substitute(expression(a + b), list(a = 1)))  #> expression(1 + b)[>表达式(1 + b)]
(s.s <- substitute( a + b,            list(a = 1)))  #> 1 + b[> 1 + B]
c(mode(s.e), typeof(s.e)) #  "call", "language"[“呼叫”,“语言”]
c(mode(s.s), typeof(s.s)) #   (the same)[(同)]
# but:[但是:]
(e.s.e <- eval(s.e))          #>  expression(1 + b)[>表达式(1 + b)]
c(mode(e.s.e), typeof(e.s.e)) #  "expression", "expression"[“表达”,“表情”]

substitute(x <- x + 1, list(x=1)) # nonsense[废话]

myplot <- function(x, y)
    plot(x, y, xlab=deparse(substitute(x)),
         ylab=deparse(substitute(y)))

## Simple examples about lazy evaluation, etc:[#简单的例子,对懒惰的评价等:]

f1 <- function(x, y = x)             { x <- x + 1; y }
s1 <- function(x, y = substitute(x)) { x <- x + 1; y }
s2 <- function(x, y) { if(missing(y)) y <- substitute(x); x <- x + 1; y }
a <- 10
f1(a)# 11[11]
s1(a)# 11[11]
s2(a)# a[一]
typeof(s2(a))# "symbol"[“符号”]

转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。


注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2025-5-5 07:07 , Processed in 0.028763 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表