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

Rstudio Viewer window not closed on shiny::stopApp() #2033

Closed
vnijs opened this issue Apr 21, 2018 · 5 comments
Closed

Rstudio Viewer window not closed on shiny::stopApp() #2033

vnijs opened this issue Apr 21, 2018 · 5 comments
Assignees

Comments

@vnijs
Copy link
Contributor

vnijs commented Apr 21, 2018

@kevinushey A while back I posted an issue/question on Rstudio Community about Rstudio Viewer (and Rstudio Window) not (always) closing when closing a shiny app. I now have a minimal reproducible example (see code below). If you start the app and click the Stop button the app stops and the window closes. However, if you click the Refresh button first and then click on the Stop button the window does not close, even though the shiny app does stop. The error that pops-up from shinyapp.js is shown in the screen shot below.

It seems that the Rstudio browser tries to reset the port when the Refresh button is pressed and the code in shinyapp.js is not able to capture that.

I think this might be an issue in Rstudio because I don't see it in Chrome or other browsers. I'm posting here but cc-ing @kevinushey (and @jcheng5 and @bborgesr)

This is an important issue in all my apps (e.g., radiant.data). Being able to refresh the (Rstudio) browser from code is key to (fully) (re)setting the state of an app (i.e., data, inputs, etc.) based on a previous state loaded by the user.

[Error] Unable to post message to http://127.0.0.1:4535. Recipient has origin http://127.0.0.1:25108.

image

ui <- fluidPage(
  actionButton("refresh", "Refresh", icon = icon("refresh")),
  uiOutput("refresh_window"),
  actionButton("stop", "Stop", class = "btn-danger", onclick = "setTimeout(function(){window.close();}, 100);")
)

server <- function(input, output) {
  observeEvent(input$stop, {
    stopApp(message("App stopped"))
  })

  output$refresh_window <- renderUI({
    req(input$refresh)
    tags$script("window.location.reload();")
  })
}

shinyApp(ui = ui, server = server)

image

@jcheng5
Copy link
Member

jcheng5 commented May 3, 2018

The problem appears to be this:

var parentUrl = (parent !== window) ? document.referrer : null;

document.referrer starts out as the URL for RStudio itself, but after reload, it becomes the URL for the Shiny app. So in the latter case we call postMessage with the wrong origin. I guess we need to rely on something other than document.referrer.

@jcheng5
Copy link
Member

jcheng5 commented May 3, 2018

Alternatively, we could decide that the fact that the app is disconnected is not privileged information and postMessage('disconnected', '*').

@wch
Copy link
Collaborator

wch commented May 3, 2018

@jcheng5 I think using '*' is OK here.

@jcheng5
Copy link
Member

jcheng5 commented May 3, 2018

@vnijs, my PR fixes it, but it's too late for Shiny v1.1--we'll merge it as soon as the master branch opens back up for Shiny v1.2. In the meantime, you can work around this issue by including the following JS snippet:

tags$script("$(document).on('shiny:disconnected', function() {window.parent.postMessage('disconnected', '*');});")

Thanks for another great bug report!

@vnijs
Copy link
Contributor Author

vnijs commented May 4, 2018

Wonderful! The work around works great! Thanks so much @jcheng5

@vnijs vnijs closed this as completed May 4, 2018
@ajmcoqui ajmcoqui removed the review label May 4, 2018
alandipert added a commit that referenced this issue Sep 14, 2018
Fix #2033: Rstudio Viewer window not closed on shiny::stopApp()
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

4 participants