以编程方式关闭 RStudio 中的数据查看器选项卡

发布于 2025-01-15 11:59:44 字数 456 浏览 2 评论 0原文

我想制作一个脚本来关闭 RStudio 中的所有数据查看器选项卡(通过单击“环境”窗格中的数据对象或通过调用 utils::View() 来调用这些选项卡),但保留所有“通常的”文档选项卡。
首先,我发现 rstudioapi::documentClose() 函数 - 不确定它是否适用于数据查看器选项卡,它需要的文档 ID 似乎不适用于此处:调用 getActiveDocumentContext()<数据查看器选项卡上的 /code> 返回 #console
然后,有 executeCommand('closeSourceDoc') 选项用于关闭当前选项卡,无论是数据查看器还是标准文档。我可能可以使用 executeCommand('nextTab') 循环遍历所有打开的选项卡,但我找不到如何确定活动选项卡是否是数据查看器...
有什么提示吗?

I wanted to make a script that closes all Data Viewer tabs in RStudio (those invoked by clicking on a data object in the Environment pane, or by calling utils::View()) but keeps all the "usual" document tabs.
First, I found rstudioapi::documentClose() function - not sure if it works for Data Viewer tabs, it requires the document id that seems to be not applicable here: calling getActiveDocumentContext() on Data Viewer tab returns #console.
Then, there's executeCommand('closeSourceDoc') option that closes the current tab, whether it is Data Viewer or standard document. I could probably use executeCommand('nextTab') to loop through all opened tabs, but I can't find how to determine if the active tab is Data Viewer or not...
Any hints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安人多梦 2025-01-22 11:59:44

下面的代码似乎可以满足您的要求。

Tabs=c()
doc=rstudioapi::documentPath()
while (is.null(doc)||!doc%in% Tabs) {  
   if(is.null(doc)){    
     rstudioapi::executeCommand('closeSourceDoc')  
   }     
   rstudioapi::executeCommand('nextTab')
   Tabs=c(Tabs,doc);  
   doc=rstudioapi::documentPath()
}

The following code seems to do what you want.

Tabs=c()
doc=rstudioapi::documentPath()
while (is.null(doc)||!doc%in% Tabs) {  
   if(is.null(doc)){    
     rstudioapi::executeCommand('closeSourceDoc')  
   }     
   rstudioapi::executeCommand('nextTab')
   Tabs=c(Tabs,doc);  
   doc=rstudioapi::documentPath()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文