====== Miscellaneous functions in package animation ====== Some of these functions are occasionally used in my daily life, and some are the implementations of my quick ideas when I see something interesting (like the visual illusions). ===== "Tidy up" R source ===== The function ''tidy.source()'' is defined as: > tidy.source function(source = "clipboard", keep.comment = TRUE, keep.blank.line = FALSE, begin.comment, end.comment, ...) { tidy.block = function(block.text) { exprs = parse(text = block.text) n = length(exprs) res = character(n) for (i in 1:n) { dep = paste(deparse(exprs[i]), collapse = "\n") res[i] = substring(dep, 12, nchar(dep) - 1) } return(res) } text.lines = readLines(source, warn = FALSE) if (keep.comment) { identifier = function() paste(sample(LETTERS), collapse = "") if (missing(begin.comment)) begin.comment = identifier() if (missing(end.comment)) end.comment = identifier() text.lines = gsub("^[[:space:]]+|[[:space:]]+$", "", text.lines) while (length(grep(sprintf("%s|%s", begin.comment, end.comment), text.lines))) { begin.comment = identifier() end.comment = identifier() } head.comment = substring(text.lines, 1, 1) == "#" if (any(head.comment)) { text.lines[head.comment] = gsub("\"", "'", text.lines[head.comment]) text.lines[head.comment] = sprintf("%s=\"%s%s\"", begin.comment, text.lines[head.comment], end.comment) } blank.line = text.lines == "" if (any(blank.line) & keep.blank.line) text.lines[blank.line] = sprintf("%s=\"%s\"", begin.comment, end.comment) text.tidy = tidy.block(text.lines) text.tidy = gsub(sprintf("%s = \"|%s\"", begin.comment, end.comment), "", text.tidy) } else { text.tidy = tidy.block(text.lines) } cat(paste(text.tidy, collapse = "\n"), "\n", ...) invisible(text.tidy) } As you have seen, it's quite simple. When I write R code, I'd like to "tidy" them up, because I don't want to type the spaces (either those around operators or those for indents). You just have to copy your code to the clipboard, then ''tidy.source()'', and the (more standard) code will be printed in the console. For example, this is the original code: f = tempfile() writeLines(' # rotation of the word "Animation" # in a loop; change the angle and color # step by step for (i in 1:360) { # redraw the plot again and again plot(1,ann=FALSE,type="n",axes=FALSE) # rotate; use rainbow() colors text(1,1,"Animation",srt=i,col=rainbow(360)[i],cex=7*i/360) # pause for a while Sys.sleep(0.01)} ', f) And this is the "tidy" version: > tidy.source(f) # rotation of the word 'Animation' # in a loop; change the angle and color # step by step for (i in 1:360) { # redraw the plot again and again plot(1, ann = FALSE, type = "n", axes = FALSE) # rotate; use rainbow() colors text(1, 1, "Animation", srt = i, col = rainbow(360)[i], cex = 7 * i/360) # pause for a while Sys.sleep(0.01) } ===== Visual Illusions ===== Perhaps visual illusions can inspire ideas in animations. ==== Scintillating grid illusion ==== White dots? Black dots? === Illusion === {{ :animation:scintillating_grid_illusion.png |Scintillating grid illusion}} === R code === vi.grid.illusion() ==== Hermann grid illusion ==== Gray dots at the intersections? === Illusion === {{ :animation:hermann_grid_illusion.png |Hermann grid illusion}} === R code === vi.grid.illusion(type = "h", lwd = 22, nrow = 5, ncol = 5, col = "white") ==== Lilac Chaser ==== Green dots moving? === Animation === Stare at the center cross for a few (say 30) seconds to experience the phenomena of the illusion. === R code === oopt = ani.options(ani.height = 480, ani.width = 480, outdir = getwd(), nmax = 1, interval = 0.05, title = "Visual Illusions: Lilac Chaser", description = "Stare at the center cross for a few (say 30) seconds to experience the phenomena of the illusion.") ani.start() par(pty = "s", mar = rep(1, 4)) vi.lilac.chaser() ani.stop() ani.options(oopt) ===== Create RSS feed from a CSV file ===== An RSS feed is essentially just an XML file, thus the creation is easy just with ''cat()'' to write some tags into a text file. The elments of an item in an RSS feed usually contains ''title'', ''link'', ''author'', ''description'', ''pubDate'', ''guid'', and ''category'', etc, which are stored in the CSV data file. write.rss(entry = "http://r.yihui.name/news/rss.csv")