Pergunta

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. 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))}
Foi útil?

Solução

library, require, example and maybe a few other functions could used with and without quotes:

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.

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).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top