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

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

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

                                        Interactive Tracing and Debugging of Calls to a Function or Method
                                         调用一个函数或方法的互动跟踪和调试

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

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

A call to trace allows you to insert debugging code (e.g., a call to browser or recover) at chosen places in any function.  A call to untrace cancels the tracing. Specified methods can be traced the same way, without tracing all calls to the function.  Trace code can be any R expression.  Tracing can be temporarily turned on or off globally by calling tracingState.
一个trace调用允许你插入调试代码(例如,调用browser或recover)在选择功能在任何地方。一个untrace调用取消跟踪。指定的方法可以追溯到相同的方式,而不跟踪所有调用的功能。跟踪代码可以是任何R的表达。跟踪可以暂时被打开或关闭全局调用tracingState。


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


trace(what, tracer, exit, at, print, signature,
      where = topenv(parent.frame()), edit = FALSE)
untrace(what, signature = NULL, where = topenv(parent.frame()))

tracingState(on = NULL)
.doTrace(expr, msg)



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

参数:what
The name (quoted or not) of a function to be traced or untraced.  For untrace or for trace with more than one argument, more than one name can be given in the quoted form, and the same action will be applied to each one.
一个被跟踪或untraced函数的名称(或引用)。 untrace或trace多个参数,多个名字可以给出报价的形式,将被应用到每一个相同的动作。


参数:tracer
Either a function or an unevaluated expression.  The function will be called or the expression will be evaluated either at the beginning of the call, or before those steps in the call specified by the argument at. See the details section.
无论是功能或不计算表达式。该函数将被调用或表达式将在开始呼叫,或之前在由参数at指定的呼叫这些步骤进行评估。看到细节部分。


参数:exit
Either a function or an unevaluated expression.  The function will be called or the expression will be evaluated on exiting the function. See the details section.
无论是功能或不计算表达式。该函数将被调用,或表达将退出功能进行评估。看到细节部分。


参数:at
optional numeric vector or list.  If supplied, tracer will be called just before the corresponding step in the body of the function. See the details section.  
可选的数字向量或列表。如果提供,tracer将调用相应的步骤之前,在函数体。看到细节部分。


参数:print
If TRUE (as per default), a descriptive line is printed before any trace expression is evaluated.
如果TRUE(按默认),描述线印之前任何痕迹表达评估。


参数:signature
If this argument is supplied, it should be a signature for a method for function what.  In this case, the method, and not the function itself, is traced.
如果这种说法是提供的,它应该是一个功能what方法的签名。在这种情况下,被跟踪的方法,而不是函数本身,。


参数:edit
For complicated tracing, such as tracing within a loop inside the function, you will need to insert the desired calls by editing the body of the function.  If so, supply the edit argument either as TRUE, or as the name of the editor you want to use.  Then trace() will call edit and use the version of the function after you edit it.  See the details section for additional information.  
对于复杂,如在一个函数内的循环跟踪追查,你会需要插入编辑功能体所需的来电。如果是这样,提供edit参数TRUE,或作为编辑器的名称,您要使用。然后trace()将调用edit和使用的功能编辑后的版本。更多信息,请参阅细节部分。


参数:where
where to look for the function to be traced; by default, the top-level environment of the call to trace.  An important use of this argument is to trace a function when it is called from a package with a namespace.  The current namespace mechanism imports the functions to be called (with the exception of functions in the base package).  The functions being called are not the same objects seen from the top-level (in general, the imported packages may not even be attached).  Therefore, you must ensure that the correct versions are being traced.  The way to do this is to set argument where to a function in the namespace.  The tracing computations will then start looking in the environment of that function (which will be the namespace of the corresponding package). (Yes, it's subtle, but the semantics here are central to how namespaces work in R.)  
哪里可以追溯到功能,默认情况下,调用trace的,顶级的环境。这一论点的一个重要用途是跟踪功能,当它被称为包与命名空间。当前的命名空间机制导入的功能,被称为(在基础包的功能异常)。被调用的功能是不是看到了同样的对象,从顶层(进口包在一般情况下,甚至可能不被重视)。因此,你必须确保正确的版本正在追查。办法做到这一点,是要建立一个命名空间中的功能参数where。跟踪计算,然后将启动环境功能(这将是相应的包的命名空间)。 (是的,这是微妙的,但这里的语义是中央命名空间如何在R)


参数:on
logical; a call to  the support function tracingState returns TRUE if tracing is globally turned on, FALSE otherwise.  An argument of one or the other of those values sets the state.  If the tracing state is FALSE, none of the trace actions will actually occur (used, for example, by debugging functions to shut off tracing during debugging).
逻辑;支持功能的调用tracingState回报TRUE如果跟踪全球,FALSE否则转向。一个或这些值的其他参数设置状态。跟踪状态,如果是FALSE,跟踪行动实际上会发生(使用,例如调试功能关闭在调试过程中的跟踪)。


参数:expr, msg
arguments to the support function .doTrace, calls to which are inserted into the modified function or method: expr is the tracing action (such as a call to browser(), and msg is a string identifying the place where the trace action occurs.  
支持功能.doTrace,是插入到修改后的函数或方法的调用参数:expr是跟踪行动(如呼叫browser()和msg识别跟踪动作发生的地方是一个字符串。


Details

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

The trace function operates by constructing a revised version of the function (or of the method, if signature is supplied), and assigning the new object back where the original was found. If only the what argument is given, a line of trace printing is produced for each call to the function (back compatible with the earlier version of trace).
trace函数构造函数的修订版本(或方法,如果signature提供),并分配新的对象,发现原来经营。如果只给出what参数,跟踪印刷生产线是为每个呼叫的功能(与早期版本的trace兼容)。

The object constructed by trace is from a class that extends "function" and which contains the original, untraced version. A call to untrace re-assigns this version.
trace构造的对象是从一个类扩展"function"“包含原,untraced版本。一个调用untrace重新分配这个版本。

If the argument tracer or exit is the name of a function, the tracing expression will be a call to that function, with no arguments.  This is the easiest and most common case, with the functions browser and recover the likeliest candidates; the former browses in the frame of the function being traced, and the latter allows browsing in any of the currently active calls.
如果参数tracer或exit是一个函数的名称,跟踪的表达将成为该函数调用不带参数。这是最简单和最常见的情况,职能browser和recover最有可能的候选人;前浏览在帧被跟踪的功能,后者可以浏览目前活跃任何呼叫。

The tracer or exit argument can also be an unevaluated expression (such as returned by a call to quote or substitute).  This expression itself is inserted in the traced function, so it will typically involve arguments or local objects in the traced function.  An expression of this form is useful if you only want to interact when certain conditions apply (and in this case you probably want to supply print=FALSE in the call to trace also).
tracer或exit参数,也可以不计算表达式(例如通过调用返回quote或substitute)。这个表达式本身中插入跟踪的功能,所以它通常会涉及到参数或在本地对象跟踪功能。这种形式的表达,是有用的,如果你只是想互动,在一定条件下申请(在这种情况下,你可能想提供print=FALSEtrace也调用)。

When the at argument is supplied, it can be a vector of integers referring to the substeps of the body of the function (this only works if the body of the function is enclosed in { ...}.  In this case tracer is not called on entry, but instead just before evaluating each of the steps listed in at.  (Hint: you don't want to try to count the steps in the printed version of a function; instead, look at as.list(body(f)) to get the numbers associated with the steps in function f.)
当at参数提供,它可以是指身体功能(这只是工程分步骤如果函数体{ ...}括起来的整数向量。在这种情况下tracer不叫条目,但之前,而不是纯粹评价at(提示中列出的步骤:你不想尝试计数函数的印刷版本的步骤;相反,看看as.list(body(f))得到的数量与功能的步骤f。)

The at argument can also be a list of integer vectors.  In this case, each vector refers to a step nested within another step of the function.  For example, at = list(c(3,4)) will call the tracer just before the fourth step of the third step of the function.  See the example below.
at参数,也可以是一个整数向量的名单。在这种情况下,每个向量是指嵌套在函数的另一个步骤一个步骤。例如,at = list(c(3,4))将调用函数的第三步第四步之前的示踪剂。请看下面的例子。

Using setBreakpoint (from package utils) may be an alternative, calling trace(...., at, ...).
使用setBreakpoint(从包utils)可能是一种替代方法,调用trace(...., at, ...)。

An intrinsic limitation in the exit argument is that it won't work if the function itself uses on.exit, since the existing calls will override the one supplied by trace.
在exit参数的内在限制是,它不会工作,如果函数本身使用on.exit,因为现有的呼叫将覆盖一个trace提供的。

Tracing does not nest.  Any call to trace replaces previously traced versions of that function or method (except for edited versions as discussed below), and untrace always restores an untraced version.  (Allowing nested tracing has too many potentials for confusion and for accidentally leaving traced versions behind.)
跟踪不筑巢。任何调用的trace,函数或方法取代先前追踪的版本(除下文所讨论的编辑版本),untrace始终还原untraced的的版本。 (允许嵌套追踪混乱和意外离开追溯到版本背后有太多的潜力)。

When the edit argument is used repeatedly with no call to untrace on the same function or method in between, the previously edited version is retained.  If you want to throw away all the previous tracing and then edit, call untrace before the next call to trace.  Editing may be combined with automatic tracing; just supply the other arguments such as tracer, and the edit argument as well.  The edit=TRUE argument uses the default editor (see edit).
当edit参数,反复使用调用untrace以前编辑过的版本相同的功能或方法之间,保留没有。如果你想扔掉以前所有的跟踪,然后编辑,调用untrace前trace下次调用。编辑可结合自动跟踪,只需提供如tracer,edit参数以及其他参数。 edit=TRUE参数使用默认的编辑器(见edit)。

Tracing primitive functions (builtins and specials) from the base package works, but only by a special mechanism and not very informatively.  Tracing a primitive causes the primitive to be replaced by a function with argument ... (only).  You can get a bit of information out, but not much.  A warning message is issued when trace is used on a primitive.
跟踪从基础包工程的原始功能(建宏和特价),但只有通过一种特殊的机制,而不是非常请提供知识。跟踪原始导致原始的一个参数的函数替换... (只)。你可以得到一个比特的信息,但数量不多。发出一个警告消息时trace是一种原始的使用。

The practice of saving the traced version of the function back where the function came from means that tracing carries over from one session to another, if the traced function is saved in the session image.  (In the next session, untrace will remove the tracing.)  On the other hand, functions that were in a package, not in the global environment, are not saved in the image, so tracing expires with the session for such functions.
保存版本的功能函数来跟踪从一个会议到另一个,如果跟踪功能保存在会话中的形象的手段从跟踪的做法。 (在下届会议上,untrace将删除跟踪)。另一方面,功能包,在全球环境,不保存图像,所以追查过期等与会议功能。

Tracing a method is basically just like tracing a function, with the exception that the traced version is stored by a call to setMethod rather than by direct assignment, and so is the untraced version after a call to untrace.
跟踪的方法基本上是一样跟踪功能,异常跟踪版本是由调用的setMethod,而不是直接分配存储,所以是untraced版本后调用untrace 。

The version of trace described here is largely compatible with the version in S-Plus, although the two work by entirely different mechanisms.  The S-Plus trace uses the session frame, with the result that tracing never carries over from one session to another (R does not have a session frame).  Another relevant distinction has nothing directly to do with trace:  The browser in S-Plus allows changes to be made to the frame being browsed, and the changes will persist after exiting the browser.  The R browser allows changes, but they disappear when the browser exits.  This may be relevant in that the S-Plus version allows you to experiment with code changes interactively, but the R version does not.  (A future revision may include a "destructive" browser for R.)
trace这里所描述的版本主要是用在S-PLUS的版本兼容,虽然由两个完全不同的机制工作。 S-PLUStrace使用会话框,结果,从未进行跟踪从一个会议到另一个(R不具有会话帧)。另一个相关的区别,有没有直接做以trace:S-PLUS的浏览器允许进行更改,正在浏览的框架,将坚持退出浏览器后的变化。在R浏览器允许的变化,但他们消失时,退出浏览器。这可能是有关在S-PLUS版本,让您更改代码交互试验,但R版本不。 (可能包括将来的修订版“破坏性的”河的浏览器)


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

In the simple version (just the first argument), invisible NULL. Otherwise, the traced function(s) name(s).  The relevant consequence is the assignment that takes place.
在简单的版本(只是第一个参数),无形的NULL。否则,跟踪功能(S)的名称(S)。有关后果是转让发生。


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

The version of function tracing that includes any of the arguments except for the function name requires the methods package (because it uses special classes of objects to store and restore versions of the traced functions).
跟踪,包括任何除了函数名参数的函数的版本需要methods包(因为它使用了类特殊的对象,存储和跟踪功能恢复的版本)。

If methods dispatch is not currently on, trace will load the methods namespace, but will not put the methods package on the search list.
如果方法派遣目前不上,trace将加载的方法命名空间,但不会放在搜索列表的方法包。


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

The New S Language. Wadsworth & Brooks/Cole.

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

browser and recover, the likeliest tracing functions; also, quote and substitute for constructing general expressions.
browser和recover,可能跟踪功能;也quote和substitute建设的一般表达式。


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


require(graphics)

##  Very simple use[#使用非常简单]
trace(sum)
hist(stats::rnorm(100)) # shows about 3-4 calls to sum()[约3-4来电显示SUM()]
untrace(sum)

if(.isMethodsDispatchOn()) { # non-simple use needs 'methods' package[非简单的使用需求的方法包]

f <- function(x, y) {
    y <- pmax(y, 0.001)
    if (x > 0) x ^ y else stop("x must be positive")
}

## arrange to call the browser on entering and exiting[#安排进入和退出,调用浏览器]
## function f[#函数f]
trace("f", quote(browser(skipCalls=4)),
      exit = quote(browser(skipCalls=4)))

## instead, conditionally assign some data, and then browse[#而不是分配,有条件的一些数据,然后浏览]
## on exit, but only then.  Don't bother me otherwise[#退出,但只有。不要打扰我,否则]

trace("f", quote(if(any(y < 0)) yOrig <- y),
      exit = quote(if(exists("yOrig")) browser(skipCalls=4)),
      print = FALSE)

## Enter the browser just before stop() is called.  First, find[#进入浏览器之前停止()被调用。首先,找到]
## the step numbers[#的步数]

as.list(body(f))
as.list(body(f)[[3]])

## Now call the browser there[#现在有调用浏览器]

trace("f", quote(browser(skipCalls=4)), at=list(c(3,4)))

## trace a utility function, with recover so we[#跟踪的实用功能,与恢复,所以我们]
## can browse in the calling functions as well.[#可以浏览以及通话功能。]

trace("as.matrix", recover)

## turn off the tracing[#关闭追踪]

untrace(c("f", "as.matrix"))

## Not run: [#无法运行:]
## trace calls to the function lm() that come from[#跟踪流明()函数的调用来自]
## the nlme package.[#nlme包。]
## (The function nlme is in that package, and the package[#(函数nlme是在那个包,包]
## has a namespace, so the where= argument must be used[#有一个命名空间,所以必须使用=参数]
## to get the right version of lm)[#得到正确版本的LM)]

trace(lm, exit = recover, where = nlme)

## End(Not run)[#结束(不运行)]
}

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


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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-24 11:33 , Processed in 0.023743 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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