I am running the following loop in a function:
for (i in 1:400) {
m<-update_values() # Updates values in the dataframe
dygraph(m[,1:4]) %>%
dyCandlestick() %>%
dyRangeSelector()
Sys.sleep(1)
}
The problem is that RStudio's viewer is locked while the function runs and the plot is not even displayed when the function returns (I can only manually plot the collected data afterwards). I would want the plot to be displayed at each step. Any idea how to achieve this?
Edit: This function monitors sensors in real time, so it needs to plot at runtime.
The dygraph
function uses an HTML widget, so the result needs to be printed to appear in the viewer. Just add %>% print()
at the end and the output should appear, i.e.
for (i in 1:400) {
m<-update_values() # Updates values in the dataframe
dygraph(m[,1:4]) %>%
dyCandlestick() %>%
dyRangeSelector() %>%
print()
Sys.sleep(1)
}
However, a disadvantage of this approach is that you'll end up with 400 pages in the viewer. As far as I know there's no way to say to replace the current view, you can just add new ones. Maybe rstudioapi
has a "Delete viewer page" function, but I don't see it.