Visualize nested for loops. Inspiration from Programming Loops vs Recursion a youtube video by Computerphile.
You can install loopviz
from GitHub with:
# install.packages("devtools")
devtools::install_github("tylurp/loopviz")
To plot a nested for loop, provide some numbers to loopviz
. Each
number represents the number of iterations. The order represents the
hierarchy, i.e. the first number is the top level loop, the second is
the first nested loop and so on. For example:
library(loopviz)
loopviz(2, 3)
Above we have a for loop which iterates 2 times. Inside that loop there is a nested loop which iterates 3 times. The code would look something like:
for (i in 1:2) {
for (j in 1:3)
print (j)
}
#> [1] 1
#> [1] 2
#> [1] 3
#> [1] 1
#> [1] 2
#> [1] 3
You can let loopviz do the translating with translate = TRUE
:
loopviz(2, 3, translate = TRUE)
#> for (i in 1:2) {
#> for (j in 1:3)
#> print (j)
#> }
Animating loops is also possible thanks to gifski:
loopviz(2, 3, animate = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.