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

Should document that observers wait for promise if returned #46

Open
wch opened this issue Apr 30, 2019 · 1 comment
Open

Should document that observers wait for promise if returned #46

wch opened this issue Apr 30, 2019 · 1 comment

Comments

@wch
Copy link
Collaborator

wch commented Apr 30, 2019

Here's an app that demonstrates the issue. If the observer's expression returns a promise, then Shiny blocks until that promise is resolved. If, on the other hand, it returns something else, then that promise will still run, but Shiny won't block waiting for it. This is something that both I and @aronatkins have run into, and it would be helpful if it were better documented in https://rstudio.github.io/promises/articles/shiny.html.

library(shiny)
library(future)
library(promises)

plan("multisession")

server <- function(input, output) {
  messages <- reactiveVal()

  observeEvent(input$go, {
    messages('starting sleep in other session...')
    p <- future({
      Sys.sleep(3)
    })
    p <- then(p, function(value) {
      messages(c(messages(), 'completed sleep'))
    })
    
    if (input$return_value == "NULL") {
      ret <- NULL
    } else if (input$return_value == "promise") {
      ret <- p
    }

    ret
  })
  
  output$messages <- renderText(
    paste0(messages(), collapse = "\n")
  )
}

ui <- fluidPage(
  p(
    "Returning NULL causes the message to update two times: one time immediately, and one time after ",
    "the future (and subsequent promise) is resolved. "
  ),
  p(
    "Returning a promise causes the message to update only once: Shiny blocks ",
    "until the future (and subsequent promise) is resolved."
  ),
  radioButtons("return_value", "Return value for observer", c("NULL", "promise")),
  actionButton("go", "Go"),
  tags$pre(tags$code(textOutput("messages")))
)

shinyApp(ui = ui, server = server)
@jeffkeller87
Copy link

In particular, this section of the documentation led me to believe that returning a promise in an observer wouldn't be blocking.

https://rstudio.github.io/promises/articles/shiny.html#observers

For anyone who comes across this issue: There are (unsafe) ways of getting around this, such as returning NULL as the last statement in an observer. But, it would be great to have a safe means of doing this. You can indicate your support for such a request by upvoting this issue comment.

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