ramnathv / rblocks

A fun and visual way to learn data structures and control flow in R.
26 stars 10 forks source link

Animated Blocks #6

Open ramnathv opened 10 years ago

ramnathv commented 10 years ago

Here is a cool application using the animation package by @yihui to visualize how loops work, along with rblocks.

library(rblocks)
block = make_block(5, 5, type = 'matrix')

library(animation)
saveGIF({
  for (i in 1:5){
    block[i, i] = 'red'
    display(block)
  }
})

animation

ramnathv commented 10 years ago

Here is another illustration of something that could be fun

library(rblocks)
block = make_block(5, 5, type = 'matrix')
animation::saveGIF({
  i = 1
  while (i <= 5){
    block[i,1:i] = 'red'
    block[1:i,i] = 'red'
    display(block)
    block = make_block(5, 5, type = 'matrix')
    i = i + 1
  }
})

animation

ramnathv commented 10 years ago

Here is a slightly modified version of the code, which will display the gif directly in RStudio

for (i in 1:5){
  dev.hold()
  block[i, i] = 'red'
  display(block)
  ani.pause()
}
josep2 commented 10 years ago

How do you embed the animation?

ramnathv commented 10 years ago

It is a gif file. So treat it like an image.

yihui commented 10 years ago

Thanks! This is very cool.

ramnathv commented 10 years ago

@yihui Thanks to your animation package, this was really easy!

woobe commented 10 years ago

Conway's Game of Life (Codes - rBlocks_test_03.R) (Original R Codes for the G.O.L.)

Conway's Game of Life Animated using #rstats #rBlocks #a... on Twitpic

Thanks @ramnathv and @yihui !!