
터미널 animation
에 표시되는 난수를 하나씩 추가하는 난수를 생성하는 패키지 의 다음 R 코드가 있습니다 .R
library("animation")
brownian.motion():
function(n=10,xlim=c(-20,20),ylim=c(-20,20))
{
x=rnorm(n)
y=rnorm(n)
for (i in seq_len(ani.options("nmax"))) {
dev.hold()
plot(x,y,xlim = xlim,ylim = ylim)
text(x,y)
x=x+rnorm(n)
y=y+rnorm(n)
}
}
R
나는 이 R 코드의 결과가 터미널 에 표시되는 것과 마찬가지로 비머 프레임에 있기를 원합니다 .
내 MWE로 아래 코드를 시도했지만 작동하지 않았습니다.
\documentclass{beamer}
\begin{document}
\begin{frame}
%beginning of R code
library("animation")
brownian.motion():
function(n=10,xlim=c(-20,20),ylim=c(-20,20))
{
x=rnorm(n)
y=rnorm(n)
for (i in seq_len(ani.options("nmax"))) {
dev.hold()
plot(x,y,xlim = xlim,ylim = ylim)
text(x,y)
x=x+rnorm(n)
y=y+rnorm(n)
}
}
% end of R code
\end{frame}
\end{document}
저를 도와주세요
답변1
나는 이전에 R을 사용해 본 적이 없습니다. 실험하고, 매뉴얼을 훑어보고, 인터넷에 문의한 후 내린 결론은 다음과 같습니다.
R 패키지를 사용하지 마십시오
animation
. 별다른 부가가치 없이는 복잡합니다.
pdf(...)
대신 벡터 그래픽과 다중 페이지 출력을 생성하는 와 같은 플롯에 적합한 출력 장치를 설정하십시오 . R 코드를 실행한 후 Rscript
PDF 출력을 \animategraphics
.
애니메이션을 실행하려면 클릭하세요(최고의 성능을 위해서는 깜박임 기반 브라우저[Chrome/Chromium, Opera]):
게다가 질문의 원래 R 코드는 실행되지 않습니다. 다음은 입자가 한계를 넘어서 이동하는 것을 방지하는 간단한 모델이 추가된 작업 예입니다. 애니메이션 프레임이 파일에 기록됩니다.frames.pdf
터미널에서 실행:
Rscript brnMotion.R
파일 brnMotion.R
:
brownianMotion <-
function(n=10,xlim=c(-20,20),ylim=c(-20,20), steps=50)
{
x=rnorm(n) # random starting position
y=rnorm(n)
for (i in 1:steps) {
plot(x,y,xlim = xlim,ylim = ylim)
text(x,y)
# iterate over particles
for(k in 1:n){
walk=rnorm(2); # random move of particle
x[k]=x[k]+walk[1] # new position
y[k]=y[k]+walk[2]
# simple model (at most two rebounds) that prevents a particle
# from moving past the limits
if(x[k]<xlim[1]) x[k]=2*xlim[1]-x[k]
if(x[k]>xlim[2]) x[k]=2*xlim[2]-x[k]
if(y[k]<ylim[1]) y[k]=2*ylim[1]-y[k]
if(y[k]>ylim[2]) y[k]=2*ylim[2]-y[k]
}
}
}
pdf("frames.pdf") # output device and file name
par(xaxs="i", yaxs="i", pty="s") # square plot region
par(mai=c(0.9,0.9,0.2,0.2)) # plot margins
brownianMotion(n=20, steps=400) # 20 particles, 400 time steps
LaTeX 입력( pdflatex
, lualatex
또는 xelatex
):
\documentclass[aspectratio=169]{beamer}
\usepackage{animate}
\usepackage{graphicx}
\begin{document}
\begin{frame}{Brownian Motion}
\begin{center}
\animategraphics[width=0.5\linewidth,controls]{25}{frames}{}{}
\end{center}
\end{frame}
\end{document}
답변2
이것은 파일이 한 단계로 생성 R
되도록 코드를 편성하여 @Alex의 탁월한 답변을 확장합니다 .pdf
R
계속 진행하려면 파일 에 포함된 코드 덩어리로 파일을 만드세요 tex
. R
코드 청크는 <<>>=
및 로 구분됩니다 @
. .Rnw
(R no web) 확장자 로 파일을 저장합니다 Daniel.Rnw
.
.png
gif
이 구문은 를 통해 png("frames%d.png")
호출된 일련의 파일을 저장합니다 . 애니메이션 슬라이드는 출력을 생성하기 위해 파일을 계층화 하는 @Alex 패키지를 사용하여 일반적인 방식으로 만들어졌습니다 . 그래프를 슬라이드에 표시하기 위해 코드 가 래핑되어 있습니다 .frame1.png
frame100.png
\animategraphics
animate
frame1.png
frame100.png
pdf
R
\begin{frame} \end{frame}
beamer
지원하는 편집기나 다른 편집기를 .Rnw
사용하여 파일을 pdflatex
컴파일 합니다 (예: WinEdt에는 이 목적을 위한 추가 기능이 있습니다). 코드를 실행하여 코드 실행 결과를 코드 와 통합하는 파일을 생성합니다 .RStudio
knitr
RStudio
R
.tex
R
LaTeX
\documentclass{beamer}
\usepackage{animate}
\begin{document}
\begin{frame}
%beginning of R code
<<code,echo=FALSE,warning=FALSE,results='hide'>>=
brownianMotion <-
function(n=10,xlim=c(-20,20),ylim=c(-20,20), steps=50)
{
x=rnorm(n)
y=rnorm(n)
for (i in 1:steps) {
plot(x,y,xlim = xlim,ylim = ylim)
text(x,y)
# iterate over particles
for(k in 1:n){
walk=rnorm(2); # random move of particle
x[k]=x[k]+walk[1] # new position
y[k]=y[k]+walk[2]
# simple model for preventing a particle from moving past the limits
if(x[k]<xlim[1]) x[k]=xlim[1]
if(x[k]>xlim[2]) x[k]=xlim[2]
if(y[k]<ylim[1]) y[k]=ylim[1]
if(y[k]>ylim[2]) y[k]=ylim[2]
}
}
}
png("frames%d.png") # output device and file name
par(xaxs="i", yaxs="i", pty="s") # square plot region
par(mai=c(0.9,0.9,0.2,0.2)) # plot margins
brownianMotion(n=20, steps=100) # 20 particles, 100 time steps
dev.off()
@
% end of R code
\begin{center}
\animategraphics[autoplay,controls,palindrome,height=0.7\textheight]{5}{frames}{1}{100}%
\end{center}
\end{frame}
\end{document}
답변3
나는 'R'을 모르지만 온라인에서 시도했을 때 귀하의 코드(r에 대한)에서 일부 오류가 발생했습니다.이것사이트에서는 샘플 r 코드를 제공합니다. png 파일을 출력으로 얻기 위해 컴파일했습니다. 이름을 지정 r.png
하고 다음 LaTeX 코드를 사용하여 프레젠테이션에 해당 이미지를 가져왔습니다. LaTeX에 이미지를 추가하는 방법에 대해 자세히 알아보려면 graphicx
설명서를 읽어보세요. 아래 예를 참조하세요.
\documentclass{beamer}
\usepackage{graphicx}
\begin{document}
\begin{frame}{A graph}
\centering
\includegraphics[height=6cm,width=\textwidth]{r} % In the {} use the appropriate file name. (case-sensitive)
\end{frame}
\end{document}