Programmatically close Data Viewer tabs in RStudio

Viewed 75

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?

1 Answers

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