More meaningful window title for Rstudio

I am using R studio (version 1.0.143) under Ubuntu (16.04) and the title bar only displays the very uninformative "RStudio".

I would like to have at least the name of the current tab, or ideally the full path to the file corresponding to that tab. It seems that under Windows the full path appears in the title bar.

This can be useful for navigating between windows, but my main use is software that keeps track of the time spent by each software (for example arbtt

). At the moment, I can only know that I spent 20 hours in R studio last week, but I would like to know which files / projects.


Below is a partial solution, but if anyone knows how to get the name and path of the current tab as well, I'm interested anyway.


Based on @ Spacedman's answer, I can now get the working directory path (but not the script name) in the window title by adding these lines in /usr/lib/R/etc/Rprofile.site

after installation wmctrl

:

RStudio_title <- function(...){system(paste0('wmctrl -r "RStudio" -N "RStudio - @ ', getwd(), '"')) ; TRUE}
addTaskCallback(RStudio_title, data = NULL, name = character())

      

One problem is that if you already have a window open with "rstudio" (case insensitive) in the title (for example, in a web browser), that window will get a new title, not an Rstudio window. There is an option -F

for the window title to be strictly identical to the title. I tried to first change the RStudio title to a title that is less likely to be present in another window by adding it to Rprofile.site

:

system('wmctrl -F -r "RStudio" -N "RStudio - @ "')

      

The problem is that the system

R function call in Rprofile.site seems to be ignored by Rstudio (while it is running from R, called by external rstudio)


In fact, the command system

from Rprofile.site is not ignored. It is executed, but for some reason the output is not shown in the Rstudio R console (for example if you type system("echo 'Hello World'")

). See the discussion in this question
The reason system('wmctrl -F -r "RStudio" -N "RStudio - @ "')

not working is probably because at the time of this command (when Rprofile.site is obtained from R) the RStudio windows are not yet present ...

This is how I am doing now, including suggestions from @Spacedman (i.e. using hex id and if(interactive())

). It works well even if there is already another window in the title that says "RStudio". It also works if you restart R from Rstudio. It will be broken (with a message) if you do rm(list=ls())

(I personally never do this, I prefer to restart R)

if(interactive()) {
    # function to capture the hexadecimal ID of the R studio window
    RStudio_ID <- function(...) {
        Rstudio_wmctrl_ID <<- system("wmctrl -l | grep 'N/A RStudio' | sed -r 's/\\s.*//'", 
            intern = TRUE); FALSE
    }
    # execute last function only once after the first completed top-level task 
    # (because the output of that function is FALSE)
    addTaskCallback(RStudio_ID, data = NULL, name = character())

    # function that will change the Rstudio window title
    RStudio_title <- function(...){system(paste0('wmctrl -i -r ', Rstudio_wmctrl_ID, 
        ' -N "RStudio - @ ', getwd(), '"')) ; TRUE}

    # this function is executed after every completed top-level task
    addTaskCallback(RStudio_title, data = NULL, name = character())
}

      

+3


source to share


1 answer


Install wmctrl

and then you can change the heading "Calculator" to "Fnord" as follows:

 xcalc &
 wmctrl -r Calculator -N "Fnord"

      

So, you just need the current title ("RStudio"?) Or maybe its ID (gettable with wmctrl -l

) and there you go.

You can call this from system

in R and insert the current working directory from getwd()

. You can hook it up to R to run on every command line, at least in regular R using addTaskCallback

, but perhaps RStudio does it with.

Callback example:

Define a function:



> f = function(...){cat("Hello\n");TRUE}

      

Add it to the task callbacks:

> addTaskCallback(f, data = NULL, name = character())
1 
1 
Hello

      

Now R says "Hello" after every command:

> ls()
[1] "f"
Hello

      

Modify f

to set the title using something like system(paste0("wmctrl ..."))

and there you go.

+1


source







All Articles