RInno shiny app unable to launch if previous session closed unexpectedly - r

I have made a Desktop shiny app for windows using the amazing package RInno but I have been experiencing some inconsistencies with launching the application. Occasionally the application will not quit its session properly, leaving R running in the background. Their github suggests using this chunk of code in the server function to insure R is properly terminated when the session ends. I can tell when the application quits properly because I include a custom function that copies the log files to a new indexed name for records and debugging.
if (!interactive()) {
session$onSessionEnded(function() {
write_logs_out() #My custom function. renames logs and stores backups of session data in .rds files
stopApp()
q("no")
})
}
In these cases a new log file is not made so I assume stopApp() and q("no") are never executed as well, leaving R running in the background. One major issue is I do not know how to reproduce this error. My current guess is that this occurs when the R session locally becomes unresponsive. While R is still running in the background, the shiny app never fully opens to a web browser. I will first need to open task manager and quit any R sessions (usually with the name "R front-end") in order to get the app working again.
Since I dont know how to expect this random error, I was wondering if any windows experts could help me write a script that will check if an R process is running, and then kill it. This would at least
I know that
tasklist | findstr Rscript.exe
will list all the current Rscripts running (which I believe is the "R front-end" i see in task manager), but I don't know if it is bad practice to simply include taskkill /IM "Rscript.exe" /F before the line that calls the .wsf file that starts the app.
The main problem I see with this is that if the session is running fine and the user for some reason clicks the app again, it will force a complete restart.
Any suggestions?

Related

Rscript slave call doesn't exit properly

I have a Desktop Entry that calls a shiny app.
Here's my desktop entry
[Desktop Entry]
Type=Application
Comment=App
Name=App
Exec=/usr/bin/Rscript -e "shiny::runApp('~/raspberry_IP/app.R', launch.browser=TRUE)"
Icon=/home/path/to/logo/rpi_logo.png
Terminal=false
Categories=Utility
StartupNotify=true
This runs well and the Shiny application works as expected. However, when I close my browser tab, I see the process is still running.
I am running this application from Ubuntu 20.04 LTS.
If I change my terminal=false to terminal=true. I see that the output of my terminal freezes (the app no longer updates, hence no diagnostic printing). But the terminal is still active (which makes sense given the process is still running). I can CTRL+C out of it to kill it from my terminal, but that's not the desired user behavior.
I see some strategies to stop the app using stopApp() like here. This strategy uses a separate action button to kill the app, so the user has to "manually" stop it. This might work, but I believe it's natural for the user to try to close the tab or Browser (and there's no way for them to know the process is still running, unless they check).
Using this on the server side, does kill the process, but it's recommended against (see here).
session$onSessionEnded(stopApp)
In this case, because my app is running on a local machine, I think I could potentially get away with this (there is only one user at a time), but I was wondering whether there's better practice to implement.

RStudio keeps loading prior session with no files and wont run commands

Rstudio keeps opening the same three files that I had opened before. Some strange things include:
Able to close the files just fine, but can't run any of the code. R just gets stuck.
Don't have any Files, plots, packages
Whenever closing or restarting R, it just gets stuck infinitely in the attached picture (I always have to force quit)
Whats stranger is I can start a New Session and all of a sudden everything works normally in the new session (e.g. files, packages is populated; can open/save/closeout and end the session) however the old session is still unusable and I have to force quit when I'm done.

Shiny application started with Rscript and CMD is unavailable after time

I have a small .bat script that runs an app using "Rscript" when the user logs in. After some time, the application / server becomes unavailable. There is no error, the page only loads for forever. When the app is started manually through RStudio there is no problem with the access to the app even after days of running. Any idea what causes this and how to solve it?
I think this will help you. It is supposed to be the correct way to launch a R Shiny app.
R -e "shiny::runApp('~/shinyapp')"
(1st result on Google ... Pardon my tone, but have you searched for this before to ask Stackoverflow how to deal with this problem?)

Launch shiny app with every new restart of R automatically

I have a rather large shiny app, where I execute longer computations, deployed on a local server.
The actual problem is that the RAM is not totally cleared with calls like gc() and rm() . After a few runs the RAM is not sufficient anymore and a restart of R is needed with .rs.restartR().
My approach so far was simply to launch the app with an .Rprofile and the "first" function like
.First <- function(){
setwd("directory")
shiny::runApp()}
I also tried to call the app in inside a script with source() instead of directly with shiny::runApp()
When opening RStudio it gives me a blank window but the app is called nevertheless.
I could live with the blank version of RStudio, but the computation takes a lot longer than normal (so long that i dont even know if it finishes at all).
Any suggestions of what could be the problem would be grateful.

RStudio Server on Microsoft Azure instance

I am currently running R on a Microsoft Azure instance (Ubuntu virtual machine) using RStudio as my IDE, to which I connect simply through my browser. I am trying to run some commands that take quite some time to complete from within RStudio and figured that I could simply close my tab with RStudio open and the process would keep running. However, when I try to reconnect to see how the process is doing, the page keeps loading but I am unable to see RStudio.
I have a few questions regarding running RStudio on a server:
First, am I correct in thinking that I can close my tab and keep the process running?
Second, is it normal behaviour that I am unable to connect to the server while the process is running?
Third, am I going about this the correct way or are there better ways?
Yes, you can close your tab and keep it running.
RStudio Server waits on updates from the R process to update the UI. This means that if you have a long-running computation, your tab may not fully reload until it's finished. You may also have seen this in the middle of a session: when R is busy, you can have problems saving scripts that are open in the editor pane.
Logging out in the middle of a computation should be safe, but be aware that RStudio will save your workspace and shut R down after a period of inactivity. It then reloads everything when you log back in. But this only extends to objects in memory; if you have any files saved in your temp directory, they'll have disappeared when you come back. They're probably still on the disk, but since your new R session has a new temp directory, you'll have to do a manual search for them.

Resources