Title: | Convert R Graphics to Flash Animations |
---|---|
Description: | Using the 'Ming' library <https://github.com/libming/libming> to create Flash animations. Users can either use the 'SWF' device swf() to generate 'SWF' file directly through plotting functions like plot() and lines(), or convert images of other formats ('SVG', 'PNG', 'JPEG') into 'SWF'. |
Authors: | Yixuan Qiu, Yihui Xie, Cameron Bracken and authors of included software. See file AUTHORS for details. |
Maintainer: | Yixuan Qiu <[email protected]> |
License: | GPL-2 |
Version: | 0.9-9 |
Built: | 2024-10-26 04:10:18 UTC |
Source: | https://github.com/yixuan/r2swf |
Given an R expression that can produce a sequence of images, this function
will record the images with the device provided (e.g.
png()
or jpeg()
) and
convert them to a Flash file.
dev2swf( expr, output = "movie.swf", bgColor = "white", interval = 1, dev = "png", file.ext = "png", img.name = "Rplot", ... )
dev2swf( expr, output = "movie.swf", bgColor = "white", interval = 1, dev = "png", file.ext = "png", img.name = "Rplot", ... )
expr |
an expression to generate a sequence of images |
output |
the name of the output swf file |
bgColor |
background color of the output SWF file |
interval |
the time interval between animation frames |
dev |
the name of the graphics device to use (e.g. |
file.ext |
the file extension for the images |
img.name |
the file name of the images without the extension |
... |
other arguments to be passed to the graphics device |
You can also use devices which are not in the grDevices package by
setting the dev
argument to the name of the function that opens a
device, e.g. CairoPNG()
in the Cairo package. Note
that the file.ext
argument should be set accordingly.
The name of the generated swf file if succeeded.
Yihui Xie <https://yihui.org>
olddir = setwd(tempdir()) output1 = dev2swf({ for(i in 1:10) plot(runif(20), ylim = c(0, 1)) }, dev='png', file.ext='png', output='movie-png.swf') swf2html(output1) if(capabilities("cairo")) { output2 = dev2swf({ for(i in 1:10) plot(runif(20), ylim = c(0, 1)) }, dev='svg', file.ext='svg', output='movie-svg.swf') swf2html(output2) } setwd(olddir)
olddir = setwd(tempdir()) output1 = dev2swf({ for(i in 1:10) plot(runif(20), ylim = c(0, 1)) }, dev='png', file.ext='png', output='movie-png.swf') swf2html(output1) if(capabilities("cairo")) { output2 = dev2swf({ for(i in 1:10) plot(runif(20), ylim = c(0, 1)) }, dev='svg', file.ext='svg', output='movie-svg.swf') swf2html(output2) } setwd(olddir)
This function converts a sequence of PNG/JPEG/SVG image files to SWF. Based
on the image format, it calls image2swf
or
svg2swf
.
file2swf(files, output, bgColor = "white", interval = 1)
file2swf(files, output, bgColor = "white", interval = 1)
files |
a character vector of input filenames |
output |
the name of the output swf file |
bgColor |
background color of the output SWF file |
interval |
the time interval between animation frames |
The name of the SWF file.
Yihui Xie <https://yihui.org>
Given the file names of a sequence of images, this function can convert them into a Flash file (.swf). Supported input formats are jpg/jpeg and png. The two formats are allowed to appear in the same sequence.
image2swf(input, output = "movie.swf", bgColor = "white", interval = 1)
image2swf(input, output = "movie.swf", bgColor = "white", interval = 1)
input |
the file names of the images to be converted |
output |
the name of the output SWF file |
bgColor |
background color of the output SWF file |
interval |
the time interval (in seconds) between animation frames |
This function uses the Ming library (https://github.com/libming/libming) to
implement the conversion. If you want to create a Flash file consisting of
vector graphics, use svg2swf()
instead.
The name of the generated swf file if successful.
Yixuan Qiu <https://statr.me>
if(capabilities("png")) { olddir = setwd(tempdir()) png("Rplot%03d.png") for(i in 1:9) plot(runif(20), ylim = c(0, 1)) dev.off() output = image2swf(sprintf("Rplot%03d.png", 1:9)) swf2html(output) setwd(olddir) }
if(capabilities("png")) { olddir = setwd(tempdir()) png("Rplot%03d.png") for(i in 1:9) plot(runif(20), ylim = c(0, 1)) dev.off() output = image2swf(sprintf("Rplot%03d.png", 1:9)) swf2html(output) setwd(olddir) }
Given the file names of a sequence of SVG files, this function could convert them into a Flash file (.swf).
svg2swf(input, output = "movie.swf", bgColor = "white", interval = 1)
svg2swf(input, output = "movie.swf", bgColor = "white", interval = 1)
input |
the file names of the SVG files to be converted |
output |
the name of the output SWF file |
bgColor |
background color of the output SWF file |
interval |
the time interval (in seconds) between animation frames |
This function uses the XML package in R and a subset of librsvg
(https://wiki.gnome.org/action/show/Projects/LibRsvg)
to parse the SVG file, and
uses the Ming library (https://github.com/libming/libming) to
implement the conversion. Currently this function supports SVG files
created by svg()
in the grDevices
package, and CairoSVG()
in the
Cairo package.
The name of the generated SWF file if successful.
Yixuan Qiu <https://statr.me>
## Not run: if(capabilities("cairo")) { olddir = setwd(tempdir()) svg("Rplot%03d.svg", onefile = FALSE) set.seed(123) x = rnorm(5) y = rnorm(5) for(i in 1:100) { plot(x <- x + 0.1 * rnorm(5), y <- y + 0.1 * rnorm(5), xlim = c(-3, 3), ylim = c(-3, 3), col = "steelblue", pch = 16, cex = 2, xlab = "x", ylab = "y") } dev.off() output = svg2swf(sprintf("Rplot%03d.svg", 1:100), interval = 0.1) swf2html(output) setwd(olddir) } ## End(Not run)
## Not run: if(capabilities("cairo")) { olddir = setwd(tempdir()) svg("Rplot%03d.svg", onefile = FALSE) set.seed(123) x = rnorm(5) y = rnorm(5) for(i in 1:100) { plot(x <- x + 0.1 * rnorm(5), y <- y + 0.1 * rnorm(5), xlim = c(-3, 3), ylim = c(-3, 3), col = "steelblue", pch = 16, cex = 2, xlab = "x", ylab = "y") } dev.off() output = svg2swf(sprintf("Rplot%03d.svg", 1:100), interval = 0.1) swf2html(output) setwd(olddir) } ## End(Not run)
This function opens a SWF device that produces Flash animation
in SWF format. Every time you call a high level plotting function
like plot()
, the movie will create a new
frame and draw following shapes on it.
swf( file = "Rplots.swf", width = 7, height = 7, bg = "white", fg = "black", frameRate = 12 )
swf( file = "Rplots.swf", width = 7, height = 7, bg = "white", fg = "black", frameRate = 12 )
file |
a character string giving the output SWF file |
width |
the width of the device in inches |
height |
the height of the device in inches |
bg |
the background color of the SWF file |
fg |
initial foreground color |
frameRate |
how many frames to be played in 1 second |
Yixuan Qiu <https://statr.me/>
## Not run: ## A demonstration of K-means clustering, using animation package olddir = setwd(tempdir()) if(require(animation)) { swf("kmeans.swf", frameRate = 1) kmeans.ani() dev.off() } ## Test built-in fonts in sysfonts package swf("fonts.swf", 8, 8) plot(1, type = "n") par(family = "sans", cex = 2) text(0.7, 1.3, "Sans-R", font = 1) text(0.7, 1.1, "Sans-B", font = 2) text(0.7, 0.9, "Sans-I", font = 3) text(0.7, 0.7, "Sans-BI", font = 4) par(family = "serif") text(1.0, 1.3, "Serif-R", font = 1) text(1.0, 1.1, "Serif-B", font = 2) text(1.0, 0.9, "Serif-I", font = 3) text(1.0, 0.7, "Serif-BI", font = 4) par(family = "mono") text(1.3, 1.3, "Mono-R", font = 1) text(1.3, 1.1, "Mono-B", font = 2) text(1.3, 0.9, "Mono-I", font = 3) text(1.3, 0.7, "Mono-BI", font = 4) dev.off() setwd(olddir) ## End(Not run)
## Not run: ## A demonstration of K-means clustering, using animation package olddir = setwd(tempdir()) if(require(animation)) { swf("kmeans.swf", frameRate = 1) kmeans.ani() dev.off() } ## Test built-in fonts in sysfonts package swf("fonts.swf", 8, 8) plot(1, type = "n") par(family = "sans", cex = 2) text(0.7, 1.3, "Sans-R", font = 1) text(0.7, 1.1, "Sans-B", font = 2) text(0.7, 0.9, "Sans-I", font = 3) text(0.7, 0.7, "Sans-BI", font = 4) par(family = "serif") text(1.0, 1.3, "Serif-R", font = 1) text(1.0, 1.1, "Serif-B", font = 2) text(1.0, 0.9, "Serif-I", font = 3) text(1.0, 0.7, "Serif-BI", font = 4) par(family = "mono") text(1.3, 1.3, "Mono-R", font = 1) text(1.3, 1.1, "Mono-B", font = 2) text(1.3, 0.9, "Mono-I", font = 3) text(1.3, 0.7, "Mono-BI", font = 4) dev.off() setwd(olddir) ## End(Not run)
This function will generate an HTML file to display the Flash animation.
swf2html(swf.file, output, width = 480, height = 480, fragment = FALSE)
swf2html(swf.file, output, width = 480, height = 480, fragment = FALSE)
swf.file |
the path of the SWF file |
output |
the output path of the HTML file; by default ‘foo.swf’
produces |
width |
width of the Flash |
height |
height of the Flash |
fragment |
whether to produce an HTML fragment only |
The HTML code as a character string.
Yihui Xie <https://yihui.org>
olddir = setwd(tempdir()) output = dev2swf({ for (i in 1:10) plot(runif(20), ylim = c(0, 1)) }, output = 'test.swf') swf2html(output) setwd(olddir)
olddir = setwd(tempdir()) output = dev2swf({ for (i in 1:10) plot(runif(20), ylim = c(0, 1)) }, output = 'test.swf') swf2html(output) setwd(olddir)