rstudio / rstudioapi

Safely access RStudio's API (when available)
http://rstudio.github.io/rstudioapi
Other
166 stars 36 forks source link

Feature request: list all open documents in a rstudio-session #72

Open thorstenhenke opened 6 years ago

thorstenhenke commented 6 years ago

Dear rstudioapi developers,

Is there a way to list all open or active documents in a rstudio session? The function I envision would be an equivalent to the rstudioapi::terminalList() function, e.g. rstudioapi::documentsList().

Thanks, Thorsten

kevinushey commented 6 years ago

Good question! This functionality does not currently exist (right now, we only have an API for getting the currently active document). I agree this would be worth implementing.

muhligs commented 5 years ago

I suppose something like this exists for the documentSaveAll() method to work, and thus could be exported for general usage? (and I agree that it would be very useful).

stevenjwest commented 4 years ago

This would really open up what one can achieve with scripting in RStudio, are there any plans to implement this any time soon? Has anyone figured out a way to compute the IDs or Document Contexts for all open Documents in RStudio?

stevenjwest commented 4 years ago

Hey all, I have been looking into this problem periodically. I recently explored the rstudioapi repository, but alas the functions getActiveDocumentContext() and getSourceEditorContext() from rstudioapi::studio-editors point to underlying cpp code, and its not clear to me how to return all Document IDs from this code base.

However, I have found an interesting 'hack' that can return all open RStudio document IDs and paths. I have included a function - getRStudioOpenDocIDs(), found in the document-functions.R file - in my R package, projectmanagr.

Essentially, the function reads from the ~/.rstudio-desktop/ config directory all the currently open documents' IDs and absolute paths on disk (if saved to disk), and saves these to a returned list. The returned list can then be interrogated for these data.

I have tested in RStudio, and open documents can be navigated to in the Source Console using rstudioapi::navigateToFile(returnedList[[1]][2]). Once the file has been made the active document, all the other rstudioapi functions will work on it (i.e. rstudioapi::getActiveDocumentContext() to get the context, and then further document manipulation as desired).

Hopefully this hack will help anyone else with a requirement for this type of functionality, until the rstudioapi adds it.

Cheers!

muhligs commented 2 years ago

It appears on my Ubuntu system that the path to the s-* directory is different and found in ~/.local/share/rstudio/sources/

The folder contain the content of the files in ID-contents files as well as ID meta files with names and last modification time stamps and more.

I made a simple function to output the paths of all currently open files in Rstudio along with timestamps of their last edits. Essentially the same as stevenjwest's script above, but where output is the file paths.
This is available here