Rstudio更有意义的窗口标题 [英] More meaningfull window title for Rstudio

查看:234
本文介绍了Rstudio更有意义的窗口标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ubuntu(16.04)下使用R Studio(版本1.0.143),并且窗口标题仅显示了一个非常不实用的"RStudio".

我希望至少具有当前选项卡的名称,或者最好具有与该选项卡相对应的文件的完整路径.在Windows下,完整的路径似乎出现在窗口标题中.

这对于在窗口之间导航可能很有用,但是我的主要用途是用于跟踪每个软件所花费时间的软件(例如arbtt).目前,我只能知道我上周在R Studio中花了20个小时,但我想知道在哪些文件/项目中.


此后提供了部分解决方案,但是如果有人知道如何也获取当前选项卡的实名和路径,我仍然很感兴趣.


基于@Spacedman的回复,我现在可以通过在安装wmctrl之后将这些行添加到/usr/lib/R/etc/Rprofile.site来获取窗口标题中的工作目录路径(而不是脚本名称):

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

一个问题是,如果您已经打开一个标题中带有"rstudio"(不区分大小写)的窗口(例如,在Web浏览器中),则该窗口将收到新标题,而不是Rstudio窗口.有一个-F选项可以使窗口标题与提供的标题严格相同.我尝试通过将RStudio标题添加到Rprofile.site:

来首先将RStudio标题修改为不太可能出现在另一个窗口中的标题

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

问题是Rstudio中的system R函数调用似乎被Rstudio忽略了(尽管它在Rstudio之外的R中起作用)


实际上,不会忽略Rprofile.site中的system命令.它已执行,但是由于任何原因,输出都不会显示在Rstudio R控制台中(例如,如果键入system("echo 'Hello World'")).请参阅此问题中的讨论
system('wmctrl -F -r "RStudio" -N "RStudio - @ "')不起作用的原因可能是在执行此命令时(当Rprofile.site由R来源时),RStudio窗口尚不存在...

这就是我现在包括@Spacedman的建议的方法(即使用十六进制ID和if(interactive())).即使标题中已经打开另一个带有"RStudio"的窗口,它也能很好地工作.如果从Rstudio重新启动R,它也可以工作.如果执行rm(list=ls()),它将被破坏(带有一条消息)(我个人从不这样做,我更喜欢重新启动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())
}

解决方案

安装wmctrl,然后可以将名为"Calculator"的名称更改为"Fnord",如下所示:

 xcalc &
 wmctrl -r Calculator -N "Fnord"

因此,您只需要当前标题("RStudio"?)或它的ID(可通过wmctrl -l获取)就可以了.

您可以从R中的system调用它,然后从getwd()粘贴当前工作目录.您可以使用addTaskCallback将其挂接到R以在每个命令行上执行,至少在普通R上执行,使用addTaskCallback,但也许RStudio对此不满意.

示例回调:

定义一个功能:

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

将其添加到任务回调中:

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

现在R在每个命令后都说你好":

> ls()
[1] "f"
Hello

更改f以使用system(paste0("wmctrl ..."))这样的标题来设置标题.

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

解决方案

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.

这篇关于Rstudio更有意义的窗口标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆