Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

animation support in html and markdown #166

Merged
merged 5 commits into from
Mar 13, 2012
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions R/hooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,27 @@ hook_plot_tex = function(x, options) {
#' @rdname hook_plot
#' @export
hook_plot_html = function(x, options) {
## TODO: output size not implemented for HTML yet
a = options$fig.align
sprintf('<img src="%s" class="plot" %s/>\n', .upload.url(x),
switch(a, default = '', left = 'style="float: left"',
right = 'style="float: right"',
center = 'style="margin: auto; display: block"'))
if(options$fig.show == 'animate') {
.ani.plot.hook.html(x, options)
} else {
## TODO: output size not implemented for HTML yet
a = options$fig.align
sprintf('<img src="%s" class="plot" %s/>\n', .upload.url(x),
switch(a, default = '', left = 'style="float: left"',
right = 'style="float: right"',
center = 'style="margin: auto; display: block"'))
}
}
#' @rdname hook_plot
#' @export
hook_plot_md = function(x, options) {
base = opts_knit$get('base.url')
if (is.null(base)) base = ''
sprintf('![plot of chunk %s](%s%s) ', options$label, base, .upload.url(x))
if(options$fig.show == 'animate') {
.ani.plot.hook.html(x, options)
} else {
base = opts_knit$get('base.url')
if (is.null(base)) base = ''
sprintf('![plot of chunk %s](%s%s) ', options$label, base, .upload.url(x))
}
}

## a wrapper to imgur_upload to get the URL of images when option upload==TRUE
Expand Down Expand Up @@ -154,6 +162,37 @@ hook_plot_md = function(x, options) {
sprintf('<iframe src="%s" class="knitr" width="100%%"></iframe>', name)
} else x
}
.ani.plot.hook.html = function(x, options) {
# pull out all the relevant plot options
animate <- options$fig.show == 'animate'
fig.num <- options$fig.num
fig.cur <- options$fig.cur
if(is.null(fig.cur)) fig.cur <- 0

# Don't print out intermediate plots if we're animating
if(animate && fig.cur < fig.num) return('')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

due to the way you call .ani.plot.hook.html(), animate is always TRUE here, so do you have to check if (animate) at all?


# TODO: only supports png device for now
# set up the ffmpeg run
ffmpeg.opts <- options$aniopts
fig.fname <- paste(sub(paste(fig.num, '$',sep=''), '', x[1]), "%d.png", sep="")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is sub(str_c(fig.num, '$'), '%d', x[1]), right?

I do not see a particular reason to restrict to png only, e.g. how about fig.fname = str_c(sub(str_c(fig.num, '$'), '%d', x[1]), x[2])

mov.fname <- paste(sub(paste(fig.num, '$',sep=''), '', x[1]), ".mp4", sep="")
if(is.na(ffmpeg.opts)) ffmpeg.opts <- NULL

ffmpeg.cmd <- paste("ffmpeg", "-y", "-r", 1/options$interval,
"-i", fig.fname, mov.fname)
system(ffmpeg.cmd, ignore.stdout=TRUE)

# figure out the options for the movie itself
mov.opts <- strsplit(options$aniopts, ';')[[1]]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a utils function sc_split() which you can use here: sc_split(options$aniopts)

opt.str <- paste(
" ",
if(!is.null(options$out.width)) sprintf('width=%s', options$out.width),
if(!is.null(options$out.height)) sprintf('height=%s', options$out.height),
if('controls' %in% mov.opts) 'controls="controls"',
if('loop' %in% mov.opts) 'loop="loop"')
sprintf('<video %s><source src="%s" type="video/mp4" />video of chunk %s</video>', opt.str, mov.fname, options$label)
}

## format a single inline object
.inline.hook = function(x) {
Expand Down