3

I'm using R studio (Version 1.0.143) under Ubuntu (16.04) and the window title displays only a 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 this tab. It seems that under Windows the full path appears in the window title.

This might be useful for navigating between the windows but my main intended use is for softwares tracking the time spent in each software (like arbtt). For the moment I can only know that I spent say 20 hours in R studio last week but I would like to know in which files/projects.


There is a partial solution presented here after but if some knows how to obtain also the ful name and path of the current tab, I'm still interested.


Based on @Spacedman reply I can now obtain the working directory path (but not the script name) in the window title by adding this lines to /usr/lib/R/etc/Rprofile.site after installing 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 have already a window open with "rstudio" (case insensitive) in the title (for example in the web browser), this window will receive the new title and not the Rstudio window. There is a -F option to make the window title strictly identical to the title provided. I tried to first modify the RStudio title to a title less likely to be present in another window by adding this to the Rprofile.site :

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

The problem is that the system R function calls in the Rprofile.site seems to be ignored by Rstudio (while it works from R called outside rstudio)


In fact, the system command from Rprofile.site is not ignored. It is executed but for any reason the output is not shown in the Rstudio R console (eg if you type system("echo 'Hello World'")). See the discussion in this question
The reason that system('wmctrl -F -r "RStudio" -N "RStudio - @ "') does not work is probably that at the time this command is executed (when Rprofile.site is sourced by R), the RStudio windows is not yet present...

This is how I do now including the proposals from @Spacedman (ie using the hexadecimal ID and if(interactive())). It works well even if there is already another window open with "RStudio" in the title. It works also if you restart R from Rstudio. It will be broken (with a message) if you execute rm(list=ls()) (I personally never do that, I prefer restarting 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())
}
Gilles San Martin
  • 4,224
  • 1
  • 18
  • 31
  • 1
    1. You can use `wmctrl -i -r 0x04c0005c -N MyRStudio` to match a window by its unique hex ID rather than window title, which as you say, might not be unique. Not sure (yet) how an application can work out its ID but you could use `wmctrl -l` to build a menu and have the user choose? – Spacedman Jul 18 '17 at 07:01
  • 2. Not sure why it doesn't work in Rprofile.site. Either RStudio hasn't totally started yet, or hasn't set its window name properly, or the window manager hasn't mapped it yet. If you put a `wmctrl -l` in Rprofile.site, does it show the RStudio window existing? – Spacedman Jul 18 '17 at 07:02
  • 1
    3. You should probably hide all this stuff behind an `if(interactive())` block so you don't break R code running outside RStudio. – Spacedman Jul 18 '17 at 07:05
  • 1. The hex ID changes at each computer startup. I could use grep and/or sed to capture the right one but I'm stuck because R studio won't execute `system` commands from Rprofile.site – Gilles San Martin Jul 19 '17 at 08:52
  • 2. If I put `system("echo 'Hello world'")` in Rprofile.site I have no hello world message from Rstudio at startup while I have one when starting R from the shell. Maybe this is for another SO question ? – Gilles San Martin Jul 19 '17 at 08:54
  • 3. `if(interactive())` : great idea thanks ! – Gilles San Martin Jul 19 '17 at 08:54
  • 1. You could use `xwininfo` to prompt the user to click the RStudio window and get the hex id from that. Or if at the command line, `system("xprop -root _NET_ACTIVE_WINDOW")` should get the id of the window with focus. But RStudio might not take focus at start up... Anyway, its all a bit hacky (which is why I like it!). – Spacedman Jul 19 '17 at 09:15

1 Answers1

1

Install wmctrl and then you can change the title of something called "Calculator" to "Fnord" like this:

 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 could call this from system in R and paste the current working directory from getwd(). You can hook this into R to execute on every command line, at least on plain R, using addTaskCallback, but maybe RStudio mucks with this.

Example callback:

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

Change f to set the title using something like system(paste0("wmctrl ...")) and there you go.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • Thanks for the hack. It does not provides the script name and has some flaws (see my edited question) but it is already much better than just having "RStudio" as title. – Gilles San Martin Jul 17 '17 at 23:29