0

I'm trying to make R restart on quit, so I'm using .Last in my .Rprofile. Solution modified from Quit and restart a clean R session from within R?, and I've tried the other solutions in that answer to no avail. my .First() works fine, but when I q() at the end, it prompts for "save workspace image?" and after answering, it insta-closes

.Last <- function()
{
 print("new R in 3 seconds")
Sys.sleep(3)
 system("R --no-save")
 print("close R in 3 seconds")
 Sys.sleep(3)
}
print("test")

(the print at the end is because there's a hidden "feature" where .Rprofile ignores the last line if theres no newline, I know it shouldn't execute that.)

Community
  • 1
  • 1
hedgedandlevered
  • 2,314
  • 2
  • 25
  • 54
  • The code you have posted comes from some comments below that post but the actual solution in that post works for me. – G. Grothendieck Apr 11 '14 at 14:46
  • yeah, my comment below *that* comment was actually in reference to the original solution, which I tested and also did not work. "refresh" just closes R. When you tested it, did you have a ".First" function also in your Rprofile? – hedgedandlevered Apr 11 '14 at 14:56
  • The solution works for me. My .Rprofile only sets a few options. – G. Grothendieck Apr 11 '14 at 15:03
  • ah. I ran shell("RGui") by itself and the answer became obvious... shell(paste0(R.home(),"/bin/i386/RGui")) works. I had setwd() earlier away from the default. I still don't get why ".Last" isn't executing though, isn't RProfile loaded in its entirety when R opens? the system("R") would need the same adjustment, of course, but Sys.sleep(3) wasn't executing either – hedgedandlevered Apr 11 '14 at 15:09
  • For most people `Rgui.exe` is on their path so there is no need to specify the path. If you prefer not to do that then this should work: `shell(file.path(R.home("bin"), "Rgui.exe"))` – G. Grothendieck Apr 11 '14 at 15:27
  • I'm running this from "R.exe" instead of "Rgui", so that works but it just spawns a "child process" of Rgui. from that child process, I can call refresh and it refreshes the way I want it to. However, the original R.exe is just sitting there while all this is happening... – hedgedandlevered Apr 14 '14 at 12:21
  • See https://bugs.r-project.org/bugzilla/show_bug.cgi?id=4056 – bers Oct 20 '20 at 11:37

0 Answers0