Second shiny app in a script not loading in browser

I have had in the last few days a very strange issue: I am observing that the same shiny app loads properly when I run my code line-by-line in RStudio, but it does not load when run as part of a larger code chunk run as one. In both cases this seems to be related to "leaky" abnormal behaviour from curly braces (where forward code starts while prior code isn't done running).

I have observed two similar instances with different apps/scripts:

  • Case 1:
    A simple app loads when code is executed line-by-line or as a whole by selecting the whole script directly, but not when the script is executed using the source() function. Inserting a time delay with Sys.sleep(10) did not help. Incidentally, in this case I also observe some "leakage" occurring, i.e. if a master script sequentially sources several smaller scripts (which I call "chunks" and are sequentially overwritten to the same local temp_chunk.R file), then I see chunk N+1 start executing when chunk N is still waiting for a Shiny app to load/complete.
    For example, if I have two chunks, each with one app, then both would work if executing the script directly but if sourcing chunk 1 then 2, app #2 fails to load. If I duplicate chunk 1 in position 2, so that now the app which was originally not loading is shifted to position 3, something does load for each position, but at position 2 it is the app from 3 (hence, again, "leakage" between chunks: the code creating the ui and server for that app should not have been run yet, the chunk itself should not even have been written to the temporary file which the source() call runs!)
    Since my successive apps were overwriting the same chunk file, ui and server variables, and using the same port, I figured I should disentangle them by indexing temporary files, variable names, and ports, to avoid leakage due to synchronisation issues. With these changes, chunk 2 (duplicate of 1) once again does not load.

  • Case 2:
    An if statement, if TRUE, will execute a very complex Shiny app then a simpler one. Code within the curly braces can be successfully run line-by-line, but when executing the whole curl brace the second app does not load. I was able to solve that one by breaking the if statement into two separate curly braces controlled by the same repeated if check. After I did this, the code works even if I select the whole script and hit Ctrl+Enter.

In both cases, I get the following error in Firefox (port # may vary; setting each to separate ports does not help):

Unable to connect

Firefox can’t establish a connection to the server at

    The site could be temporarily unavailable or too busy. Try again in a few moments.
    If you are unable to load any pages, check your computer’s network connection.
    If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the web.

If I run the app in a popup in RStudio instead of my browser, the popup remains gray forever.

Interestingly, if I set the IP to instead of, the browser error still refers to

Unfortunately, my code is massive and the issue is not easily reproducible, so I do not think that I am able to create a reproducible example easily. I can try to make it work if necessary, but for now I would appreciate a discussion on what might be cause.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.