简体   繁体   English

R:包功能的运行示例:奇怪的行为

[英]R: Running example of package function: strange behavior

I don't really understand why the behavior of example is different if I enter a string name manually from when I use the ls function. 我真的不明白,如果从使用ls函数时手动输入字符串名称,为什么example的行为会有所不同。 Consider the function below: 考虑以下功能:

> ls("package:LIM")[1]
[1] "FILERigaAutumn"

If I run the example like that: 如果我像这样运行示例:

> example(ls("package:LIM")[1])
Warning message:
In example(ls("package:LIM")[1]) : no help found for ‘ls("package:LIM")[1]’

And it seems to not execute the example. 而且似乎没有执行该示例。 But when I run it directly: 但是当我直接运行它时:

> example("FILERigaAutumn")

I get a lot of output and the example is executed. 我得到了很多输出,并执行了示例。

The type of the argument passed seems to be the same: 传递的参数类型似乎是相同的:

> typeof("FILERigaAutumn")
[1] "character"
> typeof( ls("package:LIM")[1])
[1] "character"

Does anyone have an idea why? 有谁知道为什么? I want to compute the running time of all the examples in one package: 我想计算一个包中所有示例的运行时间:

for (func in ls("package:LIM")){system.time(example(func))}

library , require , example and maybe a few other functions could used with and without quotes: libraryrequireexample以及可能使用的其他一些函数,带或不带引号:

example(runif)

example("runif")

To allow the unquoted version these functions convert the first argument into a character (without evaluating it) by calling: 要允许使用不带引号的版本,这些函数可通过调用以下命令将第一个参数转换为字符(不对其求值):

deparse(subsitute(x))

resulting in: 导致:

deparse(substitute(ls("package:LIM")[1]))
# [1] "ls(\"package:LIM\")[1]"

To circumvent this (to evaluate the argument) you have to use the character.only argument. 为了避免这种情况(评估参数),您必须使用character.only参数。

example(ls("package:LIM")[1], character.only=TRUE)

IMHO this behaviour isn't very consistent ( character.only=TRUE should be the default) and I can't see any advantages (ok, you can use tab-completion in the unquoted version). 恕我直言,这种行为不是很一致( character.only=TRUE应该是默认值),我看不到任何优势(好的,您可以在未引用的版本中使用制表符补全)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM