我在 Ubuntu(16.04)下使用 R studio(版本 1.0.143),窗口标题只显示一个非常无信息的“RStudio”。
我希望至少有当前选项卡的名称,或者理想情况下是与此选项卡对应的文件的完整路径。似乎在 Windows 下,完整路径出现在窗口标题中。
这对于在窗口之间导航可能很有用,但我的主要用途是用于跟踪每个软件所花费时间的软件(如arbtt
)。目前我只能知道上周我在 R 工作室花了 20 个小时,但我想知道在哪些文件/项目中。
此处提供了部分解决方案,但如果有人知道如何获取当前选项卡的完整名称和路径,我仍然感兴趣。
根据@Spacedman 的回复,我现在可以通过/usr/lib/R/etc/Rprofile.site
在安装后添加以下行来获取窗口标题中的工作目录路径(但不是脚本名称) wmctrl
:
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
:
system('wmctrl -F -r "RStudio" -N "RStudio - @ "')
问题是system
Rprofile.site 中的 R 函数调用似乎被 Rstudio 忽略(而它在 Rstudio 外部调用的 R 中工作)
事实上,system
来自 Rprofile.site 的命令并没有被忽略。它被执行,但出于任何原因,输出未显示在 Rstudio R 控制台中(例如,如果您键入system("echo 'Hello World'")
)。请参阅此问题中的讨论不起作用
的原因可能是在执行此命令时(当 Rprofile.site 由 R 提供时),RStudio 窗口尚不存在...system('wmctrl -F -r "RStudio" -N "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())
}