首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >带针织的rgl动画图形

带针织的rgl动画图形
EN

Stack Overflow用户
提问于 2014-08-08 19:50:27
回答 1查看 1.6K关注 0票数 58

我想通过knitr在我的.Rnw文档中包含动画rgl图形。这是我的MWE:

代码语言:javascript
复制
\documentclass{article}

<< label = setup, include = FALSE>>=
opts_chunk$set(fig.path = 'figure/',  cache = FALSE, dev = "pdf",  fig.align = 'center', fig.show = 'hold', fig.width = 3, fig.height = 3,  echo = TRUE, warning = FALSE, message = FALSE, size = 'footnotesize', comment=NA, results='hold')

knit_hooks$set(par = function(before, options, envir){
if (before && options$fig.show!='none')
 par(mar = c(4, 4, 0.1, 0.1), cex.lab = 0.95, cex.axis = 0.9, mgp = c(2, 0.7, 0), tcl = -0.3)
}
)
knit_hooks$set(rgl = function(before, options, envir) {
  if (!before) {
    ## after a chunk has been evaluated
    if (rgl.cur() == 0) return()  # no active device
    name = paste(options$fig.path, options$label, sep = '')
    rgl.snapshot(paste(name, '.png', sep = ''), fmt = 'png')
    return(paste('\\includegraphics{', name, '}\n', sep = ''))
  }
}
)

options(replace.assign = TRUE, width = 60)
@ 
\begin{document}

<< label=packages >>=
library(car)
@
<< label=rgl1, rgl=TRUE, fig.show='animate' >>=
scatter3d(prestige ~ income + education, data=Duncan)
@

\end{document}

我在编织的文档中得不到图表。

已更新

我仍然不能让它工作,并得到以下警告:

代码语言:javascript
复制
Warning messages:
1: In rgl.snapshot(paste(name, ".png", sep = ""), fmt = "png") :
  RGL: Pixmap save: unable to open file 'D:\A B\C D UAF\Test\knitr\rglAnimation\figure\rgl1.png' for writing
2: In rgl.snapshot(paste(name, ".png", sep = ""), fmt = "png") :
  snapshot failed
3: running command '"C:\PROGRA~2\MIKTEX~1.9\miktex\bin\x64\texi2dvi.exe" --quiet --pdf "rglAnimation.tex" --max-iterations=20 -I "C:/PROGRA~1/R/R-31~1.1/share/texmf/tex/latex" -I "C:/PROGRA~1/R/R-31~1.1/share/texmf/bibtex/bst"' had status 1 

我的sessionInfo()

代码语言:javascript
复制
R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] tools     stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
[1] mgcv_1.8-2    nlme_3.1-117  rgl_0.93.1098 car_2.0-21    knitr_1.6.15 

loaded via a namespace (and not attached):
[1] evaluate_0.5.5  formatR_1.0     grid_3.1.1      highr_0.3      
[5] lattice_0.20-29 MASS_7.3-34     Matrix_1.1-4    nnet_7.3-8     
[9] stringr_0.6.2  

编辑的

opts_chunk$set中将fig.path = 'figure/'更改为fig.path = ''将编译具有png图形但没有任何动画的文档。如何使用fig.path = 'figure/'获取动画rgl图形。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-30 06:06:27

我有一个基于您的示例,可以在RStudio 0.99.441中使用Windows8.1中的knitr工作。

这将生成40帧的绘图动画。它使用内置的hook_plot_custom来包含通过动画3d绘图手动生成的绘图。动画的代码基于play3dmovie3d的帮助和源代码中的代码。movied3d本身不能使用,因为它的文件命名太不灵活。

我已经把这个放到了https://github.com/NikNakk/testAnimateRgl/的github上。pdf网址为https://github.com/NikNakk/testAnimateRgl/raw/master/rglKnitr.pdf

代码语言:javascript
复制
\documentclass{article}
\usepackage{animate}

<< label = setup, include = FALSE>>=
library("rgl")
library("car")
library("knitr")
knit_hooks$set(rgl = hook_plot_custom)
@ 
\begin{document}

<< label=rgl1, rgl=TRUE, fig.show='animate', fig.width=5, fig.height=5, out.width='.6\\linewidth', dev='png', fig.num = 40, interval=0.1>>=
scatter3d(prestige ~ income + education, data=Duncan)
M <- par3d("userMatrix")
par3d(windowRect = 100 + opts_current$get("dpi") *
        c(0, 0, opts_current$get("fig.width"), 
        opts_current$get("fig.height")))
spinFunc <- par3dinterp(userMatrix=list(M,
                             rotate3d(M, pi/2, 1, 0, 0),
                             rotate3d(M, pi/2, 0, 1, 0)))
for(i in 1:40) {
  par3d(spinFunc(i / 10))
  Sys.sleep(0.05)
  rgl.snapshot(fig_path(".png", number = i), fmt = "png")
}
@

\end{document}

编辑:新版本

下面是另一个版本,它演示了如何使用自定义块选项为相当简单的spin3d设置参数。请注意,在此版本中,块只有一行( scatter3d图)。spin3d.axis用于将轴参数设置为spin3dspin3d.rpm用于设置rpm参数。使用标准的fig.numinterval参数设置图像的数量和图像之间的间隔。

代码语言:javascript
复制
\documentclass{article}
\usepackage{animate}

<< label = setup, include = FALSE>>=
  library("rgl")
library("car")
library("knitr")
hook_rgl_spin <- function(before, options, envir) {
  if (!before) {
    par3d(windowRect = 100 + options$dpi *
          c(0, 0, options$fig.width, 
            options$fig.height))
    if (!is.null(options$spin3d.axis)) {
      spin3d.axis <- options$spin3d.axis
    } else {
      spin3d.axis <- c(0, 0, 1)
    }
    if (!is.null(options$spin3d.rpm)) {
      spin3d.rpm <- options$spin3d.rpm
    } else {
      spin3d.rpm <- c(0, 0, 1)
    }
    spinFunc <- spin3d(axis = spin3d.axis, rpm = spin3d.rpm)
    for(i in 1:options$fig.num) {
      par3d(spinFunc(i * options$interval))
      Sys.sleep(0.05)
      rgl.snapshot(fig_path(".png", number = i), fmt = "png")
    }

    hook_plot_custom(before, options, envir)
  }
}
knit_hooks$set(rgl = hook_rgl_spin)
@ 
  \begin{document}

<< label=rgl1, rgl=TRUE, fig.show='animate', fig.width=5, fig.height=5, out.width='.6\\linewidth', dev='png', fig.num = 40, interval=0.1, spin3d.axis=c(0, 0, 1), spin3d.rpm = 20>>=
  scatter3d(prestige ~ income + education, data=Duncan)
@

  \end{document}
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25202887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档