Available RStudio Hooks

What RStudio hooks are available? I've found

setHook("rstudio.sessionInit", function(newSession) {

But this doesn't get executed when I restart R, only when starting a project.

You can control some aspects of your RStudio session using:

1 Like

Thanks @andrie

The problem I'm trying to solve is I want to run some rstudioapi functions in the Rprofile. For example,

rstudioapi::getActiveProject()

However, since RStudio hasn't started, just adding this to the Rprofile doesn't work (hence the hook). But the hook doesn't seem to be called on restarting R.

I managed to get this to work:

setHook("rstudio.sessionInit", function(newSession) {
  if (newSession) {
    message("Welcome to RStudio ", rstudioapi::getVersion())
  } 
  ap <- rstudioapi::getActiveProject()
  if (is.null(ap)) ap <- "No active project"
  message("Active RStudio project: ", ap)
}, action = "append"
)
3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.