Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plot handler called too late when evaluating a loop #67

Closed
jankatins opened this issue Apr 14, 2016 · 1 comment
Closed

plot handler called too late when evaluating a loop #67

jankatins opened this issue Apr 14, 2016 · 1 comment

Comments

@jankatins
Copy link

jankatins commented Apr 14, 2016

In the IRkernel, we encountered a problem were we couldn't explain the order of the output of a evaluated loop. The situation is basically this:

library(evaluate)

code <- "
for (i in 1:5) {
txt(paste('call:', i),'call')
cars <- c(i, 3, 6, 4, 9)
plot(cars)   
}
"

l <- list()
txt <- function(o, type) {
  t <- paste(o, collapse = '\n')
  l[length(l)+1] <<- t
}
oh <- new_output_handler(source = identity, 
                         text = function(o) txt(o, "text"), 
                         graphics = function(o) txt("plot", "plot"),
                         message = identity, 
                         warning = identity, 
                         error = identity, 
                         value = identity)


x <- evaluate(code, output_handler = oh)
cat(paste(l, collapse="\n"))

-> The resulting list is both filled by the handler and by a call from the evaluated code.

The problem is that it seems that the in some cases, the call happens too early:

> cat(paste(l, collapse="\n"))
call: 1               # No plot has triggered the handler -> call is first
call: 2               # second loop ...
plot                  # but the plot from the first loop is only now being handled
call: 3               # again a too early call
plot                  # plot from the second loop
call: 4
plot
call: 5
plot
plot

When "unrolling" the loop, it looks fine:

code <- "
{i <- 1
txt(paste('call:', i),'call')
cars <- c(i, 3, 6, 4, 9)
plot(cars)   
}
{i <- 2
txt(paste('call:', i),'call')
cars <- c(i, 3, 6, 4, 9)
plot(cars)   
}
{i <- 3
txt(paste('call:', i),'call')
cars <- c(i, 3, 6, 4, 9)
plot(cars)   
}
# [...]
"
> cat(paste(l, collapse="\n"))
call: 1
plot
call: 2
plot
call: 3
plot
# [...]

Any idea why this happens and if there is any way to get this in the right order?

@jankatins jankatins changed the title handler called to late when evaluating a loop handler called too late when evaluating a loop Apr 14, 2016
@jankatins jankatins changed the title handler called too late when evaluating a loop plot handler called too late when evaluating a loop Apr 14, 2016
@hadley
Copy link
Member

hadley commented Jun 14, 2024

I think it's very likely that problem is that we can't tell when a plot is "done" until a new plot starts. I don't see any way around that.

@hadley hadley closed this as completed Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants