I want to prepare a demo (that will play sequentially in clicks) for a presentation tutorial...Can somebody help me how can I write a demo, suppose the following are steps in the demo...
#start
set.seed(1345)
x1 <- sample(letters[1:10], 5)
x1
sort(x1)
x <- sample(1:10, 5)
y <- sample(c(11:20), 5)
require(lattice)
plot(x,y)
z <- rnorm(5, 1, 0.5)
dataframe <- data.frame(x, y, z)
model1 <- lm(y ~x)
aov(model1)
#end
Sorry I 开发者_StackOverflow社区could find a solution after hours and days of search. I appreciate your help.
Another way to do it:
- Save your script in a file (
demo.R
) - Edit the script and sprinkle it with
pause()
in strategic places - In R, define
pause <- function() invisible(readline())
- Run the script with
source("demo.R", echo=TRUE)
It will then print & run your commands and stop and wait for input at the sprinkled pause()
. Just hit <Enter>
to continue.
EDIT: I don't know a good way to hide the pause()
statement. A possible way would be to copy the code for source()
and modify it to skip printing calls to pause()
, but that's a little overkill I think...
...but you could rename the pause function to anything you like - including '....'
, but you still need to call it like this: ....()
Hmmm. Maybe something like this:
'....' <- function(...) invisible(readline())
Then sprinkle your script with either:
....('Press Enter to continue')
# Or
....(Press_Enter_to_continue)
Another possibility if you rename the pause function to Pausing...
:
Pausing...(Press_Enter)
A hacky way of doing what you want is:
- Save commands as a script, eg
testDemo.r
- Copy into and existing package's demo folder, eg
<Library>/base/demo
- Run with
demo(testDemo,package="base")
But it pauses in pages rather than by command. Ultimately though, you may want to create your own package to contain custom demos.
Edit
It seems the code for demo
is mainly for checking that a demo exists, and the core is quite simple:
op <- options(device.ask.default=TRUE)
source("testDemo.r",echo=TRUE,max.deparse.length=Inf,keep.source=TRUE)
options(op)
Note that any pausing is only done by the presence of graphics, not any length of echoed text, as is actually the case with demo
.
精彩评论