rstudio / rstudioapi

Safely access RStudio's API (when available)
http://rstudio.github.io/rstudioapi
Other
167 stars 36 forks source link

Add ability to show the current session ID #237

Open ronblum opened 3 years ago

ronblum commented 3 years ago

It might prove useful to get the current session ID within RStudio, for use in the Console, R scripts, R Markdown, etc. Specifically, I have a case in which it's useful for conducting some testing. The value would be the same as the environment variable RSTUDIO_SESSION_ID that is available when opening the Terminal.

Meanwhile, I can sort of fake it with the Terminal:

temp <- rstudioapi::terminalExecute("echo -n $RSTUDIO_SESSION_ID > ~/current_session_id", show = FALSE)
session_id <- readr::read_file(file = "~/current_session_id")
session_id

results in

> temp <- rstudioapi::terminalExecute("echo -n $RSTUDIO_SESSION_ID > ~/current_session_id", show = FALSE)
> session_id <- readr::read_file(file = "~/current_session_id") 
> session_id 
[1] "d963e8cd"
>

Similarly with

term <- rstudioapi::terminalExecute("echo $RSTUDIO_SESSION_ID", show = FALSE)
while (is.null(rstudioapi::terminalExitCode(term))) { Sys.sleep(0.01) }
session_id <- rstudioapi::terminalBuffer(term)