nx10 / httpgd

Asynchronous http server graphics device for R.
https://nx10.github.io/httpgd
GNU General Public License v2.0
377 stars 19 forks source link

Plots are not displayed in realtime in a loop #83

Closed jinlow closed 2 years ago

jinlow commented 2 years ago

Describe the bug

I have a package that has some interactivity, that depends on being able to display plots in a loop. In RStudio, all plots display, but in an httpgd session they are not, only the first plot, and then all of them after the interactivity ends.

To Reproduce

The following R code run in a vanilla R session, only the first plot is displayed, subsequent plots are not displayed until "q" is pressed.

httpgd::hgd()
httpgd::hgd_browse()

data(mtcars)
while (TRUE) {
  cat("Enter 'p' to print a plot, and 'q' to quit.")
  command <- readLines(n=1)
  if (command == "p") {
    var = sample(colnames(mtcars), 1)
    barplot(table(mtcars[[var]]), main = var)
  }
  if (command == "q") {
    break
  }
}

Expected behavior

I would expect all of the plots to display interactively, every time "p" is entered a new plot would be displayed.

Screenshots

Here I have pressed "p" several times and only one plot is printed. image

Only after pressing "q" and the loop ending, are all the plots displayed. image

Environment

nx10 commented 2 years ago

Thank you for this bug report and for the reproducible example code. I think this is caused by httpgd waiting for the R session to finish executing, but it is not intended behavior. I will see what I can do.

renkun-ken commented 2 years ago

Original issue: https://github.com/REditorSupport/vscode-R/issues/813.

nx10 commented 2 years ago

This is caused by later preventing calling R when there is something on the call stack https://github.com/r-lib/later/issues/159

We have to wait if the later devs consider adding functionality to disable this check. Otherwise we would have to implement a complete later replacement for httpgd at some point. As this is platform dependent low level C code I want to avoid this when possible.