I'm using Rstudio in Windows. There is no red octagon for me to click on. I've tried pressing ESC and Ctrl + C and Ctrl + Z in the console but none of those worked.
When running a code, the red octagon will only show while it is working things out. So while it is just running through your written code (reading data and names of things etc) then the octagon will not show.
Pressing ESC will work, unless Rstudio is frozen.
Good luck!
You could also try interrupting/terminating the R session via the menu:
Session -> Interrupt R
or
Session -> Terminate R...
or
Session -> Restart R (Ctrl+Shift+F10)
If nothing else helps, open the Windows command line and kill the rsession.exe proces:
> tasklist | grep rsession
# the output should be something like this:
rsession.exe 7260 Console 1 166,144 K
# The number (7260 in this case; will probably be different) is the process id (PID),
# and you can use it to kill the process:
> taskkill /F /PID 7260
Caution: this will forcefully stop the R session, and RStudio will probably have to restart (this is what happens on my machine, at least).
Using ctrl + alt + delete opens task manager and you can terminate the R session then restart it to continue working.
Related
I am writing an R code on a Linux system using RStudio. At some point in the code, I need to use a system call to a command that will download a few thousand of files from the lines of a text file:
down.command <- paste0("parallel --gnu -a links.txt wget")
system(down.command)
However, this command takes a little while to run (a couple of hours), and the R prompt stays locked while the command runs. I would like to keep using R while the command runs on the background.
I tried to use nohup like this:
down.command <- paste0("nohup parallel --gnu -a links.txt wget > ~/down.log 2>&1")
system(down.command)
but the R prompt still gets "locked" waiting for the end of the command.
Is there any way to circumvent this? Is there a way to submit system commands from R and keep them running on the background?
Using ‘processx’, here’s how to create a new process that redirects both stdout and stderr to the same file:
args = c('--gnu', '-a', 'links.txt', 'wget')
p = processx::process$new('parallel', args, stdout = '~/down.log', stderr = '2>&1')
This launches the process and resumes the execution of the R script. You can then interact with the running process via the p name. Notably you can signal to it, you can query its status (e.g. is_alive()), and you can synchronously wait for its completion (optionally with a timeout after which to kill it):
p$wait()
result = p$get_exit_status()
Based on the comment by #KonradRudolph, I became aware of the processx R package that very smartly deals with system process submissions from within R.
All I had to do was:
library(processx)
down.command <- c("parallel","--gnu", "-a", "links.txt", "wget", ">", "~/down.log", "2>&1")
processx::process$new("nohup", down.comm, cleanup=FALSE)
As simple as that, and very effective.
I was wondering if in R there is a way/keyboard shortcut/command/processing-thread-killing-alternative. For instance, using RStudio as IDE, you got a little stop button to kill the current processing thread(s), but most of the times it ends up in the prompt to end the session and restart the environment. Suggestions?
Ctrl + C <- will stop the R process without exiting the R session
and not
Ctrl + Z <- will stop the R process and exit the R session
I want R to shutdown my computer after my (extensive) simulation and saving results, is this possible?
Yes, look at the function shutdown in the package fun.
The flags for system command shutdown depends on your operating system, the function simply calls the appropriately flagged command.
fun::shutdown
function (wait = 0)
{
Sys.sleep(wait)
ifelse(.Platform$OS.type == "windows", shell("shutdown -s -t 0"),
system("shutdown -h now"))
}
R can send commands to the system with ?system, and so whatever is required for Windows can be done with that:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/shutdown.mspx?mfr=true
R has a .Last() function controlled by quit() (or q()) with its runLast argument, so this is where you would send the shutdown commands via system, so that it occurs after quitting R. Saving objects with R is done with save or save.image, though there is a default to save as well with quit().
Executing
system("shutdown -s")
will shut your computer down. Just add it at the end of your script
When running R in 'batch mode', the interactive flag is set to false, which prevents any sort of user prompts that would interrupt the process in the context of an embedded/batch setting. See:
R -e 'interactive()'
Some holds when using Rscript. The flag can also manually be set by using the interactivity package. To test, we can try:
R -e 'install.packages("anything")
Assuming no CRAN mirror has been set yet, install.packages will not show some interactive mirror picker, but instead throw an error and exit.
However, I noticed that the help or ? command will still work. For example, the command below will show the help browser, and it does not continue until I press q.
R -e 'help(lm); 1+2'
However, when sending the output to a file, it does not show the interactive prompt and just dumps the complete help document in the output:
R -e 'help(lm); 1+2' > output.txt
I'm a bit confused now about what is really going on when using help. Why do you get the interactive help browser even when R is running in batch mode? Is this a feature of R, or from the Debian/Ubuntu front-end? Is there a risk of R becoming unresponsive when the help function is called inside i.e. Rserve, RInside, JRI, etc, due to R waiting for using input?
help actually spawns a new process, pager, which is like "less":
> help(lm); 1+2
[1]+ Stopped R -e 'help(lm); 1+2'
$ ps
PID TTY TIME CMD
27735 pts/6 00:00:00 bash
31607 pts/6 00:00:00 R
31615 pts/6 00:00:00 sh
31616 pts/6 00:00:00 pager
31617 pts/6 00:00:00 ps
I want R to shutdown my computer after my (extensive) simulation and saving results, is this possible?
Yes, look at the function shutdown in the package fun.
The flags for system command shutdown depends on your operating system, the function simply calls the appropriately flagged command.
fun::shutdown
function (wait = 0)
{
Sys.sleep(wait)
ifelse(.Platform$OS.type == "windows", shell("shutdown -s -t 0"),
system("shutdown -h now"))
}
R can send commands to the system with ?system, and so whatever is required for Windows can be done with that:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/shutdown.mspx?mfr=true
R has a .Last() function controlled by quit() (or q()) with its runLast argument, so this is where you would send the shutdown commands via system, so that it occurs after quitting R. Saving objects with R is done with save or save.image, though there is a default to save as well with quit().
Executing
system("shutdown -s")
will shut your computer down. Just add it at the end of your script