21 May 2013

R Quick Tip: Shutdown Windows after Script Has Finished

Quite often I have long procedures running and want to do this over night. However, my computer would still be running all night after the script has finished. This is easily circumvented by the following lines that I put at the end of such a script:

# set working dir
# setwd("C:/Users/Kay/Desktop")
 
# long procedure:
for(i in 1:1e+5) {cat(i); cat("\n..................\n")}
 
d <- "something"
 
# save history
savehistory()
 
# and worspace
save.image()
 
# then shutdown after 240 s
system("C:/Windows/system32/shutdown.exe -f -s -t 240")
 
# this would abort the shutdown:
# system("C:/Windows/system32/shutdown.exe -a")

2 comments :

  1. Hi Kay,
    I've made a bunch of such functions in my installr pacakge (http://cran.r-project.org/web/packages/installr/)

    They can all be accessed through a gui by using os.manage()
    Or by calling each function manually.

    These functions are defined here:
    https://github.com/talgalili/installr/blob/master/R/os.manage.r

    Please feel free to make any suggestion for improvement.

    With regards,
    Tal

    ReplyDelete
    Replies
    1. It was very likely that I was not the first to have come across this! However, I wouldn't have suspected it to be already included in a package! Good to know that. I like your os.sleep() - in many cases it will be more convenient to just let the system sleep instead of shuting it dow, i.e., no save and load needed!

      Thanks,
      Kay

      Delete