在RStudio中进行循环绘制 [英] Plotting during a loop in RStudio

查看:317
本文介绍了在RStudio中进行循环绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为R(模拟退火)中的旅行商问题(TSP)实施解决方案,我想定期输出当前的最佳路径.我已经搜索了很多关于如何在for循环中输出图的方法,但迄今为止失败了.

I am implementing a solution to the Traveling Salesman Problem (TSP) in R (simulated Annealing) and I want to output the current best path periodically. I have searched quite a bit for how to output plots during a for loop and have thus far failed.

我使用RStudio,并希望在生成图形时查看它们.如果您曾经看过TSP求解器做他们的事情,您将了解看它有多酷.这是图形输出的示例,我想查看 http://www.staff.science.uu.nl/~beuke106/anneal/anneal.html

I use RStudio, and want to see the graphs as they are generated. If you have ever watched TSP solvers do their thing, you will understand how cool it is to watch. Here is a sample of the graphics output I want to see http://www.staff.science.uu.nl/~beuke106/anneal/anneal.html

我认为内存使用不会有问题(在大约500,000次迭代中,我只期望50-100个图).这是一个示例函数,在该函数运行期间,我们希望可以看到10个不同的图:

I don't think that the memory usage will be a problem (during about 500,000 iterations, I am only expecting 50-100 plots). Here is a sample function, where we would expect to see 10 different plots during the time the function runs:

Plotz <- function(iter = 1000000, interval = 100000) {
  x <- 1:10
  for(i in 1:iter){
    y <- runif(10)
    if(i %% interval == 0) {
      plot(x, y)
    }
  }
  return(c(x, y))
}
Plotz()

运行此命令时,我看到的只是生成的最终图(在RStudio中).如何查看生成的图?

When I run this, all I see is the final plot produced (in RStudio). How can I see the plots as they're generated?

另外:我在Ubuntu上(无论最新的稳定版本是什么).不知道这是否相关.

Also: I am on Ubuntu (whatever the newest stable release is). Don't know if that is relevant.

谢谢大家.

根据墨菲船长的建议,我尝试在Linux终端中运行此命令,并且图形出现.我仍然认为如何在RStudio中做到这一点"这个问题.仍然是相关的.这是一个很好的程序,所以也许有人对如何使它起作用有一个想法?

Per Captain Murphy's suggestion, I tried running this in the Linux terminal, and the graphics appeared. I still think the question of "How to do this in RStudio?" Is still relevant, however. It's such a good program, so maybe someone has an idea of what could be done to get this to work?

正如Thilo所说,这是Rstudio中的一个已知错误.如果没有解决软件本身的问题而有其他任何想法可以解决此问题,那么仍然有很多事情需要讨论.否则,请考虑解决此问题.

As Thilo stated, this is a known bug in Rstudio. If anyone has any other ideas to solve this without the software itself being fixed, then there is still something to discuss. Otherwise, consider this question solved.

推荐答案

您可以做的一件事是打开x11窗口并在其中绘图:

One thing you can do is open a x11 window and plot in there:

x11()
Plotz()

应该与在终端中运行相同.

That should work the same as running it in terminal.

这篇关于在RStudio中进行循环绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆