Christian Vincenot
Christian Vincenot

Reputation: 157

RStudio : Refresh viewer output at runtime when plotting graphs

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.

Upvotes: 2

Views: 1162

Answers (1)

user2554330
user2554330

Reputation: 44957

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.

Upvotes: 2

Related Questions