randy3k / radian

A 21 century R console
MIT License
2k stars 76 forks source link

option to restart R session #296

Open maxheld83 opened 3 years ago

maxheld83 commented 3 years ago

something akin to rstudioapi::restartSession() would be great.

Not sure whether that's in scope for / possible for radian.

Same as:

q()
r

but could save you one line 🙂

randy3k commented 3 years ago

Here you are. :)

restart <- function() {
    getOption("rchitect.py_tools")$attach()
    os <- import("os")
    sys <- import("sys")
    os$execv(sys$executable, c("sys$executable", "-m", "radian"))
}
randy3k commented 3 years ago

I think at some point, we could move something like this to an R package, say, radianapi or just radian.

HenrikBengtsson commented 2 years ago

A few comments:

@randy3k, you may want to support restarting with different CLI options, e.g.

radian_restart <- function(args = NULL) {
  getOption("rchitect.py_tools")$attach()
  os <- import("os")
  sys <- import("sys")
  os$execv(sys$executable, c("sys$executable", "-m", "radian", args))
}

@maxheld83, contrary to the RStudio menu 'Session' -> 'Restart R', with rstudioapi::restartSession() you don't end up in fresh, empty R session. Instead, restores your global environment, sets you up with the same packages loaded/attached, etc. as before. So, it's not like quit() followed by starting R again.

FWIW, startup::restart() is capable of restarting radian as-is, or with different CLI options, e.g. startup::restart(args = "--no-init-file"). If you're interested, it achieves this by (a) registering a .Last() command that will launch the commandArgs()[1] binary via system2() call, and then (b) calling quit(runLast = TRUE) that will terminate the current R session, and call .Last() on the way out. Somewhat surprisingly, this does work without messing up the terminal.

sofinico commented 2 years ago

Hi! Where should I declare those functions in order to use them in radian console?

donyunardi commented 1 year ago

I had the same question and ended up adding them to keybinding.json.

    {
        "description": "r restart R session",
        "key": "ctrl+shift+0",
        "command": "r.runCommand",
        "args": "getOption(\"rchitect.py_tools\")$attach();os <- import(\"os\");sys <- import(\"sys\");os$execv(sys$executable, c(\"sys$executable\", \"-m\", \"radian\"))"
    },

Once saved, I can just press ctrl+shift+0 (trying to mimic RStudio's cmd+shift+0) to restart radian R session.

gadenbuie commented 1 year ago

@HenrikBengtsson When I use startup::restart() in radian inside vscode, it seems to not source my ~/.Rprofile, which then causes the session to lose its connection with vscode. Have you seen this behavior and/or do you have an idea about how to work around it (other than sourcing my .Rprofile manually)?

HenrikBengtsson commented 1 year ago

@gadenbuie, srry, I don't use Radian on a regular basis, so I don't know a workaround. That said, startup::restart() has not been updated to make use of the approach @randy3k suggested in https://github.com/randy3k/radian/issues/296#issuecomment-885271114. Maybe that works? Can you check if that works for you? If it does, then I'll make sure it's incorporated into startup.

grcatlin commented 1 year ago

@gadenbuie @HenrikBengtsson I ran into the same issue and putting this as a keybinding seems to source .Rprofile upon restart:

    {
        "key": "ctrl+shift+f10",
        "command": "runCommands",
        "args": {
            "commands": [
                "workbench.action.terminal.kill",
                "r.createRTerm"
            ]
        }
    }

I think this assumes that you have an active radian terminal but this is usually true for me.

gadenbuie commented 1 year ago

the approach @randy3k suggested in #296 (comment). Maybe that works? Can you check if that works for you? If it does, then I'll make sure it's incorporated into startup.

@HenrikBengtsson unfortunately in my testing that approach has the same result, which makes me wonder if this is something specific to radian?

@grcatlin Thanks for the tip! I'm basically doing the same thing manually so why not turn it into a shortcut? 😄

caiohamamura commented 5 months ago

In current version of vscode there is already an rstudioapi emulator, so you can actually use rstudioapi::restartSession().