More meaningfull window title for Rstudio

2019-07-04 21:15发布

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())
}

1条回答
ゆ 、 Hurt°
2楼-- · 2019-07-04 21:59

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.

查看更多
登录 后发表回答