-
-
Notifications
You must be signed in to change notification settings - Fork 879
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
Changes from 1 commit
8c9b1ff
270ecda
927fa13
2e3b485
d83e61d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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('') | ||
|
||
# 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="") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is I do not see a particular reason to restrict to png only, e.g. how about |
||
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]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a utils function |
||
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) { | ||
|
There was a problem hiding this comment.
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 alwaysTRUE
here, so do you have to checkif (animate)
at all?