Update plots during for loop from within Rstudio? [duplicate] - r

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Plotting during a loop in RStudio
I'm trying to monitor the status of a convergence loop, and I can't seem to get it to update the graph each time it iterates.
Here's some sample code:
print(plot(c(0,1)~c(0,100)))
for(i in seq(100)) {
Sys.sleep(.1)
print(points( runif(1)~i ))
}
Note that the graph only updates after everything's been plotted. I need it to update each loop iteration. I thought print would do that, but it's not working.
Update
This is an RStudio-specific problem, as it works properly in base R. Is there a way to force graphing in RStudio each loop iteration?

Start up a separate graphics device with X11() (or win() on windows?) and use that.
Although plots seem to update okay on my RStudio setup. My test is simply:
plot(1:10);for(i in 1:10){points(10-i,i);Sys.sleep(1)}
I see the first set of 10 points, then the next set appear at one second intervals, in the RStudio embedded graphics window.

Related

How to print an entire data frame in R on a Jupyter Notebook? [duplicate]

This question already has an answer here:
How to see all rows of a data frame in a Jupyter notebook with an R kernel?
(1 answer)
Closed 2 years ago.
No matter what I do (e.g. just calling the dataframe df or print(df) or df[1:nrow(df),] I get vertical dots in the printout skipping rows:
There are similar questions on the site about RStudio, and suggestion View(). However, this function is not supported in Jupyter Notebook (specifically, I am using Google Colab).
I am interested in a call or function within R (or R package) to do this independent of the IDE or platform I may be using.
I think the options you want are repr.matrix.max.rows and repr.matrix.max.cols.
Try to run this:
options(repr.matrix.max.rows=600, repr.matrix.max.cols=200)
The defaults are 60 and 20.
Link: https://github.com/IRkernel/IRkernel/issues/470

R viewer off kilter and slow

I'm trying to look at some data in RStudio using View(). Code is running smoothly, but for some reason the display looks kind of off. Column labels are a little off from the rest of the column. See image here or below: https://ibb.co/gTJFCj0
Scrolling is also incredibly slow. It takes close to 5 seconds for it to respond at all.
I'm new to R, so I have absolutely no clue where to even begin looking. I'm guessing it may be that RStudio is trying to display everything at once?

Updating existing plot instead of creating new in a for loop

I am attempting a homework problem where I am tasked to plot the histogram that results from a Galton board experiment, essentially creating normal distribution by adding one value at at a time and updating the histogram after each trial (ball). I would like to find a way to update the histogram after each addition of a new value to the distribution; instead of that my code currently makes a whole ton of plots.
So far I've set up a vector with length=1000 (though theoretically I should be able to apply my final code to a vector of anything length?) and created a loop to add values to it using rbinom() with 200 "pegs" with a probability of 50% (falling left or right).
x<-numeric(1000) #create vector length of 1000 values of 0
for (i in 1:1000) {
x[i]<-sum(rbinom(200,1,0.5))
hist(x,freq=FALSE)
}
I have the hist() call within the for loop (this may be a cardinal sin in R...), which as you can imagine produces 1000 graphs! Definitely not the right way to go about this. Is there any way to just essentially update on top of the previous plot? I'm thinking of things like abline(), lines(), etc, which (as far as I can tell) just add lines on top of an already existing plot in R without creating a new one. This is probably because the data associated with those functions isn't the same as the data in a vector? Anyways, I haven't been able to figure this out wth google. I haven't tried using ggplot2 or the animate packages yet, though I'm only vaguely familiar with the former and I imagine there's a learning curve.
A final note: I'm fairly new to R, so I'd appreciate unrelated advice on the above code, but I also think it's very productive to work things out on your own, so I would prefer hints and/or general advice instead of pasting working code.
Thank you very much in advance for your help!

Error in dev.off() : cannot shut down device 1 (the null device) after opening R studio [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to produce some graph with ggplot in R studio. However, although the code runs smoothly, they are not created. I have tried with dev.off(), but the error message appears. I have also noticed that the error is present also just opening Rstudio. Does anyone has some clue of why I cannot shut down device 1?
Many thanks!
I think we should take a step back. You tried using dev.off() (and got an error) but the initial problem was a failure to create a plot using ggplot. The most common cause of "they are not created" is not understanding the need to print the grid object that ggplot, returns. There is an implicit print at the console but not inside functions. This will create the behavior you describe:
dev.off() # shuts down my interactive graphics device
# could also have used system point and click to close an open window
p11 <- function() {myplot <- ggplot(data.frame(x=1,y=1), aes(x=x,y=y))+geom_point()
dev.off() }
dev.off()
Error in dev.off() : cannot shut down device 1 (the null device)
If you had opened a pdf() or png() device inside that function there would have been a device to print to. See ?Devices

Hide the in-function plot from R package [duplicate]

This question already has an answer here:
How can I suppress the creation of a plot while calling a function in R?
(1 answer)
Closed 6 years ago.
I am using the R package "GeoDE". When I use the function "chdirAnalysis", a figure will be plotted automatically, since there is a command "plot" in the source code of "chdirAnalysis". But I don't want that. How can I stop this?
A similar problem is to hide the in-function printed messages, and I've found the solution which is to use invisible
capture.output(value <- function_name(input))
That can help hide the output from "function_name", but this solution doesn't work on the plot.
Options:
Ask the maintainer to add a plot=FALSE option to the function (and maybe a verbose=FALSE option to stop the text outputs).
Edit the source for chdirAnalysis and remove the function call that does the plotting, or hide it behind a new plot=FALSE options. I think this is chdirplots, which is called but doesn't do anything with its return value. If you are doing this outside the GeoDE package source then you'll need to add the GeoDE::: prefix to any unexported GeoDE functions called by chdirAnalysis (such as chdirSig).
Make it plot to some dummy or throwaway graphics device file, as described in other questions and answers.

Resources