2

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 could find a solution after hours and days of search. I appreciate your help.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
jon
  • 11,186
  • 19
  • 80
  • 132
  • 1
    sounds like you want a bunch of screenshots and a powerpoint presentation. That or if you're using RStudio or similar, you can save your commands in a text file (.R) and then run them one at a time using ctrl+return and the output will be displayed 'one click at a time'... – nzcoops Sep 16 '11 at 02:34
  • You could check the source of `demo`, it uses `readline`. – Roman Luštrik Sep 16 '11 at 09:20
  • 1
    I tend to use Sweave (R + LaTeX) for tutorials on R. It has the benefit that it's easy to extract the R code from the Sweave file (.Rnw). Hence you don't have to worry anymore that your R script stays in sync with your presentation. – Thierry Sep 16 '11 at 12:52
  • @Thierry And using Beamer to make it into a presentation – James Sep 16 '11 at 14:51
  • My question was not related to embedding R codes in powerpoint / word, rather I need to execute some of the commands in sequential way in R, so that I do not need to type them. This is similar to what you get when you use the following demo: require(lattice); demo(lattice) So I want to put my commands in the same way into a flow Thanks for the suggestions – John Clark Sep 16 '11 at 14:55
  • @John - Did your demo work out the way you wanted? Remember, if any of the answers where was what you needed, you should mark it as an answer. You should also upvote answers (and questions) you like. Just click on the score in the upper left... – Tommy Sep 20 '11 at 00:13

2 Answers2

2

Another way to do it:

  1. Save your script in a file (demo.R)
  2. Edit the script and sprinkle it with pause() in strategic places
  3. In R, define pause <- function() invisible(readline())
  4. 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)
Tommy
  • 39,997
  • 12
  • 90
  • 85
  • thanks Tommy..for improved solution ...when I am trying this, pause () appear where I want to put a break to...is there anyway to supress them ... – John Clark Sep 16 '11 at 15:37
  • is it possible to display something like "......" instead of "pause ()" ....just look more good to eye as the participants will not think that I am not trying execute any function at the pause moment ...thanks – John Clark Sep 16 '11 at 15:46
  • seems like swirl Maybe you can write a lesson in swirl http://swirlstats.com/instructors.html – Ferroao Jun 19 '17 at 23:58
1

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.

James
  • 65,548
  • 14
  • 155
  • 193
  • thanks James ...it works...like demo...that is what I expected ..ultimate solution would be that do by command ... – John Clark Sep 16 '11 at 15:25
  • @John Looking at `demo` it appears the behaviour is mainly set by appropriate defaults to `source` and a use of the parameter `device.ask.default` in `options`. Will add as an edit – James Sep 16 '11 at 15:56