R中的动画无需保存图形且无需外部软件

Animation in R without saving figures and without external software

是否可以直接在 R 中创建动画,就像在 Matlab 中一样,而无需实际保存图形和使用外部软件?只是执行脚本?

在 Matlab 中考虑以下示例。

t = (1:360) / 180 * pi;    % 360 angles in radians from 1 to 360 degrees
n = length(t);    % Length of vector
x = cos(t);    % Cosines
y = sin(t);    % Sines
f = figure;    % Create figure and its handle
hh = plot(2, 2, 'or', 'MarkerFaceColor', 'r', 'MarkerSize', 10);    % Create plot and its handle
set(gca, 'XLim', [-1 1]);    % Set x axis limits
set(gca, 'YLim', [-1 1]);    % Set y axis limits
axis square; axis off;    % Set more properties for axes

while ishandle(f)    % Until the user closes the figure
  try    % Try this loop, if encouter an error just break the loop
    for ii = 1:n    % For all angles
      set(hh, 'XData', x(ii))    % Change point x coordinate
      set(hh, 'YData', y(ii))    % Change point y coordinate
      pause(0.01)    % Make a little pause
    end
  end
end

hrbrmstr 的回答后,我试过这个:

t <- (1:360) / 180 * pi
n <- length(t)
x <- cos(t)
y <- sin(t)

while(TRUE) {
  for (i in 1:n) {
    plot(x[i], y[i], ann=FALSE, pch=20, axes=FALSE, xlim=c(-1, 1), ylim=c(-1, 1), col="red", cex=4, asp=1)
    Sys.sleep(0.01)
  }
}

它似乎正在做这项工作。谢谢!

我也试过这个:

t <- (1:360) / 180 * pi
n <- length(t)
x <- cos(t)
y <- sin(t)

while(TRUE) {
  for (i in 1:n) {
    plot.new()
    usr<-par("usr")
    par(usr=c(-1.1, 1.1, -1.1, 1.1))
    lines(x, y, col="green")
    points(x[i], y[i], pch=20, col="red", cex=4, asp=1)
    Sys.sleep(0.01)
  }
}

这更接近我最初的想法。但是我发现 R 的 "paper and pen" 绘图模型非常糟糕。没有解决办法吗?

我不打算弄清楚所有 matlab 绘图在做什么w/r/t 美学。

这个:

while(TRUE) {
  barplot(sample(1:1000, 10))
  Sys.sleep(0.5)
}

"animates" 随机条形图在 IMO 上显示得很好并且:

t <- (1:360) / 180 * pi
n <- length(t)
x <- cos(t)
y <- sin(t)

for (i in 1:n) {
  plot(x[1:i], y[1:i], type="p")
  Sys.sleep(0.15)
}

以一种方式处理 "animation" 点的一个基本位,并且:

for (i in 1:n) {
  plot.new()
  points(x[1:i], y[1:i])
  Sys.sleep(0.15)
}

另一个(虽然那个需要一些工作设置 x 和 y 限制以避免图形设备错误)。

当 GUI 要求 "stop" 或 for 循环完成时,所有三个都停止。

不完全是 "disproportionately complex"。更像是 "not familiar to the matlab person".